Jump to content

View more

Image of the Day

Inventory ! Va falloir trouver une autre couleur pour le cadre D: #AzTroScreenshot #screenshotsaturday https://t.co/PvxhGL7cOH
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

Saving Question

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 EarthBanana   GDNet+   


Posted 09 July 2014 - 11:49 PM

I have a component system - I users of my engine can create their own components by sub-classing Component class - and then each entity has a list of components


When I'm serializing/de-serializing components, considering that when I am deserializing I need to allocate any sub-classed component classes in the engine loading code without actually knowing the sub-class type (user defined) - what is a good way to do this so that the engine user does not have to rewrite any engine code?


IE in the engine i have a Component class with pure virtual functions "serialize(FileStream & fStream)" and "deserialize(FileStream & fStream)"

so that the engine user has to write their own versions of these functions - this works great for serializing to file - but how do I deserialize?


If the engine code is something like




for each component in entity




for each component found in save file

    Component * toAdd = new Component()  (How do I allocate custom user defined components?)

    entity->addComponent(Component * toAdd)


Thanks for any help

#2 L. Spiro   Members   


Posted 09 July 2014 - 11:56 PM


Research the factory pattern.


Each class type has an associated enumerated value.

The user, who is able to create new component types, must also create a factory that takes the custom component type and creates a class of his or her custom type.



L. Spiro

Edited by L. Spiro, 09 July 2014 - 11:59 PM.

#3 Ashaman73   Members   


Posted 10 July 2014 - 12:42 AM

The problem is, how to discover the correct component type engine side, if the usesr are able to write a custom (plugin) ?


First, I would add a factory like management class (compare L.Spiros post) and a unique id, which can be designed by the component coder himself (build in some tests to avoid dublicates etc.):

interface ComponentManager {
   Component deserializeComponent(Stream);
   unique_id getUniqueId();

class ComponentManagerRegistry {
   registerComponentManager(ComponentManager); // map: unique_id -> ComponentManager
   ComponentManager getComponentManager(unique_id);

Each component must provide an unique id and must register itself to the component manager.Then the de-/serializsation code looks like this:

for each Component c do
  unique_id = c.getUniqueComponentManagerId();
  ComponentManager cm = ComponentManagerRegistry.getComponentManager(unique_id);

while stream!=end do
  unique_id = stream.deserializeId();
  ComponentManager cm = ComponentManagerRegistry.getComponentManager(unique_id);
  Componentn new_component = cm.deserialize(stream);

Edited by Ashaman73, 10 July 2014 - 12:43 AM.



Gnoblins: Website - Facebook - Twitter - Youtube - Steam Greenlit - IndieDB - Gamedev Log

#4 frob   Moderators   


Posted 10 July 2014 - 12:52 AM

There are many questions you need to answer about your engine as part of the save system.

While you can use the factory pattern (or something similar) for your game, the save/load system needs to be designed in to nearly every aspect of the game.

In the most extreme example, it is really cool when games offer a 'save game' option in the pause menu. You can stop the world mid-action, pause, save, and quit. Then some later time you can restore and everything will be as it was in the instant you paused. That means mid-animation, mid-audio, mid-effects, mid-accumulation, mid-AI, mid-physics, mid-iteration, mid-script. EVERYTHING needs to be carefully stored so that it can be completely recovered. This can be extremely challenging.

Save points and checkpoints represent among the easiest option. You only need to store the data that you would store between levels, which can be quite minimal. If you look back to many of the console games from the 1980s (3rd gen, NES and similar) the player could get a passcode of around 10-20 symbols that represented the entire saved data between levels. It basically stored the level number, the score, the time or RNG seed, the inventory, and the other minimal tidbits necessary for loading. Relatively few things need to be saved, and it can be quite easy to save off just a single very large data structure.

The point is that you need to think not just about the mechanics of serialization, but also how every game system is going to act. A comprehensive save/load system can be a very large undertaking if you don't plan for it carefully.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.

#5 SeanMiddleditch   Members   


Posted 10 July 2014 - 12:58 AM

frob: I'm thinking he meant saving game data from an editor mode, not necessarily saving player progress and world state, but I could be wrong. Good points anyway, though!

Game Developer, C++ Geek, Dragon Slayer - http://seanmiddleditch.com

C++ SG14 "Games & Low Latency" - Co-chair - public forums

Wargaming Seattle - Lead Server Engineer - We're hiring!

#6 EarthBanana   GDNet+   


Posted 14 July 2014 - 10:38 AM

So I understand making an abstract Component class and an abstract ComponentFactory class where the user must subclass both to make a new component..


If I take this route - how do I know which user-created factory to use when serializing/deserializing? Would I need to keep a map of factories such as std::map<CompTypeID, ComponentFactory>

where the user would need to register their new Factory type? And then for each saved component, save the CompTypeID so the engine can get the correct factory? This seems like it would work but is it overly complicated?


I guess dealing with user errors here would be another concern - if they didn't create a factory for their component - I guess I could just crash the engine with an error message telling them they have to make a factory for their component

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.