Skip to content

Commit 7d4d67f

Browse files
committed
Add world_exists
1 parent 1225f20 commit 7d4d67f

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

jecs.luau

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,19 +1437,24 @@ local function world_delete(world: ecs_world_t, entity: i53)
14371437
end
14381438

14391439
local dense_array = entity_index.dense_array
1440-
local index_of_deleted_entity = record.dense
1441-
local index_of_last_alive_entity = entity_index.alive_count
1442-
entity_index.alive_count = index_of_last_alive_entity - 1
1440+
local dense = record.dense
1441+
local i_swap = entity_index.alive_count
1442+
entity_index.alive_count = i_swap - 1
14431443

1444-
local last_alive_entity = dense_array[index_of_last_alive_entity]
1445-
local r_swap = entity_index_try_get_any(entity_index, last_alive_entity) :: ecs_record_t
1446-
r_swap.dense = index_of_deleted_entity
1444+
local e_swap = dense_array[i_swap]
1445+
local r_swap = entity_index_try_get_any(entity_index, e_swap) :: ecs_record_t
1446+
1447+
r_swap.dense = dense
14471448
record.archetype = nil :: any
14481449
record.row = nil :: any
1449-
record.dense = index_of_last_alive_entity
1450+
record.dense = i_swap
1451+
1452+
dense_array[dense] = e_swap
1453+
dense_array[i_swap] = ECS_GENERATION_INC(entity)
1454+
end
14501455

1451-
dense_array[index_of_deleted_entity] = last_alive_entity
1452-
dense_array[index_of_last_alive_entity] = ECS_GENERATION_INC(entity)
1456+
local function world_exists(world, ecs_world_t, entity): boolean
1457+
return entity_index_try_get_any(world.entity_index, entity) ~= nil
14531458
end
14541459

14551460
local function world_contains(world: ecs_world_t, entity): boolean
@@ -2443,6 +2448,7 @@ World.has = world_has
24432448
World.target = world_target
24442449
World.parent = world_parent
24452450
World.contains = world_contains
2451+
World.exists = world_exists
24462452
World.cleanup = world_cleanup
24472453
World.each = world_each
24482454
World.children = world_children
@@ -2610,6 +2616,9 @@ export type World = {
26102616
--- Checks if the world contains the given entity
26112617
contains:(self: World, entity: Entity) -> boolean,
26122618

2619+
--- Checks if the entity exists
2620+
exists: (self: World, entity: Entity) -> boolean,
2621+
26132622
each: <T>(self: World, id: Id<T>) -> () -> Entity,
26142623

26152624
children: <T>(self: World, id: Id<T>) -> () -> Entity,

0 commit comments

Comments
 (0)