Game Development Reference
In-Depth Information
for (auto& item : m_items)
{
file << item->GetId();
file << std::endl;
}
file << m_pCurrentRoom->GetId();
file << std::endl;
}
The Player::OnSave method writes out the name the user supplied when beginning his or her game.
It then writes out the number of items in the m_items collection. Each item's ID is written out and
finally the m_pCurrentRoom ID is written out. The block of text in the save file for a player looks like
the following:
1923481025
Bruce
1
3714624381
625001751
The first line is the unique ID of the Player object, followed by the m_name , the number of Items , the
ID of the one item, and finally the ID of the Room the player was in at the time he or she quit.
The Player::OnSave method is mirrored by the Player::OnLoad method in Listing 24-13.
Listing 24-13. The Player::OnLoad Method
void Player::OnLoad(std::ifstream& file)
{
file >> m_name;
unsigned int numItems;
file >> numItems;
for (unsigned int i = 0; i < numItems; ++i)
{
unsigned int itemId;
file >> itemId;
Item* pItem =
dynamic_cast<Item*>(
SerializationManager::GetSingleton().GetSerializable(itemId));
m_items.emplace_back{ pItem };
}
unsigned int roomId;
file >> roomId;
Room* pRoom =
dynamic_cast<Room*>(
SerializationManager::GetSingleton().GetSerializable(roomId));
m_pCurrentRoom = pRoom->GetPointer();
}
 
Search WWH ::




Custom Search