diff --git a/jecs.luau b/jecs.luau index b6169b3..eef2e2f 100755 --- a/jecs.luau +++ b/jecs.luau @@ -2050,7 +2050,9 @@ local function ecs_bulk_insert(world: world, entity: i53, ids: { i53 }, values: local from = r.archetype local component_index = world.component_index if not from then - local dst_types = ids + local dst_types = table.clone(ids) + table.sort(dst_types) + local to = archetype_ensure(world, dst_types) new_entity(entity, r, to) local ROOT_ARCHETYPE = world.ROOT_ARCHETYPE diff --git a/test/tests.luau b/test/tests.luau index 76e8181..dfc0d84 100755 --- a/test/tests.luau +++ b/test/tests.luau @@ -330,6 +330,20 @@ TEST("bulk", function() CHECK(world:get(e, c2) == 123) CHECK(world:get(e, c3) == "hello") end + + do CASE "Should ensure archetype ids are sorted" + local world = jecs.world() + local c1, c2, c3 = world:component(), world:component(), world:component() + + local e = world:entity() + jecs.bulk_insert(world, e, { c2, c1 }, { 2, 1 }) + jecs.bulk_insert(world, e, { c1 }, { 1 }) + world:set(e, c3, 3) + + CHECK(world:get(e, c1) == 1) + CHECK(world:get(e, c2) == 2) + CHECK(world:get(e, c3) == 3) + end end) TEST("repro", function()