Jump to content
  • Advertisement
Sign in to follow this  
Virii

RPG Character Saving

This topic is 4631 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!