Skip to content

Hide and show things at runtime ​

Goal ​

Press H to hide or show an actor while its collision and game logic keep running. Use a mesh actor named crate in your level.

Files you will edit ​

  • src/game.ts

Steps ​

  1. Add this system and append it to defineGame.systems:

    ts
    import { actors, visibility, type GameContext } from 'gameable';
    
    function toggleCrate(ctx: GameContext): void {
      if (!ctx.input.pressed('KeyH')) return;
      const crate = actors.named('crate');
      if (crate) visibility.setHidden(crate, !visibility.isHidden(crate));
    }
  2. To hide just the mesh, pass its component entity instead. Call these on your reveal/conceal event:

    ts
    const crate = actors.named('crate');
    if (crate) {
      visibility.setHidden(crate.component('mesh'), true);
      visibility.setHidden(crate, false);
      // Still hidden: the mesh has its own flag.
      visibility.setHidden(crate.component('mesh'), false);
    }

    Actor-wide and component flags are independent. actor.setHidden(hidden) is the actor-wide shorthand. visibility.isHidden(entity) reads effective game hiding, including the actor flag; reading the actor checks its actor-wide flag. Snapshots preserve both. Missing entities and stale handles are ignored.

Verify ​

Run npm run dev in the game. Press H twice: the crate disappears and returns. Walk into it while hidden: its collider still blocks you. Hiding does not stop simulation or actor ticks. Editor hide / showAll only affect the page session and cannot reveal something hidden by the game.

See also ​