Compare commits

...

3 commits

Author SHA1 Message Date
Intrinsic
5e3739036a
Fixed hooks invocation in the event of relationship target deletion. (#179)
Some checks are pending
analysis / Run Luau Analyze (push) Waiting to run
deploy-docs / build (push) Waiting to run
deploy-docs / Deploy (push) Blocked by required conditions
publish-npm / publish (push) Waiting to run
unit-testing / Run Luau Tests (push) Waiting to run
* Fixed hooks invocation (case #3)

* Reverted world:component() to world:entity()

Co-authored-by: lolmanurfunny <77128366+lolmanurfunny@users.noreply.github.com>

* Removed whitespace

---------

Co-authored-by: lolmanurfunny <77128366+lolmanurfunny@users.noreply.github.com>
2025-01-17 01:46:14 +01:00
Magic
0296c4b2f9
fix 'jecs.World.new()' being inferred to return 'any' (#177) 2025-01-17 00:45:55 +01:00
Intrinsic
c354a29fb1
Fixed world:delete() not invoking OnRemove hooks. (#178)
* Fixed cached wildcard queries

* Fixed world:delete() not calling OnRemove hook

* Typo in hooks fix

* Fixed test case

* Fixed test case (again)

* Fixed test case (again 2x)

* Now it should work
2025-01-17 00:45:29 +01:00
2 changed files with 67 additions and 3 deletions

View file

@ -957,8 +957,10 @@ local function archetype_delete(world: World, archetype: Archetype, row: number,
-- TODO: if last == 0 then deactivate table -- TODO: if last == 0 then deactivate table
local component_index = world.componentIndex
for _, id in id_types do 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 if on_remove then
on_remove(delete) on_remove(delete)
end end
@ -1156,7 +1158,6 @@ do
local idr_t_archetype = archetypes[archetype_id] local idr_t_archetype = archetypes[archetype_id]
local idr_t_types = idr_t_archetype.types local idr_t_types = idr_t_archetype.types
local on_remove = idr_t.hooks.on_remove
for _, child in idr_t_archetype.entities do for _, child in idr_t_archetype.entities do
table.insert(children, child) table.insert(children, child)
@ -1178,6 +1179,7 @@ do
end end
break break
else else
local on_remove = id_record.hooks.on_remove
local to = archetype_traverse_remove(world, id, idr_t_archetype) local to = archetype_traverse_remove(world, id, idr_t_archetype)
if on_remove then if on_remove then
for _, child in children do for _, child in children do
@ -2311,7 +2313,7 @@ export type World = {
} }
return { return {
World = World, World = World :: { new: () -> World },
OnAdd = EcsOnAdd :: Entity<(entity: Entity) -> ()>, OnAdd = EcsOnAdd :: Entity<(entity: Entity) -> ()>,
OnRemove = EcsOnRemove :: Entity<(entity: Entity) -> ()>, OnRemove = EcsOnRemove :: Entity<(entity: Entity) -> ()>,

View file

@ -1659,4 +1659,66 @@ TEST("wildcard query", function()
CHECK(counter == 1) CHECK(counter == 1)
end end
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(entity)
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(Relation, 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:delete(B)
CHECK(called)
end
do CASE "#3"
local world = world_new()
local pair = jecs.pair
local viewingContainer = world:entity()
local character = world:entity()
local container = world:entity()
local called = false
world:set(viewingContainer, jecs.OnRemove, function(e)
called = true
end)
world:add(character, pair(viewingContainer, container))
world:delete(container)
CHECK(called)
end
end)
FINISH() FINISH()