@kayelaa/canvas API - v0.2.15
    Preparing search index...

    Variable useSelfConst

    useSelf: <T>(init: () => T) => T = ...

    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 useState only for rare structural triggers.

    Type Declaration

      • <T>(init: () => T): T
      • Type Parameters

        • T

          Type of your state/logic object

        Parameters

        • init: () => T

          Factory called once — returns your stateful object

        Returns T

        The same object instance on every render

    const 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));
    });