Jump to content
  • Advertisement
Sign in to follow this  
EarthBanana

Saving Question

This topic is 1410 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

 

serialize:

 

for each component in entity

    currentComp->serialize(fStream)

 

deserialize:

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

Share this post


Link to post
Share on other sites
Advertisement

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 {
   serializeComponent(Stream);
   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:

serialize:
for each Component c do
  unique_id = c.getUniqueComponentManagerId();
  ComponentManager cm = ComponentManagerRegistry.getComponentManager(unique_id);
  stream.serailizeId(uniqueId);
  cm.serializeComponent(stream,c);
end


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


Edited by Ashaman73

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

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

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!