Creating and Saving New Forms of Data – Save File

Last Thursday we discussed how to create global variables of any kind of data, and why that might be desirable. We created a variable called $sophies_choice which stored what our choice was for a particular quest. We could then call $sophies_choice == :kill_mage in a conditional branch which would return yes if we decided to kill the mage and no if we didn’t.

Why did we do that exercise, why not just store our choice in a variable? Sometimes, you have games that have a shit-ton (a unit of measurement in the metric system) of data to keep track of, and keeping that in a variable you have to remember is gross. Just like our global variable can store a symbol, it can store a hash keying symbols to data. So for a super complex game, we can do something like this at the beginning of our game:

$quest_decisions = {}

And then later, when we make a decision, we do:

$quest_decisions[:sophies_choice] = :kill_mage

Now all of our quest decisions are stored in a single global variable! Nifty!

*NB: If you don’t understand the hash structure we just went over, I strongly recommend Code Academy’s free online tutorials on Ruby. You’ll learn all about Ruby syntax. Basically what we’ve done is create a “Hash” called $quest_decisions. Hashes are buckets. You can put pairs of things into a hash bucket – the pair is represented by a “key” and a “value.” They allow you to ask the question “hey bucket, here’s a key, what value is associate with that key?” And it will report back. Here, :sophies_choice is a key, and :kill_mage is a value. So when we say $quest_decisions[:sophies_choice], we can get whatever choice was made.

So here’s the main issue we confront. When we save the game and reload it, $quest_decisions doesn’t exist. It got created in our game, and existed so long as we were still playing. But once we exited, we run all of the scripts that get called at the beginning of our game. Including $quest_decisions = {}. Well, crap, that just emptied our whole hash. What we want to do is save that information in our game save file. Let’s do it!

BUT BEFORE WE DO, AN EXTREME NOTE OF CAUTION. From here on out, we’re modifying the Data Manager. This isn’t a script that is handling some camera setting or making a flashy graphic. We’re playing with something very close to the guts of RPG Maker. If we do this, and we do something wrong, a variety of things can happen, including:

  • Deleting save files
  • Prohibiting loading files
  • Game freezing
  • Computer exploding
  • Panic attacks
  • Massive embarrassment

Moreover, when we mess up here, RPG Maker doesn’t give us a handy reason why by default. If you’ve seen a script bomb out before, you’re probably familiar with a little window that shows up telling you where shit is broken. Where we’re going, all you’ll get is a friendly “ERR-ERR” buzzer, and probably a game freeze.

Create It

The first thing we want is the function which creates game objects. Let’s take a look at what it does. Scroll up to DataManager and look (don’t touch) at create_game_objects.


This function gets called when we hit New Game. It makes a new player, map, set of variables, all that stuff. It creates the stuff we need for our game. We want to add something here. We want to add $quest_decisions = {} to make our new hash. So let’s add it!


Save It

Nice! Our hash exists! Now, let’s save it. Scroll down to make_save_contents, and look around. This is where we build up the contents for a save file, and then return it. You can probably tell by the syntax here what we need to do, but just to clarify. Contents is a Hash! It has key-value pairs. The key is a symbol we define, whatever you’d like. We’ll probably go with :quests. The value is our global variable, our $quest_decisions! So let’s add it:


NB: DO NOT DELETE THE LAST LINE WHICH JUST SAYS “CONTENTS.” It does something called “return” the contents you’ve built up. That’s all it takes, every time we save, your Hash is saved. So if we’ve added a key-value to the Hash (like $quest_decisions[:sophies_choice] => :bribe_mage), it will be saved!

Load It

Finally, scroll down just a touch and you’ll see extract_save_contents. All we need to do is put our line from above in reverse, just like how all of the other ones look.


Now when we load, we look at the save file, find something called “:quests” and put that into our $quest_decisions global variable. And we’re done! We can do this for all kinds of data, we can save all kinds of things into the save file.

….But that’s where this stops. Saving into the save file. But there are some things we might want to save more broadly. For example, I want to save the fact that you beat the game, so I can check and open up New Game+. That’s a concept which exists outside the save file, at the title screen. And we’re going to have to do something very different to save that information. Next week!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s