Jump to content
  • Advertisement
Sign in to follow this  
ImInLoveWithCoffee

Should an object know how to parse/populate itself?

Recommended Posts

Posted (edited)

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

Edited by ImInLoveWithCoffee

Share this post


Link to post
Share on other sites
Advertisement

Objects should never now how they are taken from/placed into memory. Each object has an atomic reason for existence and should only care about that. You will usually have multilayer solution for loading/saving game data.

  • Resource Manager checks its caches if the object already exists, otherwise starts the chain to get it
  • You'll have a file anywhere that handles the objects prototype, Resource Manager locates that
  • Each file format needs a protocol that tells the object manager / a loader how to get the contents out of the file. A texture for example can be BMP, JPG or PNG formatted so anyone needs to choose the correct protocoll/ file schema
  • If plain data is in memory then you could start copying the prototype to instances as requested

Same is true for saving those objects

  • Savegame Manager (for example) collects the necessary data to make a savegame
  • Each object is pushed through a serializer and written into a stream
  • Here it is getting tricky:
  •            - Either you have a defined serializer for each object
  •            - or - you have a generalized serializer and some kind of RTTI system
  • The stream is saved to disk

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!