-
Notifications
You must be signed in to change notification settings - Fork 1
Description
System information.
Commit Hash: 2489738
Description.
Ill be referring to Entity as entity id.
Sometimes other entities reference other entities through their components.
An example is as follows; i have a follower entity whose purpose is to follow other entities.I have another entity which will be followed by the follower.The follower entity has a reference to the followed entity (by using the entity id). I despawn the followed entity and now the follower entity stops in its tracks as it references an invalid entity id.
I spawn a new entity somewhere else and suddenly the follower decides to follow the newly spawned entity.
The fact that it automatically follows the newly spawned entity is surprising as i didn't explicitly change the reference nor tell it to follow that newly spawned entity.This i found out because the newly created entity replaces the despawned one at the entity id referenced by the follower entity.
This example shows that entity ids should be unique to prevent such unwanted behavior.
Steps to reproduce.
A simple demo:
const world = new World()
const e1 = world.spawn([]) // spawn an entity id of 0
const e2 = world.spawn([]) // spawn an entity id of 1
world.despawn(e2) // despawns e2
const e3 = world.spawn([]) // spawns an entity of id 1In this case,e3 has the same entity id as e2 after despawning e2.
Expected behaviour.
The entity id should be unique for all entities spawned in a World for a given application run.
Actual behaviour.
Entity id of despawned entities are used as entity ids of the newly spawned entities
Cause(s).
This is because we recycle entity indexes(which are used as the entity id) thus causes newly created entities to spawn with previous entity ids
Solution(s).
A good approach make Entity to be composed of both the index and the generation of the entity.
The generation is incremented every time an entity on a given index is despawned.
Extra care must be taken when actually using the Entity to ensure that it has a valid generation and index when fetching using the Entity.