Savegame structure for Entity/Component System with complex components [SOLVED]

Hello dear JGO :slight_smile:

I am in need of a proper way to save my game to files.
The options i looked at are mainly XML/JSON/Serialization/Custom, and the ones i consider are JSON, Serialization and a custom parser.

My problem with this whole thing is, that my components get more and more complex, and use a lot of data types (float, bool, int etc. which is not the problem) and also objects of other classes i wrote for specific Components.

So basically, my components contain basic data types, and class objects which again contain other class objects.
This was great for programming small Components, since i could simply initialize my Components with behaviour of classes i wrote specifically for them.

I tried serialization, but then i would need to implement Serializable on EVERY SINGLE class i used in the component(right? Oo), which seems to me like a dirty solution.
Serialization seems great and easy, but makes things pretty much invisible to debug, and hard for me to adjust my engine architecture to it. Also, i will have a lot of entities on different layers(like in a tile map) etc.

On the other hand side, if i were to use something like Json, i would have the problem of needing to save one of my class objects as a string description of some sort, and then load it by identifying the string, get its values stored in a similar descriptive way, and then create the correct class objects from that.
Which is pretty much reinventing the wheel, allthough it may be a bad way of doing things.

I really have no idea how to approach this problem. And yes, i have tried to simplify my components by quit a bit, but that doesnt change the fact that i dont want to write a new component(and sometimes also a system) for each and every type of Plant, for instance.
I would really like to create a simple parser, game->Text->Game, for my loading and saving (i dont need obfuscation/encryption), nor am i working with a team.
I am ‘off course’, using Java(LWJGL)…

Really need some help from the masters :slight_smile:

Take a look at xstream

You have already posted a similar thread here with more details on your Entity / component API you created. http://www.java-gaming.org/topics/how-to-save-game-data-with-entity-component-based-engine-via-json/34784/msg/333101/view.html These details do reveal the difficulty of your serialization efforts. More or less your component system from the other post was a bit basic. IE one level of storage under an Entity class as the container.

I’ll assume your design hasn’t changed much from your other post.

Which is sort of like traditional OOP again once you have explicit composition back into the mix. Other standard object serializers will not be able to understand the 1st tier implicit lookup of your Entity class. But potentially serializing the 1st tier of components since they use explicit composition from then on could work by a standard serialization library. (Kryo is a good one for Json).

I assume you at least separate things into pure data components and then system components that work only on the data ones. Perhaps add an annotation to your data components that marks them as such. You’ll still have to create a little bespoke save / load parser for serialization that walks the 1st tier of components and looks for any that have the @DataComponent or whatever annotation tag you make at the Class level. Once you find one of those then chuck it into the grinder of whatever standard OOP serializing library you are using. On load you’ll have to conceivably partially compose an Entity of a particular type adding all relevant systems then deserialize each data component and add it to the entity. If all goes well the object graph of each data component even the explicit composition aspects are handled by your library of choice.

Yeah don’t do that. Josh Bloch, the fellow involved in implementing Serializable back in the Java 1.0 days, profusely apologizes for this blight.

That’s what some of the 3rd party libraries already do which is serialize / deserialize an object graph.

Use Kryo and TaggedFieldSerializer.

Thank you all for your quick answers.

@ cylab:
I will definitely have a look at xStream, seems like they have a nice way of working out some of the problems i have, thank you :slight_smile:

@ Catharsis:
I know that this is pretty similar, but i thought about all of my options for quite some time. Then i though a new post would be the easiest as i could rethink my question as well.

Quote by Catharsis:

[quote]Which is sort of like traditional OOP again once you have explicit composition back into the mix.
[/quote]
Yes i only use the composition as components, i.e, data only. I don’t think i could do without composition, as i dont want to write a specific component for each ‘Type’ of an Entitiy derivation. But all this made me think about my component approach, and honestly, i think i have found my error in the approach of an ECS.
But some of my components are so complex that i really need to create 2-4 specific classes for it. Allthough i would like my components to be ‘Data only’, they still are and an external system (static method or object method), runs on it.
But i realize a lot of people are having trouble with this. But i tend to over-think stuff before i actually do it. I also realize that saving/loading when using composition will be the same, or even more.

I’ll try Kryo and xStream before anything else now.

But i have one more question.
What advantage does it give me work this way: ->object–>Serialize—>XML—>De-serialize–>object?
When i could just use a library that goes: ->object->XML->object (by which i mean, to define my own field to XML directly without the need for a library which de-/serializes it before and after)
I think i will break my own save/load system a lot, so i tend to think that doing the basic things on my own, even if it is basic, i’ll have a less harder time to find a way to make it work.

The work has to be done whether you write your own serialization/deserialization or use a library. Kryo does it for you automatically and TaggedFieldSerializer will let you evolve your class while still being able to load previously serialized bytes, again automatically.

I will try Kryo and xStream for a while, so that i know what it all is about. Thank you again for the answers.
I just didn’t know where i’m at and how to do it with all the stuff one has to deal with when programing a game.

Thank you very much!

Greetings and all the best

Composition is great and indeed it sounds like you are 10% of the way there for an efficient ECS, but are still clinging to the old ways. It takes time believe me as I’ve worked on mine for years as things go.

You’re doing it wrong… ;p This screams thinking more about the separation of data and the systems that process it. In an ideal scenario a 1:1 or 1:N arrangement is good. 1 data component containing everything a system component needs to process this data and complete a task.

You are making very heavyweight “objects” despite having that 1st tier of implicit composition (seen in your other post). Components are more about the architecture / API that provides implicit composition and ability to manipulate those relationships. IE calling a heavyweight object a component does not make it so.

There is very little codified per se for ECS.

Ok gotta go. Flying and phones off right now… :smiley:

[quote]Composition is great and indeed it sounds like you are 10% of the way there for an efficient ECS, but are still clinging to the old ways. It takes time believe me as I’ve worked on mine for years as things go.
[/quote]
10% sounds kind of rough to me, now, after all these months of ‘research and pain’ :stuck_out_tongue:

[quote]You’re doing it wrong… ;p This screams thinking more about the separation of data and the systems that process it. In an ideal scenario a 1:1 or 1:N arrangement is good. 1 data component containing everything a system component needs to process this data and complete a task.
[/quote]
I guess, i thought it would be intelligent, to tidy stuff away in a class object and just initializing my Component with it when some more complex data is needed :slight_smile:
So thats the OOP way of thinking, i have to get rid of then for an ECS. And now where you point it out to me(and i’ve read a lot of times on the sites, ‘you’ll need time time to change your mindset’…)
Hmm… But classes… are so…hmmm…tidy and you can put everything int them and extend them :point: :’( …

Also, i usually tried to not have 1:1 or 1:N arrangement the way you say it should be, i tried to aim for: => Less components,but more ‘modular’ and also less systems, but more specific, 1:1 or near, seems more (unecessary?)system/components, just to get to that ratio.

But having though about it, i see that some components really are complex, but could still contain every data needed, for a system to process it. But then some of my components will get a little bloated, which is then pretty much inevitable with this approach, right? Oo
But what about Enum states? I guess i can use Enums which are globally visible (don’t know if thats the right terminology for enum’s), or at least where i need them,
allthough enums are objects. I don’t know how ‘fine grained’ the ECS needs to be…

So: Component(contains ALL data, data only)–>runs through a System(static method/obj. method) identified by EnumSet mask ?

Plus, with making components without any composition apart from enums, my problems with saving/loading will be nicely solved with a clean JSON, and i can handle this myself!

[quote]It takes time believe me as I’ve worked on mine for years as things go.
[/quote]
How does yours roughly work? I’ve started with the GUID where every entitiy is an id and only has components somehow referenced to it, but i’m not feeling ready for this;p Plus, i’ve seen so many approaches i don’t know which to use. Eventually i’m going to need references from entity to entity,which will be a problem when saving if i use my current setup, id’s would make these things easier. (I think i need to dump my current Entity class + component map thingy)

I’ll leave it a that for now, so you don’t have to read so much :x

Thank you very much again for your patience!, you are helping me big time, and the 10% of being on my way to a descent ECS, has given me some kick to put some effort into it again ;D