Fix listener array indexing in observers

This commit is contained in:
Ukendio 2025-04-25 22:45:41 +02:00
parent 0816c645da
commit d237b176e6

View file

@ -89,12 +89,6 @@ local function join(world, component)
end
end
local positions_join = join(world, Position)
for e, v in positions_join() do
end
local function query_changed(world, component)
assert(jecs.IS_PAIR(component) == false)
local callerid = debug.info(2, "sl")
@ -208,7 +202,6 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
world.added = function(_, component, fn)
local listeners = signals.added[component]
local max = #listeners + 1
local component_index = world.component_index :: jecs.ComponentIndex
assert(component_index[component] == nil, "You cannot use hooks on components you intend to use this signal with")
if not listeners then
@ -222,6 +215,7 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
end
world:set(component, jecs.OnAdd, on_add)
end
local max = #listeners + 1
listeners[max] = fn
return function()
local n = #listeners
@ -232,12 +226,10 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
world.changed = function(_, component, fn)
local listeners = signals.emplaced[component]
local max = 0
local component_index = world.component_index :: jecs.ComponentIndex
assert(component_index[component] == nil, "You cannot use hooks on components you intend to use this signal with")
if not listeners then
listeners = {}
max = 1
signals.emplaced[component] = listeners
local function on_change(entity: number, id: number, value: any)
for _, listener in listeners :: any do
@ -245,9 +237,8 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
end
end
world:set(component, jecs.OnChange, on_change)
else
max = #listeners + 1
end
local max = #listeners + 1
listeners[max] = fn
return function()
local n = #listeners
@ -258,7 +249,6 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
world.removed = function(_, component, fn)
local listeners = signals.removed[component]
local max = #listeners
local component_index = world.component_index :: jecs.ComponentIndex
assert(component_index[component] == nil, "You cannot use hooks on components you intend to use this signal with")
if not listeners then
@ -271,6 +261,7 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
end
world:set(component, jecs.OnRemove, on_remove)
end
local max = #listeners + 1
listeners[max] = fn
return function()
local n = #listeners