API Reference › Scenes & layers › exit
keyword

exit

on exit { … }

exit names the body of a scene's exit hook, written on exit { … }. It runs once when the scene is left — on a become Other, the leaving scene's on exit runs first, then the active scene switches and the new scene's on enter runs. Use it to tear down what the scene created so it does not leak into the next state: despawn the scene's models, hide a menu, save progress. Like on enter, it is a lifecycle hook rather than a phase and holds plain statements, not phase handlers.

Example

program ExitHookExample {
  property Position { column: int = 0, row: int = 0 }
  model Enemy { Position }

  scene Battle start {
    on enter { spawn Enemy { Position { column: 8, row: 4 } } }
    on exit {
      for (any_position) in query [Position, {Enemy}] { despawn self() }
    }
    layer Fight {
      handler LeaveBattle phase Update {
        if Input.key() == 'q' { become Overworld }
      }
      handler DrawBattle phase Render {
        Screen.clear(Color.Crimson)
        Screen.show()
      }
    }
  }

  scene Overworld {
    layer World {
      handler DrawWorld phase Render {
        Screen.clear(Color.MidnightBlue)
        Screen.show()
      }
    }
  }
}
← All symbols