junkdog / artemis-odb

A continuation of the popular Artemis ECS framework
BSD 2-Clause "Simplified" License
777 stars 112 forks source link

[Question] Copy a component, or create it from Json #628

Closed pylvain closed 3 years ago

pylvain commented 3 years ago

Hello ! I want to allow an entity, its components, and their values to be described from a json file, with a specific syntax of my own. Let's say I have the following basic component :

class LocationComponent( var x: Int = 0, var y: Int = 0) : Component()

and the following json string :

{ "x" : 12, "y" : 3 }

I could not find in the documentation an obvious way to what this fictive function would do :

val entity = world.create()
LocationComponentMapper.createFromJson(entity,"""{ "x" : 12, "y" : 3 }""")

Of course I can use something like Gson to instantiate a Component from json, but i cannot find a way to copy the component, excepted by hand, onto the newly created component. I also read about prefabs, but that doesn't seam to be flexible enough, I really want to be able to create components one by one.

Thank you for your attention !

genaray commented 3 years ago

Well... its not possible in that way ^^ You actually can create entities from json, but in another way... https://github.com/junkdog/artemis-odb/wiki/Prefabs

Another way im currently using is to use LibGdx to serialize/deserialize components. This way we can easily store a array of serialized components in an json file and then we load them, deseralize them and attach them to some entity.