Making Save Files

Started by
4 comments, last by EndersGames 13 years, 3 months ago
Alright, i know theres a reason "file" is in the name. you've gotta use c++'s 'write to files' commands, but my question is:

what exactly do you write to a save file?

Say youre making an RPG. To the file you would just write the Stats, Last Position, Name, Items in Inventory, Skills, Etc., all in a specific order? Then when the file is loaded next, just set the game up using what it read from the file?

tutorial link please?
::Get your paw on::
Advertisement

Alright, i know theres a reason "file" is in the name. you've gotta use c++'s 'write to files' commands, but my question is:

what exactly do you write to a save file?

Say youre making an RPG. To the file you would just write the Stats, Last Position, Name, Items in Inventory, Skills, Etc., all in a specific order? Then when the file is loaded next, just set the game up using what it read from the file?

tutorial link please?


There's no one right way to do it. Just save any data you need, however you want to, and read it back in however you want to. It doesn't even necessarily have to be in a specific order, and it doesn't need to be written to a file either - you can save it as a database entry, send data to a remote server, or whatever. Typically, though, you do save it to a file, and what you described sounds fine to me.

For a game with lots of data to save, the save file format may need to be optimized for space, so that the saves don't fill up the users hard drive. To do that, you have to make sure that you're saving only what you need, and that the data you are saving, is saved in an efficient way. The simplest step in that direction is just to save your data in a binary format, rather than a plain text one.

For the most part, though, especially for a small game, I think it's best to just stick with a plain text format. That way, if you find a problem with reading/writing a save, you can just open up the save file in a text editor and figure out what's wrong with it (or even fix it). To make reading & writing easier, you can use libraries that handle common plain text formats like XML or JSON.
Alright, thanks extralong.

Also, glance at this code s'il vous plais:




void MyFunc();
void MyOtherFunc();

void main()
{
MyFunc();
system("pause");
}

void MyFunc()
{
if(condition == true) goto here;
else{}
}

void MyOtherFunc()
{
here:
//cout << "Success." << endl;
}



I would like for the code to jump from one function to another (i.e. 'goto') if a condition returns true/false. But, this does not work. Is there another way to do this, so I dont have the program just nesting MyOtherFunc within MyFunc within Main (which could result in a nested loop in the actual program i'm making.)

::Get your paw on::

if( condition ) return MyOtherFunc();
// or...
if( condition ) { MyOtherFunc(); return; }
// or...
if( condition ) MyOtherFunc();
else { ... }
// or... ad nauseum


That's not a "nested loop," by the way. Nested loops are:


for( int a=0;...)
{
for( int b=0;...)
{
}
}


If you're worried about calling one function from another, that's quite common. Is that your concern?

How about:

if( MyFunc() ) MyOtherFunc();
//...
bool MyFunc
{
if( condition ) return true;
// do other stuff
return false;
}

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.





void MyFunc();
void MyOtherFunc();

void main()
{
MyFunc();
system("pause");
}

void MyFunc()
{
if(condition == true) goto here;
else{}
}

void MyOtherFunc()
{
here:
//cout << "Success." << endl;
}




Avoid the use of goto in your code.

Replace goto here; with MyOtherFunc();

If your problem is that you want it to jump to a specific point in that function, all you need is an if statement and to give MyOtherFunc() an argument. Since you're obviously relatively new to programming and all your functions are of type void, I will assume you're not aware you can pass arguments to functions.

Also, if you're really worried about functions being called from within functions, are you saying you're worried about the call stack overflowing? I wouldn't worry about that for something like this. Maybe for a factorial function that would be an issue, but in this instance, there is no need to restructure your code to avoid it. It would be very simple to restructure your code to do so though.

------------

Save files can contain anything you want in any order.

All that you need to be sure of is that you store it in a consistent manner so that you can easily parse the data when loading a game state from the file. For simplicity's sake you can use nice big obvious delimiters.

Example: player has 10 strength, 5 current health, 15 maximum health, 30 gold, x_coord 4, y_coord 7, level=2

Save file might look like this:

10*5*15*30*4*7*2

Or

10
5
15
30
4
7
2

Or forego saving data to files and use a database like MySQL.
Oooh I like the database option. haven't thought of that and it would work great for android devices as they are able to access the internet already. :) problem solved for me yeah

This topic is closed to new replies.

Advertisement