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

50 lines
No EOL
2.9 KiB
HTML
Raw 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>handler — Ludic</title>
<meta name="description" content="A named block the engine runs each frame during phase P.">
<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#structure">Program structure</a> <span>›</span> <span class="here">handler</span></div>
<div class="item-head">
<span class="kind-badge kind-keyword">keyword</span>
<h1 id="top">handler</h1>
</div>
<code class="sig">handler Name phase P { … }</code>
<div class="desc"><p>A <code>handler</code> is a named block of behavior that the engine runs automatically during a given <code>phase</code> — the unit that turns your data into a game. A handler with no query runs <b>once per phase tick</b>; a handler with a <code>@Queries</code> annotation (or an inline <code>for (…) in query […]</code> loop) runs its body <b>once per matching model instance</b>, with each property bound by name and <code>self()</code> giving the current instance. Handlers are registered implicitly just by being declared, and you can pause one at runtime with <code>disable Handler</code> and bring it back with <code>enable Handler</code>. Keep behavior in handlers and keep data plain in properties — that separation is the whole point.</p></div>
<div class="examples"><h2>Example</h2><pre data-lang="ludic">program Runner {
property Position { column: int = 0, row: int = 0 }
property Velocity { delta_x: int = 0, delta_y: int = 0 }
model Player { Position, Velocity }
handler Boot phase Start {
spawn Hero { Position { column: 0, row: 6 }; Velocity { delta_x: 1 } }
}
handler AdvancePositions phase Update {
for (position, velocity) in query [Position, Velocity, {Player}] {
position.column = position.column + velocity.delta_x
}
}
handler DrawWorld phase Render {
Screen.clear(Color.MidnightBlue)
for (position) in query [Position, {Player}] {
Screen.fill_rectangle(x: position.column * 16, y: position.row * 16, width: 16, height: 16, color: Color.LimeGreen)
}
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>