Skip to main content
GameDev.net gamedev.net
🔒 Locked

Saving & Loading problems

Started by WizHarDx Oct 7, 2001 at 5:47 AM 1 replies 300+ views
Original Post
WizHarDx
WizHarDx
I made two template functions that use fstream to stream data to a binary file. So I used these functions in my game & send them the main structure so the player can save his position. Everything worked fine except that the player score is diffrent each time. but everything else like health,xPos etc is correct. Any glues why this is happening ? thanx in advance WizHarD
who is it that keeps on nicking WizHarD name !! :P
WizHarDx
WizHarDx
// These are the saving & loading functions

// saves a file
template
bool save(char *file,T &instance) // recieve refrence (easier)
{
fstream fout;
// open stream
fout.open(file,fstream::out);
if(!fout.is_open())
{
return false; // opening file failed
}
fout.write(reinterpret_cast(&instance),sizeof(T));
fout.close();

// finshed writing too
return true;
}
// loads a file
template
bool load(char *file,T &instance) // recieve refrence (easier)
{
fstream fin;
fin.open(file,fstream::in);
if(!fin.is_open())
{
return false;
} // if fail
fin.read(reinterpret_cast (&instance),sizeof(T));
fin.close();
return true;
}

// Here are the functions that update the structure to be saved
void Game::Update()
{
Ammo=player1.Ammo;
Health=player1.health;
Level=DDraw.Level;
MaxBots=DDraw.MaxBots;
ReInCarnation=player1.ReInCarnation;
Score=player1.Score;
xPos=player1.xPos;
yPos=player1.yPos;
return;
}

// Makes all the varibles = to the struct
void Game::Reupdate()
{
player1.Ammo=Ammo;
player1.health=Health;
DDraw.Level=Level;
DDraw.MaxBots=MaxBots;
player1.ReInCarnation=ReInCarnation;
player1.Score=Score;
player1.xPos=xPos;
player1.yPos=yPos;
return;
}

// Here are the functions that call the save & load templates
Game game;
// saves the game
void SaveGame(char *file)
{
Game &Save=game; // make refrence which is passed to
Save.Update(); // save template function
instance.save(file,Save);
return;
}
// Loads the game
void LoadGame()
{
Game &Load=game;
instance.load("wizhard",Load);
Load.Reupdate();
}

Now it compiles all right. It saves fine & loads fine. But the score part is loaded wrong ??
thanx in advance WizHarD
who is it that keeps on nicking WizHarD name !! :P

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.