Fixed world:delete() not calling OnRemove hook

This commit is contained in:
Intrinsic 2025-01-16 15:06:42 -08:00
parent 2171e68ee8
commit 610b668665
No known key found for this signature in database
2 changed files with 46 additions and 1 deletions

View file

@ -957,8 +957,10 @@ local function archetype_delete(world: World, archetype: Archetype, row: number,
-- TODO: if last == 0 then deactivate table
local component_index = world.componentIndex
for _, id in id_types do
local on_remove: (entity: i53) -> () = world_get_one_inline(world, id, EcsOnRemove)
local idr = component_index[id]
local on_remove: idr.hooks.on_remove
if on_remove then
on_remove(delete)
end

View file

@ -1659,4 +1659,47 @@ TEST("wildcard query", function()
CHECK(counter == 1)
end
end)
TEST("world:delete() invokes OnRemove hook", function()
do CASE "#1"
local world = world_new()
local A = world:entity()
local entity = world:entity()
local called = false
world:set(A, jecs.OnRemove, function(e)
called = true
end)
world:add(entity, A)
world:delete()
CHECK(called)
end
do CASE "#2"
local world = world_new()
local pair = jecs.pair
local Relation = world:entity()
local A = world:entity()
local B = world:entity()
world:add(B, pair(jecs.OnDelete, jecs.Delete))
local entity = world:entity()
local called = false
world:set(A, jecs.OnRemove, function(e)
called = true
end)
world:add(entity, A)
world:add(entity, pair(Relation, B))
world:remove(entity, B)
CHECK(called)
end
end)
FINISH()