Game Development Reference
In-Depth Information
virtual void OnSave(std::ofstream
&
file);
virtual void OnLoad(std::ifstream
&
file);
Pointer GetPointer() const { return m_pointer; }
};
Now any time any part of our code wishes to store a
shared_ptr
to a
Serializable
object, it should
be retrieving the pointer from a shared place. The easiest place for this to be is on the object itself,
which is registered with the
SerializationManager
via its unique ID.
The
Room
class has to save and load the state of its dynamic options. Listing 24-15 shows the save
and load methods.
Listing 24-15.
Room::OnSave
and
Room::OnLoad
void Room::OnSave(std::ofstream& file)
{
file << m_dynamicOptions.size();
file << std::endl;
for (auto& dynamicOption : m_dynamicOptions)
{
file << dynamicOption->GetId();
file << std::endl;
}
}
void Room::OnLoad(std::ifstream& file)
{
m_dynamicOptions.clear();
unsigned int numDynamicOptions;
file >> numDynamicOptions;
if (numDynamicOptions > 0)
{
for (unsigned int i = 0; i < numDynamicOptions; ++i)
{
unsigned int optionId;
file >> optionId;
Option* pOption =
dynamic_cast<Option*>(
SerializationManager::GetSingleton().GetSerializable(optionId));
if (pOption)
{
Option::Pointer sharedPointer = pOption->GetPointer();
m_dynamicOptions.emplace_back{ sharedPointer };
}
}
}
}