Store ES to file

Zay ES is a wonderfull abstraction of game data and I wonder if it is possible to store the current state to a file and load it from a file is easily possible. I currently wrote some json based load/save but I store the stuff manually. This is painfull when ever I extend / change entity component set.
Is there some best practice? Experiences?

Well if you only store simple data as you should in the components,

you could for example use xstream to export them without further work to xml
or jackson to write to json (note i use this)

So if you loop over all entites & components you should be able to save them directly

Thanks for the pointers. I think that it is not directly possible for my case because I have a Link component which is a link to another entity.
But just wonder if you have a class with only getters how would it look like to “serialize” this into a json object then?

Wait…
the link is not just the id of the other entity (as it should be)?

getters/setters can be ignored, those libs can directly serialize using reflection based on the fields.

Yes it is only the id, but if you load it back I guess you have then other ids or it is not guaranteed that you have the same, except that load back restores it 1:1. But ok I will then have a look at those mentioned libs.

Well, no matter what you will have to keep the entity IDs the same when you reload… and then make sure that your ID generator is seeded with the appropriate starting value.

I’ve never solved this problem personally because I just use the database support already built in. There was a thread where someone else was doing it and I showed them the minimum they’d need to extend the DefaultEntityData class to capture the data needed to fully introspect the components for serialization… but I don’t remember what it was.

Well I use 265 bit id’s for mine entities, witha generator that ensures:

No two servers will ever generate the same id’s
As long as no server creates more Integer.Max entites per MS there cannot be duplicates.

So in my case restoring is as simple as just using the same ID again, as they cannot overlap.

(Also I have a clone util, that creates a deepclone from an Entity and a list of linktypes, e.g. I can easily duplicate a spaceship that way with all internal state)
→ Not really usefull ingame, but really great for level editing creation from parts (eg large_space_station_with_dock_and_hostile_ai.template)

ah you refer to the hsqldb right? do you need a server for that or is it possible to embed it somehow? is there maybe a sample so I can wrap my head around? Or how is this server thing solved, I mean I just want to start my game :wink:

No server involved. It’s just files on the disk. You just instantiate and use the different EntityData implementation.

EntityData ed = new SqlEntityData( File dbPath, long writeDelay )

…use ed as normal. Components that implement PersistentComponent will be stored. Components that don’t, won’t.

It’s more like minecraft storage at that point as it’s always moving forward. You can’t go back to some specific point in time. It’s always saving, basically.

Aha ok that would be fine I guess it is more a save it on exit and resume. I guess for the sql stuff I need that hsqldb jar or something like that. I also guess a clear is possible somehow. I’m currently not sure if I need a go back in time anyway.
I’ll experiment.
Thanks to all of you for the pointers.

Aha so you can have an own id generator. the default just seems to count upwards. And if I do it the same order it should work. just have no clue how to get all entities and then get all components.

…just need to find that other thread where I described how to subclass DefaultEntityData and override one method to capture the info needed.

Probably just search for that class… there can’t be too many mentions of it.

It’s definitely a ‘resume’. But no need to save on exit… it saves always at whatever delay (usually small like 1 second) that you specify. The delay is only for performance.

Ah I already wonder about this delay. good to know. I’ll lookup that thread. thanx