A Better Way to Save Games

For a while now I’ve been trying to find the best way to save games in UDK. And I don’t mean some cheap Kismet hack or a workaround that dumps floats in random places in your hard drive – I mean a real save game system. So far I’ve tried config files and a C++ dll-bind, both of which have their pros and cons.

Config files are easy to implement, and what’s more they’re even supported by UnrealScript via the SaveConfig command. However, as most everyone who’s messed with .ini files knows, they’re horribly insecure. Anyone can open up the .ini, change what they want, and save/quit. We don’t want that, now do we?

On the other hand, there’s a dll-bind. For those of you who don’t know what that is, it’s basically code written in C/C++ or another language built to go through the C compiler that handles save games for us. Now, while it IS secure and very fast, it’s also a pain to write. And since UDK’s installer doesn’t like third-party patches like this, you will NEVER get your dll-bind through to a finished package.

Enter BasicSaveObject(). It’s a function in Engine.uc that’s been there for who knows how long, but has only since popped up on the Mobile page at UDN as a way to save game states on the iOS. From what I can gather in my research, not many people know about it, which is quite sad considering how powerful this function is.

So how do you use it? First, create a new class that extends Object like so:

class MySave extends Object;
var int NumKills, NumDeaths;

and so on. Only basic variables like ints, floats, strings, and names will be saved (though that’s usually enough for a save-state, isn’t it?).

Then in your PlayerController, you can create a function:

exec function SaveGame() {
 local MySave saver;
 saver = new class'MySave';
 saver.NumKills = 5;
 saver.NumDeath = 2;
 class'Engine'.static.BasicSaveObject(saver, "SaveGame.bin", true, 1);
}

It should be noted that the BasicSaveObject function can be called from any class, however I prefer this method because now all I have to do is call ConsoleCommand(“SaveGame”); You can name the output file anything you like, and what’s more you can specify any directory you like. The input function BasicLoadObject() works in the same way…just be sure to have a MySave class initialized first.

If only I knew about this function earlier…

Advertisements

11 thoughts on “A Better Way to Save Games

  1. Pingback: Online Service Ideas | WillyG Productions

  2. Pingback: Fully Saved Levels in UDK | WillyG Productions

  3. Hi.

    When the game initialize the mySave class the next time when I to play my game it will automatically load the ​​saved values?

  4. Hi Willy,

    Thanks for this short tutorial. But i have a doubt regarding save location. What if i want to save the “SaveGame.bin” to another directory (For example, My Documents folder). How can i get My Documents path using UnrealScript?

  5. hey

    what exactly does this saves. is it the just the variables in the the MySave.uc. and what if i want to save the whole world(every actor).

    thanks.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s