Game Development Community

dev|Pro Game Development Curriculum

How To Save/Load Your Game State, The Easy Way

by Dante Falcone · 05/27/2008 (9:45 am) · 12 comments

Every SimGroup object has a save function, which saves all the data to a text file. So, to save out my current mission, I just execute:

MissionGroup.save("MySaveFile.save"); // Where 'MissionGroup' is the name of your mission SimGroup.

In order to find the name of your mission SimGroup, just open up the mission file and it is at the top, declared:
new SimGroup(MissionGroup)


Now, to save your custom data, the easy way is to just add it to the SimGroup. Like so:

MissionGroup.MyNewPlayerName = "Tony";
MissionGroup.DoesThisReallySave = "Yes it does";


If you are wondering how to change the player spawn point for when we load the save, you need to make a variable to store the current position at save, then call the setPosition function with that variable on the player when loading your save file.

When Saving:
MissionGroup.PlayerPosition = LocalClientConnection.getControlObject().getTranform();

When Loading (Do this from server side):
%client.player.setTransform(MissionGroup.PlayerPosition);


Full Example, bound to the F3 key:
function saveOutMission(%val)
{
	if (%val)
	{
		MissionGroup.PlayerName = "Tony";
		MissionGroup.save("./SAVEFILE.SAVE");
	}
}
moveMap.bind(keyboard , F3, saveOutMission);

And to load the save file, just load it as the mission, instead of the original mission file, and Presto!

Of course, this means any important game state data must be put in the MissionGroup, at least for saving and loading.

About the author

I am a Producer in the Video Game industry and specialize in MMO development, Character Art->Game pipelines, and general support programming.


#1
05/28/2008 (6:14 pm)
Great tip.
#2
09/23/2008 (3:27 pm)
It saves the file but when I go to reload it (unless I first close torque completely and restart) the game just hangs there and says "waiting for server", any ideas?
Thanks
#3
09/23/2008 (3:32 pm)
also after saving if I don't first don't exit torque completely and restart, when I go to load the mission file it does not display the mission name but instead displays the name of the file
#4
09/23/2008 (4:03 pm)
I just don't understand it, if I save a game and then exit to the main menu and go to load it, the console says can not find file.... but when I restart torque it finds the file fine
#5
10/03/2008 (8:55 am)
This looks promising. I'm just wondering if its easy to hack the .SAVE file.

Also, from what Jesse is saying, it sounds like it may need to recompile (?) after saving, so calling Disconnect or something similar may be needed?

I'll try it out and see what happens.

Thanks for the resource!
Tony
#6
11/10/2008 (10:34 pm)
Sorry for the very late reply, I've been away from Torque for a bit, just retuning.

Jesse - I'm sorry I didn't include loading as well in this resource. It is a little trickier than saving. To figure it out I had to trace all the steps the scripts go through when loading the default mission. So here is the way to load:

In Client/Init.cs, you will see a function for loading the default mission. So let's copy it and tweak it to fit our needs. Make this new function:
function loadMissionFromFile(%fileName)
{
	disconnect();
	Canvas.setCursor("DefaultCursor");
	createServer( "SinglePlayer", expandFilename("~/data/"@%fileName) );
	$pref::Player::Name = MissionGroup.playerName;
	
	%conn = new GameConnection(ServerConnection);
	RootGroup.add(ServerConnection);
	%conn.setConnectArgs($pref::Player::Name);
	%conn.setJoinPassword($Client::Password);
	%conn.connectLocal();
}

That new function does the same exact thing as the default load function when you start the game, but now we can feed it a file name of a mission file.

Now in default.bind.cs add this code:
function pressLoadKey(%val)
{
	if (%val)
	{
		loadMissionFromFile("SAVEFILE.SAVE");
	}
}

moveMap.bind(keyboard, O);

/*Now we can load our game from "SAVEFILE.SAVE" by pressing the 'O' key in game.*/
#7
11/10/2008 (10:40 pm)
Hey Tony,

You are right to say this Save file method is easy to hack, since the save file is plain text. The strength here is to have something quick and dirty for prototyping.

A proper 'shipable' save method should convert the save data to binary file. I'm not sure how or if you can do this purely with Torque Script. If anyone has investigated, please chime in.

Thanks,
-Dante
#8
12/22/2008 (11:18 pm)
wow, I just figured out a real simple way to save and load progressive missions using this. All I did was cheat by having the original missions 2+ renamed .foo and only the first level .mis, then inside the .mis files for levels 2+ I added the line MissionGroup.save("starter.fps/data/missions/level2.mis"); to save a copy of the mission with .mis in addition to the .foo, that way the gui at startup will only see .mis files and will look like it has saved the players missions as he/she progresses. Too cool, this has only taken me two years to figure out and was darn simple!
#9
12/22/2008 (11:52 pm)
Now I need to figure out a good way to save the player's inventory at the beginning of each level. And to load it when the level is reloaded. I have already implemented the following two resources that I am utilizing for inventory:
http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=1955
http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=4213

Anyone want to help on this?
#10
12/28/2008 (5:40 pm)
Oh yea, I implemented inventory in this system a while back. Just save the inventory to the mission save file, then when loading up a new mission you can have it assign the inventory from the save file to your player's inventory. I put "None" in slots that have nothing so that it never complains about not having an object to assign etc...

Unfortunately my code here is a mess and not working right now. I'll post a resource on Inventory when I work on that for my game (may be a few weeks).

I've also been working on implementing a binary save/load system all on engine side. I'll be posting that soon as well, although I'm forced to use STL for some file in/out functions because I couldn't find the appropriate wrapper function in torque.
#11
12/28/2008 (8:20 pm)
thanks, this is the last thing holding up my project, your resource is a great help
#12
02/22/2009 (5:11 am)
Hmm. Any way we could connect this function as a command to a GUI button? Also, is it possible you can do a Step by Step [1) do this 2) do this 3) do this, 4) etc...]? Thanks so far for your code. I'm a little bit confused, but I'll try and see what I can do so far,