Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transitions from rootArchetype to rootArchetype fail 0.9.0-beta.0 #157

Open
TheyCallMeRyan opened this issue Jan 21, 2025 · 1 comment
Open
Labels
bug Something isn't working

Comments

@TheyCallMeRyan
Copy link

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 TheyCallMeRyan added the bug Something isn't working label Jan 21, 2025
@TheyCallMeRyan
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant