I was looking into different methods to implement a pattern that works in loading game save data. For example the player can save the game and it would save their inventory of different items and the type of character the player is.
The items all inherit from a superclass and when the data is saved the name of derived classes are saved into a file. When the data is loaded every data about the items should be converted into their respective derived class objects and stored in a std::vector
.
Below is a rough example of what I am trying to achieve (There could be some errors)
Example Save file:
DarkSword
GoldenShield
EyeOfOdin
This is an example code file:
std::vector<Item> inventory;
std::vector<std::string> loadedInventoryData = LoadFromFile("exampleInventoryFile.txt");
for (const auto& textItem : loadedInventoryData)
{
inventory.emplace_back(ObjectFromString(textItem));
}
My main question is about the ObjectFromString
method. After some search on the internet I came across the factory pattern which required to make a factory class that returns the correct object using a switch-pattern but I wonder how big would this class grow if there are hundreds if not thousands of items in the game that need to be loaded in? Some suggest a hash_map but that surely also a lot to handle at a larger scales.
Is there a scalable method that would not require the programmer to work with a monstrous switch-block? What methods are used in large games such as Minecraft, World of Warcraft or Terraria?
Thank you and sorry if this is off-topic or in the wrong place.
Aucun commentaire:
Enregistrer un commentaire