This entry is part 7 of 9 in the series Artificial Galaxies

This has been fun to be sure, but what about saving our data out and loading it as well? The fancy word for this is serialization, and this is where the OOP principles can really shine!

If we build into each of our classes a ‘Serialize’ and ‘DeSerialize’ feature, we can have it spew out the data into a CSV or JSON formatted file, and we’ll be able to load it back up with minimal hassle. We’ll make a function called ‘Serialize’ in each class, and make it only save out the data that’s relevant. 

Usually, I only serialize what I set up in  the initialization, i.e.:

So we’d wanna make a Serialize function that could spew out all the needed variables as text, JSON preferably. As for the objects that they’re pointing to, like a Ship’s destination, for instance, we’ll write out the ‘.name’ variable, where we can point it back to the object itself when we DeSerialize it. 

I’ve moved the Ship and Star creation methods into the Galaxy object itself, too, to further the encapsulation methodology of OOP. I know I haven’t gotten too deep into OOP, but we’ll make it there I’m sure. 🙂 

So, after all is said and done, I’ve created the Galaxy:Serialize() function so we can call it and we get output like this: (it’s long, sorry, but you can see it’s kinda nice to have relatively human-readable output):


So notice a couple things, we have 3 parent branches; Stars, Ships and Civilizations, and under those we have outputted our data. 

To make the filesize smaller, for example, we can use index numbers for a master table:

local environment = {“Chthonian planet”,”Carbon planet”,”City planet”,”Coreless planet”,”Desert planet”,”Gas dwarf”,”Gas giant”,”Helium planet”,”Ice giant”,”Ice planet”,”Iron planet”,”Lava planet”,”Ocean planet”,”Protoplanet”,”Puffy planet”,”Silicate planet”,”Terrestrial planet”}

[11, “Karis I”],

[11, “Karis II”],

[14, “Karis III”]

Where the 11 and 14 next to the Karis entries correspond to the environment table entries. We could definitely just output the human readable portion to the file in JSON, that would make it easier to read on the fly. I was just experimenting with efficiency. 

So, we now have Serialization. What about de-serialization? Tune in next time!

PS: Aliens


Series Navigation<< Territory BordersDeserialization >>