This process of “picking a character” is really part of a larger question about something called “gamestate” or “client gamestate”. How do you track what is going on in the game at any given time, including offscreen status of objects and environment, previous choices, etc.
Different people recommend various approaches.
Generally, you need to be familiar with the following concepts, for starters:
serialization
deserialization
files (reading and writing)
databases
global variables
lua tables
Serialization is the ability to turn data into a format that you can write to a file or to a database or a single variable.
Deserialization is the ability to go the other way and convert a format into a complex object.
I recommend Json via the require(“json”) wherein you get json.encode and json.decode
You have access to temporaryDirectory files and more via various Corona APIs.
Some libraries like (I’ve seen) Preferences.lua writes serialized values for you and pulls out deserialized data, but you can do it yourself however you like. Files/DB/Hardcoded Globals have different tradeoffs. I rely on DB+Globals
Corona gives you access to the sqlite db included on both iOS and Android. I believe Nook and Kindle have one as well, but I haven’t tested.
Lastly you should understand that Lua Tables { … } is the data structure of choice. It is for arrays and for hashmaps and for objects. So you are usually creating tables then serializing and deserializing them.
I like to use the storyboard module in main.lua like so:
s = require(“storyboard”)
then I create a global table:
-- Now I can put whatever I want in here. s.g = { character = nil, otherdata = nil, -- I have a list of levels I want to keep track of! listoflevels = { [1] = "Start", [2] = "Candyland", [3] = "Chutes and Ladders", [4] = "Mouse Trap", }, }
s.g.character = “boy”
Now I can reference s.g.character from anywhere.
Global variables are how I maintain client gamestate. Sometimes I write json.encoded data to the client Database and pull it out when the app starts up because changed data in a global variable does not persist/write automatically when the app shuts down.