NateTheGreatt/bitECS

hasComponent issue when component was added through a Proxy

caryoscelus opened this issue · 0 comments

i'm using bitECS with Svelte 5, which uses Proxy objects for reactivity and i've run into issue where components got into reactive object and later on added to an entity. this isn't strictly necessary in my case so i found a workaround, but the issue was still annoying as i didn't even know where to start. addComponent was working without any warnings and i could see the component values being accessible for the entity, and yet hasComponent said the entity doesn't have it

essentially my code looks like

const world = createWorld();
const A = defineComponent({field: Types.i32});
const B = defineComponent({field: Types.i32});
const components = [A, B];
let selectedComponent = $state(components[0]); // this is where Svelte 5 reactivity comes in
const spawnEntity = () => {
  const e = addEntity(world);
  addComponent(world, selectedComponent, e);
  selectedComponent.field[e] = 2;
  console.log(hasComponent(world, A, e)); // false
  console.log(A.field[e] === 2); // true
};

(i will try to provide a minimal working example if needed)

the best behaviour for me would be for proxies to work transparently and for hasComponent to report component as present. second best behaviour would be for addComponent to throw error if it encounters a proxy object

as i have little knowledge of inner workings of bitECS or Proxies, i don't know how feasible would it be to implement either option, so i would understand if this isn't fixed, but in that case it should be documented

thanks for you work on the library