Save and load the game state to and from a binary file. C++
I was looking for a simple way to save and load my game state and have tried a few thing but cant get my head around it. here is what i got for saving...
FILE *file2 = fopen("Save.bin", "w");
if ( file2 != 0){
fwrite((Game*)game, sizeof(Game), 1, file2);
}
and loading
FILE *file = fopen("Save.bin", "r");
if ( file != 0){
fread(game, sizeof(Game), 1, file);
}
Game is a class that controls the whole game and contains all the values that i would need to save. T main problem that i think i am running into is that Game contains 2 vectors and a pointer to another class but i do need to save the values in these to开发者_开发问答o. what is the best way to do so?
I'm pretty sure doing a straight fwrite of your class is going to not work on at least sixty-zillion separate levels. What you need to do is save the data for your X/Y/Z position, weapons collected, ammo, current map, events triggered and so on to a file, probably by building a long INI-style string using concatenation and fwrite-ing that.
Then when the game is loaded you parse the data back from the string and rebuild your class.
You could use some of the Boost Serialization functions.
加载中,请稍侯......
精彩评论