Lastly, to complete the introduction of the process, we’ll need a Civilization object to cram our species into. We’ll keep it basic for now, with name, type of government and species type. The Civilization object will be pretty basic but we can definitely finagle some more attributes into it down the road (and boy howdy do I plan to!)
So we’ll begin the awesome goodness of creating a bunch of objects and making sense of the whole thing. So first, we’ll instantiate the Civilization object and fill it with some info on the Species and government type and name.
Let’s begin. Download the file. Unzip it to wherever you want and open it on up. It should look like the below:
Don’t panic, one of those files we didn’t discuss, but we will. Matter of fact, let’s discuss it right now. The one named ‘government_types.json’ is a JSON list of various government types I grabbed as an example from David A. Rozansky’s site since he had a convenient list of them. Of course, you’re welcome to add to the list, remove, etc. but there’s a function you can see in Civilization.lua called Select_Random_Government. What that function does is go out to that file every time we create a new Civilization and assign a random government type to it. Random, to be sure, but we can always add more functionality down the road.
Open up the Civilization.lua file from under classes in your editor of choice.
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 |
Civilization = class("Civilization") local json = require( "json" ) function Select_Random_Government() local result = {} -- Path for the file to read local path = system.pathForFile( "/misclua/government_types.json", system.ResourceDirectory ) -- Open the file handle local file, errorString = io.open( path, "r" ) if not file then -- Error occurred; output the cause print( "File error: " .. errorString ) else -- Read data from file local contents = file:read( "*a" ) -- Output the file contents -- print( "Contents of " .. path .. "\n" .. contents ) -- Close the file handle io.close( file ) local government_type = json.decode(contents) result = government_type[RAND(1,112)] print("Government ",government_type[1].type) end file = nil return result end function Civilization:init(name) self.name = name or "Empire of Nabzorg" self.government = Select_Random_Government() self.species = Species:new() end --= Return Factory return Civilization |
Now mind you this is in Corona and not pure Lua. Pure Lua, we’d use the lfs and some JSON module I’m sure. I’ll get a pure Lua version uploaded soon, but in the meantime, you can ignore the Select_Random_Government function for now, and we’ll concentrate on the OOP aspect of the code.
So far, we only deal with two objects; the Species and Civilization objects. In main.lua we create the Civilization object first, which then in turn creates a Species and keeps track of it in itself:
1 2 3 4 5 6 7 8 9 10 11 |
Civilization = class("Civilization") json = require( "json" ) math.randomseed( os.time() ) class = require("misclua.30log-global") require("classes.Civilization") require("classes.Species") local myCivilization = Civilization:new() print("Civilization name: "..myCivilization.name) print("Civilization government: "..myCivilization.government.type..": "..myCivilization.government.description) print("Civilization species name: "..myCivilization.species.name) |
Notice on line 8, THAT’S where we instantiate our Civilization object. We assign to the variable ‘myCivilization’ the Civilization object we’re instantiating. Then below, you can see we can use dot notation to start querying the data from that object. For now, it’s not super exciting, we have the following output from my test run:
1 2 3 |
Civilization name: Empire of Nabzorg Civilization government: Oligarchy: A few powerful leaders sharing power equally. Civilization species name: Species |
But now that the foundation is laid, we can start gussying up these objects!
Here’s the code, simple as it is, if you want to take a look at it!
artificial galaxies 1