943,648 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 620
  • C++ RSS
Dec 6th, 2008
0

Help with saving/loading system.

Expand Post »
I have no idea how to make somthing like this, and i'm kind of new with file I/O.
How would you start somthing like this?

O.k. here is what I have to save,
I have roughly 56+ variables that need to be counted in this.
But what I am having trouble with is when you write in a word doc it can't save the actual vairable values.
I need to save like if somthing is 1 or 0. Like if you equiped a weapon or not, or if a quest if active. And then acess that when you load the game back up, but I can't seem to get the variable to show up when you access the load screen so the game locks up when you access portions of the map that "are not acessed yet." Which should have been saved during the previous play.
(it's also annoying when you have to start over agian when you start up the game)
For example,
int x;
x = x + 1;
I need to save the value of x, being that it is a coordinate to the map, and load it from the file agian exactly as it saved it.

Anybody know how to do this, or at least give me a brief walkthrough with file I/O and or how to access and save to files?

Here is an example of two code snippets;
C++ Syntax (Toggle Plain Text)
  1. {
  2. loadgame:
  3. savefile1.open ("savefile1.txt", ios::out | ios::app);
  4. savefile1.close();
  5. goto maptest;
  6. }
C++ Syntax (Toggle Plain Text)
  1. {
  2. save:
  3. savefile1.open ("savefile1.txt");
  4. savefile1 <<"x ="<<x<<"\n"<<"y ="<<y<<"\n";
  5. savefile1.close();
  6. goto jumpto;
  7. }
could someone tell me how to access those x and y variables?


Yes this was orginally posted in game dev, but i would think i would have more success with an answer here.
Reputation Points: 8
Solved Threads: 4
Junior Poster in Training
Shinedevil is offline Offline
71 posts
since Nov 2008
Dec 6th, 2008
0

Re: Help with saving/loading system.

you will need to do some form of string parsing. Firstly you need to read from the file you can use the following method
C++ Syntax (Toggle Plain Text)
  1. ifstream f("savefile1.txt", ios::in, ios::binary);
  2. string line;
  3.  
  4. getline(f, line);

once you have something to work with then you can begin parsing the string.
string streams useful here,
C++ Syntax (Toggle Plain Text)
  1. #include <sstream>
  2. ...
  3. ostringstream streams;
  4. streams.str(line);
  5. string words[3];
  6. streams >> words[1] >> words[2] >> words[3];
That way word[1] would be "y" word[2] would be "=" and word[3] the variable.

Another method would be to read the file word by word using the >> operator.

Chris
Last edited by Freaky_Chris; Dec 6th, 2008 at 2:14 pm.
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Dec 9th, 2008
0

Re: Help with saving/loading system.

Sorry this did not help. Your thing with >> doesn't work at all. Could you please explain how and what it does exactly so I can at least understand how it works, and figure out how to write it to fit my code.

Or could someone tell me how to load up a file, and change the variables in a program to the numbers of the file, scince that's exactly what I am trying to do.
Reputation Points: 8
Solved Threads: 4
Junior Poster in Training
Shinedevil is offline Offline
71 posts
since Nov 2008
Dec 17th, 2008
0

Re: Help with saving/loading system.

Ok, please help me.
Here is another recap of what i need to do.

I need to read off a file a number.

I need to change a variable to that number that is stored on the file.
Reputation Points: 8
Solved Threads: 4
Junior Poster in Training
Shinedevil is offline Offline
71 posts
since Nov 2008
Dec 17th, 2008
0

Re: Help with saving/loading system.

http://www.daniweb.com/forums/post31214.html
This link should help you. It teached me well how to use ifstream objects.
Reputation Points: 42
Solved Threads: 13
Junior Poster in Training
unbeatable0 is offline Offline
90 posts
since Sep 2008
Dec 17th, 2008
0

Re: Help with saving/loading system.

i Should note it should be 0..1..2 on my array in the post very accidental mistake sorry

Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Dec 17th, 2008
0

Re: Help with saving/loading system.

You should try to make the question as limited as possible.

"I would like to write a number to a file, and then later retrieve that number from a file." is much clearer than something about a game and a quest, etc.

Try to get a sample application working (using unbeatables post as a starting point) before you try to integrate it into your game. You sample program should do exactly what you're trying to do and nothing else (ie. write a number to a file, and then read it in again.)

If you get that to work on its own, you should have no problem integrating it into the game.

Post back with results from the sample program.

Dave
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: reading from serial port way too slow (C++)
Next Thread in C++ Forum Timeline: open new Form in project





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC