matter
matter copied to clipboard
Incorrect _size values for deferred spawn/despawn 0.9.0-beta.0
Description
Can get negative sizes when despawning and then immediately spawning within the same commit commands.
This is caused by the spawnAt functionality:
if not willBeDeleted then
self._size += 1
end
executeDespawn will still occur even if a later spawnAt command it executed, and executeDespawn will always decrement the _size by 1, even if markedForDeletion is no longer set.
Steps to reproduce
world:commitCommands()
world:spawnAt(1)
world:commitCommands()
world:despawn(1)
world:spawnAt(1)
world:commitCommands()
world:despawn(1)
world:spawnAt(1)
world:commitCommands()
world:despawn(1)
world:spawnAt(1)
world:commitCommands()
print(world:size())
Expected behavior
Keep the appropriate _size value.
Two possible fixes:
- Remove the if not willBeDeleted then - this will make size inaccurate until the next commitCommands
- Add in executeDespawn a check that it's still markedForDeletion before decrementing the size
I'm not sure what people use _size for, I just noticed this while trying to find the indexInArchetype bug yesterday.