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.:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
function Ship:init(data) self.name = data.name or "Ship "..RAND(1,999) self.x = data.x or RAND(1,200) self.y = data.y or RAND(1,200) self.dockedAt = data.dockedAt or nil self.civilization = data.civilization or nil self.speed = data.speed or RAND(1,15) self.heading = data.heading or 0 self.destination = data.destination or nil -- print("Created Ship: "..self.name.." at "..self.x..","..self.y..", belongs to "..self.civilization.name.." docked at "..self.dockedAt.name) end function Ship:Serialize() local results = {} table.insert(results,self.x) table.insert(results,self.y) table.insert(results,self.speed) table.insert(results,self.heading) table.insert(results,self.name) table.insert(results,self.civilization.name) -- We can match the name to saved data on de=serialize table.insert(results,self.destination.name) -- We can match the name to saved data on de=serialize return results end |
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):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
{ "stars": [ [1569, 510, "Star 99", "none", [ [16, "Star 99 I"], [1, "Star 99 II"], [4, "Star 99 III"], [10, "Star 99 IV"] ]], [1294, 605, "Calimede", "none", [ [1, "Calimede I"], [10, "Calimede II"], [2, "Calimede III"], [7, "Calimede IV"], [15, "Calimede V"] ]], [1190, 317, "Psamaka", "none", [ [5, "Psamaka I"], [14, "Psamaka II"], [13, "Psamaka III"], [17, "Psamaka IV"], [10, "Psamaka V"], [9, "Psamaka VI"] ]], [920, 202, "Anand", "Empire 258", [ [2, "Anand I"], [16, "Anand II"], [2, "Anand III"], [7, "Anand IV"], [15, "Anand V"], [16, "Anand VI"] ]], [1516, 721, "Kallene", "none", [ [5, "Kallene I"], [2, "Kallene II"], [8, "Kallene III"] ]], [142, 972, "Hegemona", "none", [ [14, "Hegemona I"], [1, "Hegemona II"], [6, "Hegemona III"], [13, "Hegemona IV"], [16, "Hegemona V"], [10, "Hegemona VI"] ]], [400, 525, "Pasiphano", "none", [ [17, "Pasiphano I"], [3, "Pasiphano II"], [1, "Pasiphano III"], [15, "Pasiphano IV"] ]], [1728, 583, "Helia", "none", [ [16, "Helia I"], [2, "Helia II"], [2, "Helia III"], [16, "Helia IV"], [5, "Helia V"] ]], [1102, 868, "Amaka", "none", [ [10, "Amaka I"], [16, "Amaka II"], [4, "Amaka III"], [7, "Amaka IV"], [16, "Amaka V"] ]], [302, 280, "Thaldene", "Empire 616", [ [8, "Thaldene I"], [11, "Thaldene II"], [13, "Thaldene III"], [16, "Thaldene IV"], [13, "Thaldene V"], [4, "Thaldene VI"] ]], [565, 356, "Narvos", "Empire 258", [ [13, "Narvos I"], [12, "Narvos II"], [9, "Narvos III"], [3, "Narvos IV"], [17, "Narvos V"], [7, "Narvos VI"] ]], [991, 526, "Laomethyone", "Empire 258", [ [1, "Laomethyone I"], [1, "Laomethyone II"], [7, "Laomethyone III"] ]], [1078, 105, "Beron", "none", [ [8, "Beron I"], [6, "Beron II"], [4, "Beron III"], [6, "Beron IV"], [2, "Beron V"] ]], [1078, 913, "Harpo", "none", [ [1, "Harpo I"], [12, "Harpo II"], [8, "Harpo III"] ]], [455, 49, "Euportia", "none", [ [14, "Euportia I"], [1, "Euportia II"], [12, "Euportia III"], [9, "Euportia IV"], [4, "Euportia V"] ]], [1258, 924, "Albioriel", "none", [ [6, "Albioriel I"], [6, "Albioriel II"], [4, "Albioriel III"], [3, "Albioriel IV"] ]], [1837, 970, "Galas", "none", [ [6, "Galas I"], [5, "Galas II"], [12, "Galas III"], [2, "Galas IV"] ]], [787, 916, "Sycordelike", "none", [ [15, "Sycordelike I"], [17, "Sycordelike II"], [3, "Sycordelike III"] ]], [1103, 571, "Skatheus", "none", [ [14, "Skatheus I"], [9, "Skatheus II"], [3, "Skatheus III"] ]], [1420, 864, "Ortia", "none", [ [9, "Ortia I"], [4, "Ortia II"], [5, "Ortia III"], [9, "Ortia IV"] ]], [558, 718, "Mimaltheus", "none", [ [8, "Mimaltheus I"], [14, "Mimaltheus II"], [1, "Mimaltheus III"], [11, "Mimaltheus IV"], [17, "Mimaltheus V"] ]], [683, 410, "Neptungr", "Empire 258", [ [9, "Neptungr I"], [4, "Neptungr II"], [15, "Neptungr III"] ]], [774, 639, "Siarnsaxa", "Empire 258", [ [2, "Siarnsaxa I"], [4, "Siarnsaxa II"], [4, "Siarnsaxa III"], [5, "Siarnsaxa IV"], [10, "Siarnsaxa V"], [12, "Siarnsaxa VI"] ]], [1039, 232, "Galassa", "none", [ [8, "Galassa I"], [9, "Galassa II"], [4, "Galassa III"], [7, "Galassa IV"], [10, "Galassa V"], [14, "Galassa VI"] ]], [387, 687, "Methelxinoe", "none", [ [12, "Methelxinoe I"], [17, "Methelxinoe II"], [12, "Methelxinoe III"], [17, "Methelxinoe IV"] ]], [586, 351, "Jarnaq", "Empire 258", [ [17, "Jarnaq I"], [14, "Jarnaq II"], [17, "Jarnaq III"], [15, "Jarnaq IV"] ]], [547, 561, "Tarvi", "Empire 258", [ [10, "Tarvi I"], [3, "Tarvi II"], [14, "Tarvi III"], [12, "Tarvi IV"], [16, "Tarvi V"], [6, "Tarvi VI"] ]], [1539, 926, "Namalthebe", "none", [ [11, "Namalthebe I"], [15, "Namalthebe II"], [16, "Namalthebe III"], [16, "Namalthebe IV"], [8, "Namalthebe V"] ]], [281, 152, "Laome", "none", [ [13, "Laome I"], [4, "Laome II"], [16, "Laome III"], [5, "Laome IV"], [9, "Laome V"], [14, "Laome VI"] ]], [1451, 828, "Desdemone", "none", [ [9, "Desdemone I"], [7, "Desdemone II"], [5, "Desdemone III"], [8, "Desdemone IV"] ]], [656, 371, "Panda", "Empire 258", [ [1, "Panda I"], [12, "Panda II"], [5, "Panda III"] ]], [1218, 440, "Haldene", "none", [ [11, "Haldene I"], [14, "Haldene II"], [4, "Haldene III"], [2, "Haldene IV"], [15, "Haldene V"] ]], [858, 445, "Bebhione", "Empire 258", [ [4, "Bebhione I"], [9, "Bebhione II"], [10, "Bebhione III"], [5, "Bebhione IV"], [10, "Bebhione V"], [11, "Bebhione VI"] ]], [165, 161, "Erissa", "Empire 616", [ [7, "Erissa I"], [10, "Erissa II"], [2, "Erissa III"], [15, "Erissa IV"], [7, "Erissa V"], [4, "Erissa VI"] ]], [579, 359, "Orthone", "Empire 258", [ [13, "Orthone I"], [6, "Orthone II"], [14, "Orthone III"], [16, "Orthone IV"], [17, "Orthone V"] ]], [122, 383, "Methyone", "none", [ [17, "Methyone I"], [1, "Methyone II"], [7, "Methyone III"] ]], [62, 613, "Palyke", "Empire 616", [ [6, "Palyke I"], [17, "Palyke II"], [14, "Palyke III"] ]], [1134, 432, "Promethys", "none", [ [16, "Promethys I"], [14, "Promethys II"], [11, "Promethys III"] ]], [1618, 790, "Helxinope", "none", [ [14, "Helxinope I"], [17, "Helxinope II"], [6, "Helxinope III"] ]], [833, 123, "Cordelinda", "Empire 258", [ [3, "Cordelinda I"], [9, "Cordelinda II"], [4, "Cordelinda III"], [5, "Cordelinda IV"], [8, "Cordelinda V"], [16, "Cordelinda VI"] ]], [1552, 859, "Biancisco", "none", [ [2, "Biancisco I"], [2, "Biancisco II"], [4, "Biancisco III"], [7, "Biancisco IV"], [2, "Biancisco V"] ]], [1661, 190, "Lysithelike", "none", [ [2, "Lysithelike I"], [14, "Lysithelike II"], [10, "Lysithelike III"] ]], [1307, 1032, "Paalinda", "none", [ [15, "Paalinda I"], [2, "Paalinda II"], [4, "Paalinda III"], [16, "Paalinda IV"] ]], [981, 966, "Euporiel", "none", [ [8, "Euporiel I"], [13, "Euporiel II"], [16, "Euporiel III"], [6, "Euporiel IV"], [8, "Euporiel V"] ]], [1556, 759, "Ophelestla", "none", [ [7, "Ophelestla I"], [4, "Ophelestla II"], [11, "Ophelestla III"], [12, "Ophelestla IV"], [12, "Ophelestla V"], [4, "Ophelestla VI"] ]], [1181, 136, "Nereip", "none", [ [17, "Nereip I"], [8, "Nereip II"], [17, "Nereip III"] ]], [740, 382, "Cupiter", "none", [ [14, "Cupiter I"], [8, "Cupiter II"], [3, "Cupiter III"], [11, "Cupiter IV"], [14, "Cupiter V"] ]], [1683, 861, "Umbrie", "none", [ [5, "Umbrie I"], [14, "Umbrie II"], [17, "Umbrie III"], [12, "Umbrie IV"], [2, "Umbrie V"] ]], [1458, 322, "Albiorie", "none", [ [8, "Albiorie I"], [17, "Albiorie II"], [9, "Albiorie III"] ]], [1093, 654, "Karis", "none", [ [11, "Karis I"], [11, "Karis II"], [14, "Karis III"] ]] ], "ships": [ [1562, 859, 6, 114.06664196339, "Ship 1", "Empire 616", "Biancisco"], [1671, 190, 7, 300.43161289077, "Ship 2", "Empire 616", "Cupiter"] ], "civilizations": [ ["Empire 616", { "description": "Three councils of delegates elected by the citizens, often according to different electoral districts.", "type": "Tricameral Republic" }, ["Species", "Insectoid", "Greebly, scuttle-ish", "Barren"] ], ["Empire 435", { "description": "The Constabulary creates and enforces the law, possibly without trials.", "type": "Police State" }, ["Species", "Insectoid", "Greebly, scuttle-ish", "Barren"] ], ["Empire 258", { "description": "One council of elected delegates and another of hereditary nobles.", "type": "Bicameral Parliament" }, ["Species", "Insectoid", "Greebly, scuttle-ish", "Barren"] ] ] |
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