ludic/kw-on.html
2026-09-17 22:10:03 +00:00

64 lines
No EOL
3.1 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>on enter / on exit — Ludic</title>
<meta name="description" content="Scene lifecycle hooks that fire as a scene becomes active or is left.">
<link rel="stylesheet" href="base.css">
<link rel="stylesheet" href="docs.css">
</head>
<body>
<header class="nav"><div class="wrap nav-in"><a class="brand" href="index.html"><span class="logo">L</span> Ludic</a><button class="nav-toggle" aria-label="Toggle menu" aria-expanded="false">☰</button><nav class="nav-links"><a href="index.html">Home</a><a href="api.html">API Reference</a><a class="nav-cta" href="https://git.workshopsoft.io/workshopsoft/ludic">Source ↗</a></nav></div></header>
<main class="wrap item">
<div class="crumbs"><a href="api.html">API Reference</a> <span>›</span> <a href="api.html#scenes">Scenes &amp; layers</a> <span>›</span> <span class="here">on enter / on exit</span></div>
<div class="item-head">
<span class="kind-badge kind-keyword">keyword</span>
<h1 id="top">on enter / on exit</h1>
</div>
<code class="sig">on enter { … } on exit { … }</code>
<div class="desc"><p><code>on</code> introduces a scene's lifecycle hooks: <code>on enter { … }</code> runs once when the scene becomes active, and <code>on exit { … }</code> runs once as it is left. They are hooks, <b>not phases</b> — the place for per-scene setup and teardown that has no equivalent in a mode register. The start scene's <code>on enter</code> fires at boot right after the <code>Start</code> phase; on a <code>become Other</code> the current scene's <code>on exit</code> runs, the active scene switches, then the new scene's <code>on enter</code> runs. Use <code>on enter</code> to spawn the models a scene needs and reset its counters, and <code>on exit</code> to tidy up before leaving.</p></div>
<div class="examples"><h2>Example</h2><pre data-lang="ludic">program SceneHookExample {
property Position { column: int = 0, row: int = 0 }
model Enemy { Position }
var elapsed_frames: int = 0
scene Overworld start {
layer World {
handler EnterBattle phase Update {
if Input.key() == 'b' { become Battle }
}
handler DrawWorld phase Render {
Screen.clear(Color.MidnightBlue)
Screen.show()
}
}
}
scene Battle {
on enter {
elapsed_frames = 0
spawn Enemy { Position { column: 8, row: 4 } }
}
on exit {
for (any_position) in query [Position, {Enemy}] { despawn self() }
}
layer Fight {
handler DrawBattle phase Render {
Screen.clear(Color.Crimson)
for (current_position) in query [Position, {Enemy}] {
Screen.fill_rectangle(x: current_position.column * 16, y: current_position.row * 16, width: 16, height: 16, color: Color.White)
}
Screen.show()
}
}
}
}</pre></div>
<a class="back" href="api.html">← All symbols</a>
</main>
<script src="ludic-highlight.js"></script>
<script>Ludic.highlightAll(); Ludic.installCards(); Ludic.flashTarget();</script>
</body></html>