diff --git a/src/init.luau b/src/init.luau index 1b2acd7..8356b9b 100644 --- a/src/init.luau +++ b/src/init.luau @@ -674,7 +674,7 @@ export type Query = typeof(EmptyQuery) type CompatibleArchetype = { archetype: Archetype, indices: { number } } -local function replaceColumns(row, columns, ...) +local function replaceMult(row, columns, ...) for i, column in columns do column[row] = select(i, ...) end @@ -800,10 +800,9 @@ local function preparedQuery(compatibleArchetypes: { Archetype }, return queryNext end - local function replace(fn) - for _, compatibleArchetype in compatibleArchetypes do - local archetype = compatibleArchetype.archetype - local tr = compatibleArchetype.indices + local function replace(_, fn) + for i, archetype in compatibleArchetypes do + local tr = indices[i] local columns = archetype.columns for row in archetype.entities do @@ -835,7 +834,7 @@ local function preparedQuery(compatibleArchetypes: { Archetype }, for i = 1, queryLength do queryOutput[i] = columns[tr[i]][row] end - replaceColumns(row, columns, fn(unpack(queryOutput))) + replaceMult(row, columns, fn(unpack(queryOutput))) end end end diff --git a/tests/world.luau b/tests/world.luau index a167c8b..178650a 100644 --- a/tests/world.luau +++ b/tests/world.luau @@ -451,6 +451,26 @@ TEST("world", function() CHECK(count == 2) end + + do CASE "should replace component data" + local world = jecs.World.new() + local A = world:component() + local B = world:component() + local C = world:component() + + local e = world:entity() + world:set(e, A, 1) + world:set(e, B, true) + world:set(e, C, "hello ") + + world:query(A, B, C):replace(function(a, b, c) + return a * 2, not b, c.."world" + end) + + CHECK(world:get(e, A) == 2) + CHECK(world:get(e, B) == false) + CHECK(world:get(e, C) == "hello world") + end end) FINISH()