Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Saving Question


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   Members   -  Reputation: 1070

Like
0Likes
Like

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

 

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



Sponsor:

#2 L. Spiro   Crossbones+   -  Reputation: 16272

Like
5Likes
Like

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   Crossbones+   -  Reputation: 9139

Like
1Likes
Like

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 {
   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, 10 July 2014 - 12:43 AM.


#4 frob   Moderators   -  Reputation: 25489

Like
2Likes
Like

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 write about assorted stuff.


#5 SeanMiddleditch   Crossbones+   -  Reputation: 8711

Like
1Likes
Like

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!

#6 EarthBanana   Members   -  Reputation: 1070

Like
0Likes
Like

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.



PARTNERS