You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
updateEach works ergonomically by generating a snapshot that gets passed into each callback and then the snapshots are written back to the store. This allows the store to be designed in ways that are optimized for storage, for example a structure of arrays, while still having an object-oriented interface for mutation.
// Position and Velocity traits are updated as expected from the mutationworld.query(Position,Velocity).updateEach(([position,velocity])=>{position.x+=velocity.x*delta;position.y+=velocity.y*delta;});
However, there is an unintuitive interaction that occurs currently where you can use the entity's set method to set the trait values from inside the loop, and then the untouched snapshot gets written back to the store, overwriting any changes made via the entity set method.
// This will confusingly do nothingworld.query(Position,Velocity).updateEach(([position,velocity],entity)=>{entity.set(Position,(prev)=>({x: prev.x+velocity.x*delta,y: prev.y+velocity.y*delta,}));});
The proper fix, I think, is to skip writing the snapshot back to the store if an entity set is called for that trait. This will require setting dirty flags, which is already supported with some refactoring.
The text was updated successfully, but these errors were encountered:
krispya
changed the title
Don't override an entity set inside of updateEach
Don't override an entity set inside of updateEachJan 2, 2025
updateEach
works ergonomically by generating a snapshot that gets passed into each callback and then the snapshots are written back to the store. This allows the store to be designed in ways that are optimized for storage, for example a structure of arrays, while still having an object-oriented interface for mutation.However, there is an unintuitive interaction that occurs currently where you can use the entity's
set
method to set the trait values from inside the loop, and then the untouched snapshot gets written back to the store, overwriting any changes made via the entityset
method.The proper fix, I think, is to skip writing the snapshot back to the store if an entity
set
is called for that trait. This will require setting dirty flags, which is already supported with some refactoring.The text was updated successfully, but these errors were encountered: