matter icon indicating copy to clipboard operation
matter copied to clipboard

Transitions from rootArchetype to rootArchetype fail 0.9.0-beta.0

Open TheyCallMeRyan opened this issue 11 months ago • 1 comments

Description

Image With the way transitionArchetypes works, transitioning from the same archetype to itself will always set the entityRecord.indexInArchetype to the number of elements in archetype.entities + 1, but it will also then put it back in the position it previously held. This leads to the indexInArchetype being 1 higher than the number of elements in archetype.entities every time. If additional entities are then added to it, multiple entities will have the same reference to the same element in the list. This makes it possible to remove an entity from archetype.entities that exists without removing the element that no longer exists.

Steps to reproduce

world:startDeferring()
world:commitCommands()
world:spawnAt(1)
world:spawnAt(2)
world:spawnAt(3)
world:spawnAt(4)
world:spawnAt(5)
world:commitCommands()
world:replace(3)
world:replace(4)
world:commitCommands()
world:spawnAt(6)
world:commitCommands()
world:despawn(4)
world:despawn(3)
world:commitCommands()
world:insert(2, Component({text = "Test 1"}))
world:commitCommands()

^ Guaranteed to get the above error

Expected behavior

The example above is a bit contrived, but I think the more common case would be if you had an entity that had no components and then you despawn it. Both executeDespawn and executeReplace have this issue because they start by transitioning to the rootArchetype before anything else.

I would expect to see this happen if, for example, the server is replicating entities to the client, and the client then removes all components from one such entity without despawning it, and eventually the server tells it to despawn the entity.

TheyCallMeRyan avatar Jan 21 '25 12:01 TheyCallMeRyan

As mentioned in the discord, I think the best solution is to include an if archetype == oldArchetype then return Another possible solution would be to prevent this in executeDespawn / executeReplace since they always transition to rootArchetype even if it currently is the rootArchetype.

TheyCallMeRyan avatar Jan 21 '25 12:01 TheyCallMeRyan