junkdog / artemis-odb

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

Confusion over entity ID assignment #265

Closed ghost closed 9 years ago

ghost commented 9 years ago

Hi there,

I'm using artemis-odb 0.9.0 and I'm noticing some confusing behavior.

I have a set of components I expect to be processed in order of insertion into the world. I build each one with the following code:

    EntityBuilder entityBuilder = new EntityBuilder(world)
        .with(new TiledMapLayer(layer, this, index))
        .group(EntityGroup.SCENE_LAYER.name());

    if (index == 0) {
      entityBuilder.tag(EntityTag.SCENE_MASTER.name());
    }

    entityBuilder.build();

This runs in a loop which iterates over an array on index from 0 to n. Based on the 0.9.0 code, it looks like all EntitySystems listen for relevant entity insertions and mark their actives to be rebuilt just before each processing loop. That part seems to work fine, in that the entities are indeed processed in ascending ID order. However, they are not processed in insertion order:

[main] INFO LayerRenderSystem - Beginning render tick.
[main] INFO LayerRenderSystem - Rendering layer index=2 [Entity ID 658]
[main] INFO LayerRenderSystem - Rendering layer index=1 [Entity ID 662]
[main] INFO LayerRenderSystem - Rendering layer index=0 [Entity ID 663]
[main] INFO LayerRenderSystem - Rendering layer index=3 [Entity ID 664]
[main] INFO LayerRenderSystem - Rendering layer index=4 [Entity ID 665]
[main] INFO LayerRenderSystem - Rendering layer index=5 [Entity ID 666]
[main] INFO LayerRenderSystem - Rendering layer index=6 [Entity ID 667]
[main] INFO LayerRenderSystem - Rendering layer index=7 [Entity ID 668]
[main] INFO LayerRenderSystem - Rendering layer index=8 [Entity ID 669]
[main] INFO LayerRenderSystem - Rendering layer index=9 [Entity ID 670]
[main] INFO LayerRenderSystem - Rendering layer index=10 [Entity ID 671]
[main] INFO LayerRenderSystem - Rendering layer index=11 [Entity ID 672]
[main] INFO LayerRenderSystem - Rendering layer index=12 [Entity ID 673]

As you can see, the entities with indices 2 and 1 have IDs earlier than the entity with index 0. It looks like the Entity Builder has a pool of IDs it uses, but I'd like to have strict control over processing order with some components. Is this possible?

Namek commented 9 years ago

Don't expect to receive your entities in order of insertion, that's overall against ECS architecture. Your system should operate on certain entity or actually it's components. If you want to deal with bunch of entities at once then you should aggregate them manually.

Have a look here: https://github.com/DaanVanYperen/artemis-odb-contrib/blob/master/contrib-jam/src/main/java/net/mostlyoriginal/api/system/graphics/RenderBatchingSystem.java

junkdog commented 9 years ago

It looks like the Entity Builder has a pool of IDs it uses

The framework internally recycles ids.

I'd like to have strict control over processing order with some components. Is this possible?

Systems always process entities in ascending entity.id order (results in slightly faster processing, and it enables much faster insertion/removal). You can work around this behavior by extending EntitySystem and intercepting the entity bag received in EntitySystem#processEntities - it's a bit clunky, but I do it from time to time.

0.10.0 - sometimes later this week - reworked the way systems subscribe to entities, making the whole process smoother.

junkdog commented 9 years ago

(closing for now, but feel free to re-open)

DaanVanYperen commented 9 years ago

boop