Serializing individual entities

Started by
6 comments, last by 3DModelerMan 9 years, 4 months ago

So, I've ran into another snag with my entity system. I've started building one based somewhat on this article. It's working great, the performance is amazing, and the extra flexibility I get in implementing systems is something I like a lot more than my old Unity style game object system. The one thing I'm struggling with though, is serialization. I have the component data managed entirely by the ComponentManager, so it stores it in whatever layout it prefers and then handles saving all of the data or loading all of the data. It works fine for saving/loading entire scenes, because I can just dump all the data into a file. I'd like to be able to define spawnable prefab entities though, and it doesn't handle that well. My first idea was to have a serializeEntity method that would take the EntityID and serialize the component data for just one entity, but that's yet another virtual function that has to be implemented when writing a new type of system. What are the best methods for implementing prefab resources within an entity system like this? Should I maybe take a step back and treat it like a DirectX vertex layout and require that systems define an interface to access component data as a struct? Or maybe are there any sort of reflection techniques that I might want to be aware of?

Advertisement

Just a thought: What if every System provides a Clone(entityToClone, newEntityID) function. If 'entityToClone' already contains that System's component type, then that System creates a new component for newEntityID, copying the data of entityToClone, otherwise it does not create a new component for 'newEntityID'.

To instantiate an entity from a template, it basically becomes:


EntityID newEntityID = getNextFreeEntityID();

for(everySystem)
{
    system.Clone(templateID, newEntityID);
}

(general example. I'm not actually suggesting all the systems inherit from a base abstract system)

And in your entity system, it can mark the template-entities as "inactive" or "template" or whatever, so actual logic is skipped for the templates themselves. Or it can keep the templates in a different map, and serialize those to a template file.

That's an interesting problem I faced some time ago.

Mind that system was fairly involved. It was built around flexibility so pretty much everything could have arbitrary scripts attached. My plan was to re-use the garbage collector machinery to isolate and entity data set and serialize it (I know I can serialize full heaps and restore correctly).

However, when it comes to partial serializations, there were so many other difficulties I couldn't solve such as handling shared data. In general I wasn't sure of validity of this operation so the thing never made it.

Can you describe your use-case in gameplay terms and how it relates to the system envisioned here? Are there any special properties you can exploit to cut corners?

Previously "Krohm"

In gameplay terms I want to be able to place an object with a spawn point that will have a reference to a player prefab. Any special properties... I allocate all entities in one contiguous array, and they're just an index and a generation value like in the BitSquid article. Component data is usually allocated as a structure of arrays, although it's kind of dependent on the system whether it is or not, it's pretty common to have some of the data that doesn't get iterated a lot (GUIDs, tags, etc.) in a hash map instead.

For this specific example I would first of everything try to regen them from scratch. Is this an optimization?

Previously "Krohm"

What do you mean by regen them from scratch? Like have some sort of descriptive format for saving them and build them from that?

Yes, you've talked about prefabs previously. What I really mean "between the lines" is that just doing new SpecialEntity() in some way might likely end up being just as fast as the whole de-serialization thing. Of course in this case we're no more talking about a generic de-serialization but a more specific kind of spawning. You can see this has plenty of advantages for your mental sanity.

Previously "Krohm"

Okay. So basically just creating component templates using structures and allocating them individually. For the amount of things I'll actually be spawning that way that actually sounds pretty good. I think the only real big loading costs are going to be the things that are placed on levels at the start anyways. I'll be having probably 9v9 player counts at most, so spawning 18 prefabs like that, plus whenever I have to spawn some sort of rocket or other slow moving projectile wouldn't be too bad.

This topic is closed to new replies.

Advertisement