I am working on a text-based RPG game and I want to allow the player to save his progress. So I need to save several integers and a string. And my problem starts here "How can I save integers and load them?". I read the tutorial but I dont understand. Can anyone help me to learn or write me function to save game?
struct player_t
{
int power_level;
int hp;
int gold_coins;
...
void save_to_file( string filename ) const
{
ofstream f( filename.c_str() );
f << power_level << endl;
f << hp << endl;
f << gold_coins << endl;
}
bool load_from_file( string filename )
{
ifstream f( filename.c_str() );
f >> power_level;
f >> hp;
f >> gold_coins;
return f.good();
}
};
player.save_to_file( "user_scores.txt" );
1 2 3 4
if (!player.load_from_file( "user_scores.txt" ))
{
showmessage( "Fooey, I could not restore your game!" );
}
If you want to get fancy and use INI files for multiple players and the like (and you are on Windows), just #include <windows.h> and use the functions Disch explains here: http://www.cplusplus.com/forum/beginner/30814/#msg166989
Okey I code 2 functions, one for saving and one for loading. But when I try to save the program stops. And when I look into save file there are blank lines.
When I try to load game ıt doesnt load after 6.line.
void func_savegame()
{
ofstream save( wrath.c_str());
save << player.name << endl;
save << player.equip.weapon.name << endl;
save << player.equip.weapon.attck << endl;
save << player.equip.weapon.defense << endl;
save << player.equip.weapon.magic << endl;
save << player.equip.weapon.price << endl;
save << player.equip.weapon.type << endl;
save << player.equip.armor.name << endl;
save << player.equip.armor.attck << endl;
save << player.equip.armor.defense << endl;
save << player.equip.armor.magic << endl;
save << player.equip.armor.price << endl;
save << player.equip.armor.type << endl;
save << player.equip.shield.name << endl;
save << player.equip.shield.attck << endl;
save << player.equip.shield.defense << endl;
save << player.equip.shield.magic << endl;
save << player.equip.shield.price << endl;
save << player.equip.shield.type << endl;
save << player.level << endl;
save << player.level_hp << endl;
save << player.level_mp << endl;
save << player.level_attck << endl;
save << player.equip_hp << endl;
save << player.curr_hp << endl;
save << player.max_hp << endl;
save << player.equip_mp << endl;
save << player.curr_mp << endl;
save << player.max_mp << endl;
save << player.curr_attck << endl;
save << player.equip_attck << endl;
save << player.curr_exp << endl;
save << player.max_exp << endl;
save << player.max_temper << endl;
save << player.curr_temper << endl;
save << player.stats << endl;
save << player.sp << endl;
for( int i = 0; i < 10; ++i )
{
save << player.skills[ i ].name << endl;
save << player.skills[ i ].attck << endl;
save << player.skills[ i ].defense << endl;
save << player.skills[ i ].magic << endl;
save << player.skills[ i ].time << endl;
save << player.skills[ i ].learn << endl;
}
for( int i = 0; i < 10; ++i)
{
save << player.num_pot[ i ] << endl;
}
save << player.gold << endl;
for( int i = 0; i < 10; ++i)
{
save << player.inventory[ i ].name << endl;
save << player.inventory[ i ].price << endl;
save << player.inventory[ i ].attck << endl;
save << player.inventory[ i ].defense << endl;
save << player.inventory[ i ].magic << endl;
save << player.inventory[ i ].type << endl;
}
save << player.type << endl;
save << chapter << endl;
}