Saving Question

Started by
4 comments, last by EarthBanana 9 years, 9 months ago

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

Advertisement

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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

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


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.
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!

Sean Middleditch – Game Systems Engineer – Join my team!

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

This topic is closed to new replies.

Advertisement