Skip to content

fix: recs setEntities now updates component if component exists #407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .changeset/chatty-kings-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
"@dojoengine/state": patch
"template-vite-ts": patch
"@dojoengine/core": patch
"@dojoengine/create-burner": patch
"@dojoengine/create-dojo": patch
"@dojoengine/predeployed-connector": patch
"@dojoengine/react": patch
"@dojoengine/sdk": patch
"@dojoengine/torii-client": patch
"@dojoengine/torii-wasm": patch
"@dojoengine/utils": patch
"@dojoengine/utils-wasm": patch
---

fix: recs `setEntities` now updates component if component exists
29 changes: 21 additions & 8 deletions packages/state/src/recs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import {
Component,
ComponentValue,
Entity,
getComponentValue,
hasComponent,
Metadata,
Schema,
setComponent,
updateComponent,
} from "@dojoengine/recs";
import {
Clause,
Expand Down Expand Up @@ -405,26 +408,23 @@ export const setEntities = async <S extends Schema>(

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

for (let key in entities) {
for (const key in entities) {
if (!Object.hasOwn(entities, key)) {
continue;
}

for (let componentName in entities[key]) {
for (const componentName in entities[key]) {
if (!Object.hasOwn(entities[key], componentName)) {
continue;
}
let recsComponent = Object.values(components).find(
(component) =>
component.metadata?.namespace +
"-" +
component.metadata?.name ===
componentName
const recsComponent = Object.values(components).find(
(component) => component.metadata?.name === componentName
);

if (recsComponent) {
try {
const rawValue = entities[key][componentName];

if (logging)
console.log(
`Raw value for ${componentName} on ${key}:`,
Expand All @@ -448,6 +448,19 @@ export const setEntities = async <S extends Schema>(
);
}

if (hasComponent(recsComponent, key as Entity)) {
updateComponent(
recsComponent,
key as Entity,
convertedValue as Partial<ComponentValue>,
getComponentValue(recsComponent, key as Entity)
);
if (logging)
console.log(
`Update component ${recsComponent.metadata?.name} on ${key}`
);
continue;
}
setComponent(recsComponent, key as Entity, convertedValue);

if (logging)
Expand Down
Loading