Add cleanup for ref

This commit is contained in:
Ukendio 2024-09-06 00:51:47 +02:00
parent fb218dc419
commit a4fe3b29d1
2 changed files with 11 additions and 3 deletions

View file

@ -8,8 +8,9 @@ local components: {
Model: Entity<Model>,
Player: Entity,
Target: Entity,
Transform: Entity<{ old: CFrame, new: CFrame }>,
Transform: Entity<CFrame>,
Velocity: Entity<number>,
Previous: Entity,
} = {
Character = world:component(),
Mob = world:component(),
@ -18,6 +19,7 @@ local components: {
Target = world:component(),
Transform = world:component(),
Velocity = world:component(),
Previous = world:component(),
}
return table.freeze(components)

View file

@ -2,7 +2,13 @@ local world = require(script.Parent.world)
local handle = require(script.Parent.handle)
local refs = {}
local function ref(key)
local function fini(e, key)
return function()
refs[key] = nil
end
end
local function ref(key): (handle.Handle, (() -> ())?)
if not key then
return handle(world:entity())
end
@ -12,7 +18,7 @@ local function ref(key)
refs[key] = e
end
-- Cannot cache handles because they will get invalidated
return handle(e)
return handle(e), fini(e, key)
end
return ref