Hello,
i´m developing a little console rpg with c++.
My problem:
I use the following object hierarchy in my project:
Parsable -> Entity -> Player/Enemy (Player and Enemy inherit from Entity, Entity inherits from Parsable)
Parsable -> Item -> Weapon/Armor/Material/Consumable (Weapon/Armor/Material/Consumable inherit from Item, Item inherits from Parsable)
Which part of the program should know how to parse the objects, what is a valid way to get a specific instance of an object based on a typeid and how should they get populated?
My current solution(which works):
Parsable specifies three methods parse(filePath), populate(vector), serialize() (to write them back into a file). Entity and Item implement them and do the work as following:
1) Open File -> Read Line by Line -> split the lines by a delimiter -> push them into a vector -> check the first token (it decidecs which type i want to instantiate) -> call a factory method to get a new instance of the specified type -> call the populate method of the specified instance and pass them the "token vector" -> let the object set its member variables -> save the object into a std::map (Key = ItemID, value = Object itself).
Do you think it is valid to let the objects decide how they should populate themself?
Can you give me some advice?
Thank you in advance