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.
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.
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 :
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);
returntrue;
}
elsereturnfalse;
}
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.
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?
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(?)