Skip to content

Commit d8a9b0d

Browse files
committed
fix: recs setEntities now updates component if component exists
1 parent 0e92e46 commit d8a9b0d

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

.changeset/chatty-kings-nail.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
"@dojoengine/state": patch
3+
"template-vite-ts": patch
4+
"@dojoengine/core": patch
5+
"@dojoengine/create-burner": patch
6+
"@dojoengine/create-dojo": patch
7+
"@dojoengine/predeployed-connector": patch
8+
"@dojoengine/react": patch
9+
"@dojoengine/sdk": patch
10+
"@dojoengine/torii-client": patch
11+
"@dojoengine/torii-wasm": patch
12+
"@dojoengine/utils": patch
13+
"@dojoengine/utils-wasm": patch
14+
---
15+
16+
fix: recs `setEntities` now updates component if component exists

packages/state/src/recs/index.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import {
22
Component,
33
ComponentValue,
44
Entity,
5+
getComponentValue,
6+
hasComponent,
57
Metadata,
68
Schema,
79
setComponent,
10+
updateComponent,
811
} from "@dojoengine/recs";
912
import {
1013
Clause,
@@ -405,26 +408,23 @@ export const setEntities = async <S extends Schema>(
405408

406409
if (logging) console.log("Entities to set:", entities);
407410

408-
for (let key in entities) {
411+
for (const key in entities) {
409412
if (!Object.hasOwn(entities, key)) {
410413
continue;
411414
}
412415

413-
for (let componentName in entities[key]) {
416+
for (const componentName in entities[key]) {
414417
if (!Object.hasOwn(entities[key], componentName)) {
415418
continue;
416419
}
417-
let recsComponent = Object.values(components).find(
418-
(component) =>
419-
component.metadata?.namespace +
420-
"-" +
421-
component.metadata?.name ===
422-
componentName
420+
const recsComponent = Object.values(components).find(
421+
(component) => component.metadata?.name === componentName
423422
);
424423

425424
if (recsComponent) {
426425
try {
427426
const rawValue = entities[key][componentName];
427+
428428
if (logging)
429429
console.log(
430430
`Raw value for ${componentName} on ${key}:`,
@@ -448,6 +448,19 @@ export const setEntities = async <S extends Schema>(
448448
);
449449
}
450450

451+
if (hasComponent(recsComponent, key as Entity)) {
452+
updateComponent(
453+
recsComponent,
454+
key as Entity,
455+
convertedValue as Partial<ComponentValue>,
456+
getComponentValue(recsComponent, key as Entity)
457+
);
458+
if (logging)
459+
console.log(
460+
`Update component ${recsComponent.metadata?.name} on ${key}`
461+
);
462+
continue;
463+
}
451464
setComponent(recsComponent, key as Entity, convertedValue);
452465

453466
if (logging)

0 commit comments

Comments
 (0)