Define behaviour for world:entity(id)
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

This commit is contained in:
Ukendio 2025-05-08 23:21:38 +02:00
parent fd6883cfed
commit 4bb7e64edd
4 changed files with 20 additions and 17 deletions

View file

@ -1,16 +1,16 @@
local jecs = require("@jecs")
type Observer<T...> = {
type Observer = {
callback: (jecs.Entity) -> (),
query: jecs.Query<T...>,
query: jecs.Query<...jecs.Id>,
}
export type PatchedWorld = jecs.World & {
added: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id, value: T) -> ()) -> () -> (),
removed: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id) -> ()) -> () -> (),
changed: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id, value: T) -> ()) -> () -> (),
observer: (PatchedWorld, Observer<any>) -> (),
monitor: (PatchedWorld, Observer<any>) -> (),
added: <T>(PatchedWorld, jecs.Id<T>, (jecs.Entity, jecs.Id, T) -> ()) -> () -> (),
removed: <T>(PatchedWorld, jecs.Id<T>, (jecs.Entity, jecs.Id) -> ()) -> () -> (),
changed: <T>(PatchedWorld, jecs.Id<T>, (jecs.Entity, jecs.Id, T) -> ()) -> () -> (),
observer: (PatchedWorld, Observer) -> (),
monitor: (PatchedWorld, Observer) -> (),
}
local function observers_new(world, description)

View file

@ -780,7 +780,11 @@ local function world_entity(world: ecs_world_t, entity: i53?): i53
local any = dense_array[dense]
if dense <= alive_count then
return any
if any ~= entity then
error("Entity ID is already in use with a different generation")
else
return entity
end
end
local e_swap = dense_array[dense]
@ -790,9 +794,9 @@ local function world_entity(world: ecs_world_t, entity: i53?): i53
r_swap.dense = dense
r.dense = alive_count
dense_array[dense] = e_swap
dense_array[alive_count] = any
dense_array[alive_count] = entity
return any
return entity
else
for i = max_id + 1, index do
sparse_array[i] = { dense = i } :: ecs_record_t

View file

@ -23,7 +23,6 @@ TEST("addons/observers", function()
world:set(world:entity(), A, true)
CHECK(count == 3)
print(count)
end
do CASE "Ensure ordering between signals and observers"

View file

@ -672,7 +672,7 @@ TEST("world:range()", function()
local e2v1 = world:entity(399)
CHECK(world:contains(e2v1))
CHECK(ECS_ID(e2v1) == 399)
CHECK(ECS_GENERATION(e2v1) == 1)
CHECK(ECS_GENERATION(e2v1) == 0)
end
do CASE "over range start"
@ -685,12 +685,12 @@ TEST("world:range()", function()
local e2v1 = world:entity(405)
CHECK(world:contains(e2v1))
CHECK(ECS_ID(e2v1) == 405)
CHECK(ECS_GENERATION(e2v1) == 1)
CHECK(ECS_GENERATION(e2v1) == 0)
do
local _e2v1 = world:entity(405)
CHECK(_e2v1 == e2v1)
end
world:delete(e2v1)
local e2v2 = world:entity(e2v1)
CHECK(ECS_ID(e2v2) == 405)
CHECK(ECS_GENERATION(e2v2) == 0)
end
end)