Constconst self = useSelf(() => ({
pos: new LEA.Vector2(400, 300),
vel: new LEA.Vector2(180, 0),
health: 100,
hurt(dmg: number) {
this.health -= dmg;
if (this.health <= 0) {
// structural change → useState trigger
isDead.set(true);
}
}
}));
useTick(dt => {
self.pos.add(self.vel.clone().scale(dt));
});
Creates a stable "god object" that holds most of an entity's mutable state and logic. The initializer runs only once — returned object is the same reference forever.
This hook MUST be called at the top level of a component function — never inside loops, conditions, nested functions, or callbacks. The call order of all hooks is strictly fixed across every refresh; reordering calls will corrupt internal state.
Recommended 2025–2026 pattern: Put almost everything here (position, velocity, health, AI state, methods…). Use
useStateonly for rare structural triggers.