Help with saving/loading system.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 29
Reputation: Shinedevil is an unknown quantity at this point 
Solved Threads: 0
Shinedevil Shinedevil is offline Offline
Light Poster

Help with saving/loading system.

 
0
  #1
Dec 6th, 2008
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;
  1. {
  2. loadgame:
  3. savefile1.open ("savefile1.txt", ios::out | ios::app);
  4. savefile1.close();
  5. goto maptest;
  6. }
  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Help with saving/loading system.

 
0
  #2
Dec 6th, 2008
you will need to do some form of string parsing. Firstly you need to read from the file you can use the following method
  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,
  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.
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 29
Reputation: Shinedevil is an unknown quantity at this point 
Solved Threads: 0
Shinedevil Shinedevil is offline Offline
Light Poster

Re: Help with saving/loading system.

 
0
  #3
Dec 9th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 29
Reputation: Shinedevil is an unknown quantity at this point 
Solved Threads: 0
Shinedevil Shinedevil is offline Offline
Light Poster

Re: Help with saving/loading system.

 
0
  #4
Dec 17th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 90
Reputation: unbeatable0 is an unknown quantity at this point 
Solved Threads: 12
unbeatable0 unbeatable0 is offline Offline
Junior Poster in Training

Re: Help with saving/loading system.

 
0
  #5
Dec 17th, 2008
http://www.daniweb.com/forums/post31214.html
This link should help you. It teached me well how to use ifstream objects.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Help with saving/loading system.

 
0
  #6
Dec 17th, 2008
i Should note it should be 0..1..2 on my array in the post very accidental mistake sorry

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Help with saving/loading system.

 
0
  #7
Dec 17th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC