Skip to content

v0.4.0 - Have Heaven

Compare
Choose a tag to compare
@krispya krispya released this 19 Apr 17:10
· 30 commits to main since this release

I realized that the implementation of onAdd and onRemove was mixing two ideas together. One is trait events, when a trait is added, removed of changed on an entity. Another is query events, when a query has an entity added or removed from it. These are now two distinct events bringing a breaking change.

  • onAdd triggers when entity.add() is called after the initial value has been set on the trait.
  • onRemove triggers when entity.remove() is called, but before any data has been removed.
  • onChange triggers when an entity's trait value has been set with entity.set() or when it is manually flagged with entity.changed().
// Subscribe to Position additions
const unsub = world.onAdd(Position, (entity) => {
    console.log(`Entity ${entity} added position`);
});

// Subscribe to Position removals
const unsub = world.onRemove(Position, (entity) => {
    console.log(`Entity ${entity} removed position`);
});

// Subscribe to Position changes
const unsub = world.onChange(Position, (entity) => {
    console.log(`Entity ${entity} changed position`);
});

And then we have query events which trigger when an entity is added or removed from a query with no guarantees about data. This is stream of all modifications to a query, not to the addition or removal of traits to an entity. You can think of this an internal hook for advanced features that require observing queries like our useQuery hook.

// Subscribe to add or remove query events
// This triggers whenever a query is updated
// Return unsub function
const unsub = world.onQueryAdd([Position, Velocity], (entity) => {})
const unsub = world.onQueryRemove([Position, Velocity], (entity) => {})

What's Changed

  • 💥 core: onAdd is now a trait event, former onAdd is renamed onQueryAdd
  • 🐛 core: fix queryFirst types
  • 🐛 react: fix useTraitEffect, add tests