Does Ark guarentee any kind of ordering with Queries? #252
-
|
I'm wondering if Ark guarentees that when querying for Entities, they are returned in a consistent order (like the order they where created in). Say I have a factory game where items move along a belt. Items at the start of the belt would have always been created first - I never reorder the items on the belt. If I do something like below, will the output always be type BeltItem struct { ecs.RelationMarker }
type Belt struct {}
// Kinda psuedo code
belt := beltMap.NewEntity(&Belt{})
item1 := itemMap.NewEnity(&BeltItem{}, ecs.RelIdx(0, belt))
// some other entities spawn
item5 := itemMap.NewEntity(&BeltItem{}, ecs.RelIdx(0, belt))
// some other entities spawn
item16 := itemMap.NewEntity(&BeltItem{}, ecs.RelIdx(0, belt))
q := ecs.NewFilter1[BeltItem](&world).Query(ecs.RelIdx(0, belt))
for q.Next() {
fmt.Printf("%d\n", q.Entity().ID())
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
|
Hi @Tecnology73, No, like in any archetype-based ECS, there is no such guarantee. In your example below, you should get your expected order, but you can't generalize the assumption. Firstly, the order depends on which archetypes the entities are in. Archetypes are iterated first, and entities within archetypes are iterated in the inner loop. Secondly, the order may change if you remove entities or move them to a different archetype (i.e. if you add or remove components). This is because swap-remove is uses for efficiency. I.e. if you remove any but the last entity of an archetype, it will be replaced by the last entity of the archetype. However, the iteration order is deterministic. This means that if you do the same operations in the same order, iteration order will be the same in any repetition. |
Beta Was this translation helpful? Give feedback.
Hi @Tecnology73,
thank you for using Ark and for reaching out!
No, like in any archetype-based ECS, there is no such guarantee.
In your example below, you should get your expected order, but you can't generalize the assumption.
Firstly, the order depends on which archetypes the entities are in. Archetypes are iterated first, and entities within archetypes are iterated in the inner loop. Secondly, the order may change if you remove entities or move them to a different archetype (i.e. if you add or remove components). This is because swap-remove is uses for efficiency. I.e. if you remove any but the last entity of an archetype, it will be replaced by the last entity of the archetype.
Howeve…