save/load Game State


Hello everyone,
I have a question that I don't seem to find the answer to.
I am programming a text game (console) in c++. I already have a save and load function. What I want to know is this: Is it possible to save the "game state" in a way that when it is loaded, it is already inside a specific function?


For example:

void level1()
{
dungeon1();
}

void dungeon1()
{
cave1();
}

void cave1()
{
//something
}

If I save inside the cave1 function, when loaded i want the player to be inside the cave1 function and not the begining of Level1 function.

Thanks in advance!
Not in the way you are describing. I'd just make a variable in the save data that tells you the player's location and use that to figure out where to go.
Can you please tell me how would you use it exactly? I have tried many things and now I'm in mind-block... Would something like 'goto' help??
Would something like 'goto' help??

That's cute.

Essentially, if I understand correctly, Zhuge was suggesting that you write all your important information (like, player position, for example) to a file(your savegame file), where these pieces of information are presented in a certain syntax - a syntax which, when it's time to load a gamesave, will allow your game to parse (load) the previously saved information back into memory, and then do things with it (like positioning the player).

How you decide to structure your gamesaves is entirely up to you. The simplest way I can think of right now is to place unique separator characters between different strings of information, something like :

PLAYER foo|100|53|etc
I have the save/load functions and it's working.
For example a part from my LoadGame.cpp looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
bool LoadGame(PLAYER* player)
{
	colorStruct playColors;     //Holds color info
	fontStruct playFonts;       //Holds Font info
	GameStatus StateOfGame;     //Holds state of game per level
	playerstruct playerStats;   //Holds Player info
	weaponstruct weaponStats;   //holds player's weapons info
	armorstruct armorStats;     //holds player's armors info
	
		
	// Reading from it
	ifstream input_file("sav.data", ios::binary);
	if (input_file)
	{
		input_file.read((char*)&playColors, sizeof(playColors));	//Load colors
		input_file.read((char*)&playFonts, sizeof(playFonts));		//Load Fonts
		input_file.read((char*)&StateOfGame, sizeof(StateOfGame));	//Load Player stats
		input_file.read((char*)&playerStats, sizeof(playerStats));	//Load Player stats
		input_file.read((char*)&weaponStats, sizeof(weaponStats));      //Load Weapon stats
		input_file.read((char*)&armorStats, sizeof(armorStats));	//Load Armor stats
		input_file.close();
	
		//Load Colors First
		LoadColors(playColors);

		//Load Fonts
		LoadFonts(playFonts);

		//Load Game State 
		LoadGameState(StateOfGame);

		//Load everything to player
		LoadPlayer(player,playerStats,weaponStats,armorStats);

		return true;
	}
	else
		return false;

}


Now the load state holds the function of each level (level1, level2....).
What I want is to somehow hold (and then go to) the last function inside the level, that the player was in when he saved the game.
At the moment it reminds me of Diablo because when I die and load a saved game, I have the experience, the items, everything but I start at the begining of each level.
closed account (Dy7SLyTq)
you could make like a function pointer and just write the name of the function to file
Thanks, I'll look into this and get back with what I find. :)
Well, I am trying to use 'pointer function' but now I am confused. I read some tutorials but they didn't help me much. I believe it's too advanced for me... :P
Could someone maybe help me on how to use it? With an example or something, how to store the address of the function that the pointer function is pointing? And then of course how to call it back.

EDIT: I did it. The app compiles with no problem and saves the function pointer in the save file. But now I see it's useless because when I re-run the app and try to load, the saved address from the function pointer is different because the OS resuffles memory in the meantime. Thus, the function pointer on load, points to garbage...
Someone that can help me solve this, please?
Last edited on
Here's a hacky solution:

Save the level number in the game save (for example: 1).
Then, when you're loading the level info, use the level number as an index for an array of function pointers(?)

Please, gods of all that are holy, don't kill me
Topic archived. No new replies allowed.