ECS ressources edition and serialization

I began to play with JMonkeyEngine and the ZayEs/ZayES-net/SimEthereal frameworks recently. I started with the provided examples and now I’m heading toward a multiplayer rpg game.
My goal:
I want to build a basic editor to easily place some gameobject in the game world and add them some properties like we do with entities, just by adding them some components to determine their behaviour.
At the end of the road I imagine one format for the editor with all the ressources. Them I could export the server ressources with only geometry element for map and logic ressources(item statistics , etc…) and the client ressources with only the maps and the assets.
What I need:
So I ll need a way to represent/serialize/deserialize my ressources, which I want to be entities if possible.
What my problem:
Above all, I don’t know how to handle the identification of entities, should I use the generated entityId or make a specific Id component in order to be independant of the entityId generate at each entity creation .
First things first, it is a common/good practice to create/serialize/deserialize game ressources as components/entities ?
Should I use the ZayEs framework in the editor and extend the EntityData to make a FileSerializer?

Hi @NyouB

Note, there is already a SqlEntityData and PersistentComponent provided in Zay-ES to save entity state on database.

But I assume you are asking about creating entity “prefabs”.

“prefabs” which are blueprints for game entities. They contain a list of components and default values for those components. Prefabs can be instantiated many times, which is an efficient way of creating many entities of the same kind.

I create entity prefabs in JSON. (I am using Gson for this).

For this, I am using the concept of a “Scene Prefab”. It’s a JSON file that is generated by the scene editor and has a unique scene name and contains all the entity prefabs IDs with their spawn positions in the scene. So in the server when I want to spawn a scene I load the scene prefab JSON file (using Gson) and spawn each entity in the defined spawn position.

Hi, thanks for your reactivity.
The prefabs looks exactly the tool I need. Do I have to develop the prefab system or does it exists already in the ZayEs ecosystem?

Zay-ES does not provide a tool for creating prefabs, but you can easily do it with Gson.

You can do something similar to this:

public class EntityPrefab {

    private Map<Class, EntityComponent> componentMap;

    public EntityPrefab(EntityComponent... components) {
        this.componentMap = new HashMap<>();
        for (EntityComponent component : components) {
            setComponent(component);
        }
    }

    public <T extends EntityComponent> void setComponent(T component) {
       componentMap.put(component.getClass(), component);
    }

    public <T extends EntityComponent> T getComponent(Class<T> type) {
       return (T) componentMap.get(type);
    }
    
    public EntityId createEntity(EntityData ed) {
        EntityId id = ed.createEntity();
        for (EntityComponent component : componentMap.values()) {
            ed.setComponent(id, component);
        }
        return id;
    }
}

You create an entity prefab set components and serialize it with Gson.

You can do a similar thing for the ScenePrefab class. At its base it’s a collection of entity prefab names and their position in the scene.

Note you need to use RuntimeTypeAdapterFactory for using polymorphism in Gson.

Saying so, you need to register your component types in it like below:

RuntimeTypeAdapterFactory<EntityComponent> componentAdapterFactory = RuntimeTypeAdapterFactory.of(EntityComponent.class, "cType");
// register components  
componentAdapterFactory.registerSubtype(MyComponent1.class);
componentAdapterFactory.registerSubtype(MyComponent2.class);
...

and use it to create Gson instance

Gson gson = new GsonBuilder()
                .registerTypeAdapterFactory(componentAdapterFactory)
                .create();

now you can use this gson instance to save/load an EntityPrefab to/from JSON file.

// Save
EntityPrefab prefab1 = ...
gson.toJson(prefab1, EntityPrefab.class, path);

// Load
EntityPrefab prefab1 =  gson.fromJson(EntityPrefab.class, path);
1 Like

Thanks, I will try it asap