RPG Character Saving

Started by
11 comments, last by Enigma 18 years, 5 months ago
So I'm creating a text RPG, and I want to have multiple characters that are able to be saved in the games memory, or to a text file or something. My questio nis, what is the best way to save it all and what is the best way to read the information back into the game to continue their adventure? Right now my plans are to have a text file hold everything, from what items they are holding, to HP and level plus character name. My only problem is figuring out how to read all that into the game and properly assign it all to the right variables. Also, how can I create multiple characters and save them all to the same file and then later.. read back JUST the characters name and have the user select it, and then read all the data to the right variables. Does all this make sense?
Advertisement
I just used a simple text file with a {descriptor}={value} pair for almost everything:

i.e.
hp=40
maxhp=60
name=Bob

and then just used some simple parsing crap to break each line into tokens for reading back into my program. I wrote an accessor library for these files, and had a getInt(descriptor)/ getBool(descriptor)/ getString(descriptor) method that would step through the file, find that descriptor, parse its associated value and return it.

You could probably use TinyXML or something, it would be a little bit easier.
Could you reword that so that someone who is really new to C++/programming could understand it a little better?

Did you create a text file with the information with something=value, and then how did you parse it all through so that it matches up with te different character names?

Also, someone told me fread/fwrite/fopen/fclose are bad to use, is this true and why? If it's true, what is the better way of read/writing things to a file?
Firstly, the methods for saving and reloading depend on what language you are using.

As to the rest of your question, saving it all as one text file would probably work fine. I would suggest the first line of the file holding the number of characters. Then you can loop through as many characters there are, each time getting all the information you need. With only reading the names, and nothing else, you could have the first line of each new character be the name, so for each character there would be:
Name
HP
Level
Armor
etc...
So depending on how many things you had for each character, you could skip that many lines, so you would only get the names (first line for each character).

This might be confusing, as I'm kind of in a rush, but your file could look something like this:
3 (Number of characters)
NameChar1
HPChar1
LevelChar1
NameChar2
HPChar2
LevelChar2
NameChar3
HPChar3
LevelChar3

If you tell me what language you are using, I can show you how to load all the information.

Hope this helps.
I'm using C++.

Heres a pastebin of all coding I have done so far. Feel free to also critic me on my coding.

http://pastebin.com/425268
Quote:Original post by Virii
Could you reword that so that someone who is really new to C++/programming could understand it a little better?

Did you create a text file with the information with something=value, and then how did you parse it all through so that it matches up with te different character names?

Also, someone told me fread/fwrite/fopen/fclose are bad to use, is this true and why? If it's true, what is the better way of read/writing things to a file?


I created a text file with the pairs in it when I was saving my stuff to disk, which isn't exactly perfect but it works well.

To parse, I use ifstream::getline and strtok (although you shouldn't use strtok; it's one of those C functions that isn't necessarily safe. Someone else can probably prescribe a better solution for that).

Effectively, I go through the file from the top, reading each line and tokenizing it (so I get the "descriptor" before the "=") and then check to see if that's the one I'm looking for. If so, I take what's left on the line (after the =) and turn that into an integer/string/etc.

The C file functions aren't too bad, but you should probably use the C++ functions in fstream.
Id say something like this would be perfect for saving your characters

FILE* out;out=fopen("Save.sav","wb");fwrite(&character,sizeof(character),1,out);fclose(out);
"I seek knowledge and to help those who also seek it"
If you're willing to use c++.net or an xml library for plain c++, you could store all needed data within a xml file. Just save to the xml file or load from it as necessary.
Well, if you're using C++, you can do something like this:

char Name[32];
int NumChars;
int MaxHP;
int HP;
std::ifstream CharFile("CharFile.dat", std::ios::binary);
CharFile >> NumChars; // Get number of characters
for(int i = 0; i < NumChars; i++) // Loop through all the characters and get the info
{
CharFile >> Name;
CharFile >> MaxHP;
CharFile >> HP;
}

I think that would work. It uses fstream, rather than the FILE structure, but it should be fairly easy to change to suit your needs.

Hope this helps.
As you can see, there's definately more than one way to skin a cat. My solution would be more object oriented, and from looking at your code I think the procedural way is better suited for you.

It's just a matter of what best fits you.

Personally here's what you could consider...

write functions like:

ReadInt, WriteInt, ReadFloat, WriteFloat, ReadStr, WriteStr... etc.

Then you can get the data in and out easily. All you have to worry about is the format.
--before honor is humility--

This topic is closed to new replies.

Advertisement