Compare commits

...

3 commits

Author SHA1 Message Date
PepeElToro41
8568277123
Merge f912866fcb into 0874e426af 2025-08-21 19:54:16 -04:00
dai
0874e426af
Fix bulk_insert with moving archetypes (#272)
Some checks failed
analysis / Run Luau Analyze (push) Has been cancelled
deploy-docs / build (push) Has been cancelled
publish-npm / publish (push) Has been cancelled
unit-testing / Run Luau Tests (push) Has been cancelled
deploy-docs / Deploy (push) Has been cancelled
* Fix bulk_insert with moving archetypes

* Reword message
2025-08-21 21:32:11 +02:00
PepeElToro41
f912866fcb add way to check preregistered id creations after world creation 2025-07-29 14:18:15 -06:00
2 changed files with 47 additions and 6 deletions

View file

@ -126,6 +126,8 @@ type world = {
max_component_id: number, max_component_id: number,
max_archetype_id: number, max_archetype_id: number,
start_component_id: number,
start_tag_id: number,
observable: Map<i53, Map<i53, { Observer }>>, observable: Map<i53, Map<i53, { Observer }>>,
@ -163,6 +165,8 @@ export type World = {
max_component_id: number, max_component_id: number,
max_archetype_id: number, max_archetype_id: number,
start_component_id: number,
start_tag_id: number,
observable: Map<Id, Map<Id, { Observer }>>, observable: Map<Id, Map<Id, { Observer }>>,
@ -760,6 +764,12 @@ local function ECS_ID_IS_WILDCARD(e: i53): boolean
return first == EcsWildcard or second == EcsWildcard return first == EcsWildcard or second == EcsWildcard
end end
local function get_max_ids_difference(world: World): (number, number)
local diff_components = world.start_component_id - ecs_max_component_id
local diff_tags = world.start_tag_id - ecs_max_tag_id
return diff_components, diff_tags
end
local function id_record_get(world: World, id: Entity): ComponentRecord? local function id_record_get(world: World, id: Entity): ComponentRecord?
local component_index = world.component_index local component_index = world.component_index
local idr: ComponentRecord = component_index[id] local idr: ComponentRecord = component_index[id]
@ -2110,15 +2120,13 @@ local function ecs_bulk_insert(world: world, entity: i53, ids: { i53 }, values:
local dst_types = ids local dst_types = ids
local to = archetype_ensure(world, dst_types) local to = archetype_ensure(world, dst_types)
new_entity(entity, r, to) new_entity(entity, r, to)
local row = r.row
local columns_map = to.columns_map
for i, id in ids do for i, id in ids do
local value = values[i] local value = values[i]
local cdr = component_index[id] local cdr = component_index[id]
local on_add = cdr.on_add local on_add = cdr.on_add
if value then if value then
columns_map[id][row] = value r.archetype.columns_map[id][r.row] = value
if on_add then if on_add then
on_add(entity, id, value :: any) on_add(entity, id, value :: any)
end end
@ -2148,12 +2156,10 @@ local function ecs_bulk_insert(world: world, entity: i53, ids: { i53 }, values:
end end
local to = archetype_ensure(world, dst_types) local to = archetype_ensure(world, dst_types)
local columns_map = to.columns_map
if from ~= to then if from ~= to then
entity_move(entity_index, entity, r, to) entity_move(entity_index, entity, r, to)
end end
local row = r.row
for i, set in emplaced do for i, set in emplaced do
local id = ids[i] local id = ids[i]
@ -2164,7 +2170,7 @@ local function ecs_bulk_insert(world: world, entity: i53, ids: { i53 }, values:
local on_add = idr.on_add local on_add = idr.on_add
if value ~= nil then if value ~= nil then
columns_map[id][row] = value r.archetype.columns_map[id][r.row] = value
local on_change = idr.on_change local on_change = idr.on_change
local hook = if set then on_change else on_add local hook = if set then on_change else on_add
if hook then if hook then
@ -2263,6 +2269,9 @@ local function world_new()
archetype_index = archetype_index, archetype_index = archetype_index,
max_archetype_id = 0, max_archetype_id = 0,
max_component_id = ecs_max_component_id, max_component_id = ecs_max_component_id,
start_component_id = ecs_max_component_id,
start_tag_id = ecs_max_tag_id,
observable = observable, observable = observable,
signals = signals, signals = signals,
@ -3365,6 +3374,7 @@ return {
pair_first = ecs_pair_first :: <P, O>(world: World, pair: Pair<P, O>) -> Id<P>, pair_first = ecs_pair_first :: <P, O>(world: World, pair: Pair<P, O>) -> Id<P>,
pair_second = ecs_pair_second :: <P, O>(world: World, pair: Pair<P, O>) -> Id<O>, pair_second = ecs_pair_second :: <P, O>(world: World, pair: Pair<P, O>) -> Id<O>,
entity_index_get_alive = entity_index_get_alive, entity_index_get_alive = entity_index_get_alive,
get_max_ids_difference = get_max_ids_difference,
archetype_append_to_records = archetype_append_to_records, archetype_append_to_records = archetype_append_to_records,
id_record_ensure = id_record_ensure :: (World, Id) -> ComponentRecord, id_record_ensure = id_record_ensure :: (World, Id) -> ComponentRecord,

View file

@ -331,6 +331,37 @@ TEST("bulk", function()
CHECK(world:has(e, c1, c2, c3)) CHECK(world:has(e, c1, c2, c3))
CHECK(count == 3) CHECK(count == 3)
end end
do CASE "Should bulk add with hooks moving archetypes without previous"
local world = jecs.world()
local c1, c2, c3 = world:component(), world:component(), world:component()
world:added(c1, function(e)
world:set(e, c3, "hello")
end)
local e = world:entity()
jecs.bulk_insert(world, e, {c1,c2}, {true, 123})
CHECK(world:get(e, c1) == true)
CHECK(world:get(e, c2) == 123)
CHECK(world:get(e, c3) == "hello")
end
do CASE "Should bulk add with hooks moving archetypes with previous"
local world = jecs.world()
local c1, c2, c3 = world:component(), world:component(), world:component()
world:added(c1, function(e)
world:set(e, c3, "hello")
end)
local e = world:entity()
world:add(e, world:entity())
jecs.bulk_insert(world, e, {c1,c2}, {true, 123})
CHECK(world:get(e, c1) == true)
CHECK(world:get(e, c2) == 123)
CHECK(world:get(e, c3) == "hello")
end
end) end)
TEST("repro", function() TEST("repro", function()