The more you get into developing a complex game, the more you’ll find you wish you could save data beyond switches and variables. Sure, RPG Maker itself saves all kinds of things – your equipment, your experience, your location, your gold, hell even your steps! But let’s say we have some data that we don’t really want to express as a boolean (switch, on/off) or a variable (integer). And we still want to save it. How might we do that? Today we’re going to set-up the problem and cover some basics, and Monday we’ll talk saving.
Here’s an example. Sophie the Soldier comes across a mage. He tells her that ahead is an evil monster, and that she should not continue forward for fear of her life. He has erected a large magical barrier, to prevent access. Sophie is given a few options:
- Kill the mage, in the hopes that his life is necessary to sustain the barrier
- Bribe the mage to lower the barrier
- Ask a different mage to dispel the barrier
- Ignore this all and just continue on the main quest
We see this style of RPG in a lot of Bioware games, where one path doesn’t just fork into yes/no, but has several possible solutions. To make it meaningful, we want to save what the player has elected to do, and refer back to that decision later. (For example, we might want to know if the mage is dead much further in the game, and if he’s not, have him enter a cutscene or something). RPG Maker has two ways to handle this, generally:
- Using switches, set all relevant switches on. This would probably take four switches – one for each solution to our side quest above – and we’d set the correct one on. Fine enough, but if we have 100 side quests, we have 400 switches, and things start to get REAL complicated.
- Using variables, we can assign the choice to a single variable. But then we have a problem of “magic numbers” – the variable “MAGE SIDE QUEST SOLUTION” would just be equal to “3.” When you’re still developing your game 4 months from now, will you remember what 3 means? Cuz I won’t.
What I really want to do is store a “symbol” – it’s a little bit of text, preceded with a “:”, and it will instantly tell me what’s going on. I also don’t want to have to remember which variable or switches are involved here. If I could just conjure up something like “the player chose to kill the mage?” I’d be so happy.
In Ruby, we use “$” to indicate that something should be accessible from anywhere else. We can name the rest of it whatever we want, but we need to use $ at the front. I’m going to call mine “$sophies_choice” – because that was the joke that I set up several paragraphs ago.
We also want to store a symbol in that variable – a symbol is a piece of text with “:” in front. Why a symbol and not a string? For reasons as mysterious as they are bizarre, strings can’t be compared but symbols can be. “Test” does not equal “Test” but :test does in fact equal :test. And since we’ll want to run some comparisons later, we’ll use symbols. So,
Cool! Now later, we can do fun things by simply asking about Sophies’ Choice in a manner far more organized than either of RPG Maker’s usual approaches.
This is a contrived example, but the point is, there are structures we want that can be different than straight switches and variables.
RPG Maker automatically saves switches and variables into a save file. It doesn’t save custom stuff you’ve made. So if you make your choice, then save, then exit, then reload, $sophies_choice will be nill – nothing will be stored there. We need to tell RPG Maker to keep that data in the save file as well – stay tuned for Monday!