Skip to content
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

Don't override an entity set inside of updateEach #37

Open
krispya opened this issue Jan 2, 2025 · 0 comments
Open

Don't override an entity set inside of updateEach #37

krispya opened this issue Jan 2, 2025 · 0 comments

Comments

@krispya
Copy link
Member

krispya commented Jan 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.

// Position and Velocity traits are updated as expected from the mutation
world.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 nothing
world.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.

@krispya krispya changed the title Don't override an entity set inside of updateEach Don't override an entity set inside of updateEach Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant