Game Development Reference
In-Depth Information
if (iter != m_serializables.end())
{
iter->second->OnLoad(file);
}
}
}
}
else
{
found = false;
}
}
return found;
}
The
Load
method is a little more involved than
Save
. You can see that it is using an
ifstream
,
input
file stream
, rather than an
ofstream
. The
ifstream
is initialized using the file name to load. The
is_open
method in
ifstream
is used to determine if a file with the given name was found. If the
player has never played the game, then no save file will exist; this check ensures that we do not try
to load a game when no save game exists.
The next check is used to determine if the save file that does exist has a valid save state inside. This
is done using the
>>
operator, just as is done when using
cin
. This is what happens next when
cin
is
used to ask the player if he or she would like to load the save game. If the player types anything but
yes,
then game will start without loading.
There is then a while loop that is checking if the
eof
method is returning
true
. The
eof
method is
determining whether the method has hit the
end of file
. The inner section of this loop reads the
unique ID from the file, retrieves the
Serializable
from the map, and then calls the
OnLoad
method
on that object.
The last
SerializationManager
method is
ClearSave
, which is used to write out a file with
false
as
its only value. Listing 24-5 shows this method.
Listing 24-5. The
SerializationManager::ClearSave
Method
void SerializationManager::ClearSave()
{
std::ofstream file{ m_filename };
file << false;
}
The
SerializationManager
class is fairly simple. The
Serializable
class is also straightforward
and is listed in Listing 24-6.
Listing 24-6. The
Serializable
Class
class Serializable
{
unsigned int m_id{ 0 };