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

45 lines
No EOL
2.7 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>event — Ludic</title>
<meta name="description" content="Declare a public event carrying a flat payload that listeners react to.">
<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#events">Events</a> <span>›</span> <span class="here">event</span></div>
<div class="item-head">
<span class="kind-badge kind-keyword">keyword</span>
<h1 id="top">event</h1>
</div>
<code class="sig">event Name { field: T = default }</code>
<div class="desc"><p><code>event</code> declares a public event — a named signal carrying a flat payload of typed fields (each with a default; the payload may be empty). Where lifecycle hooks are the closed reactions the author compiles in, events are the <b>open runtime surface a game exposes to mods</b>: an in-language listener registers with <code>@On(Name)</code> and reads the payload fields by name, and <code>emit Name(field: value, …)</code> fires every listener in declaration order as a direct call. It all desugars to a generated function — there is no interpreter and no dispatch table — and a program that declares no <code>event</code> compiles byte-for-byte as before. Mark an event <code>cancellable</code> when a listener should be able to veto the action it announces.</p></div>
<div class="examples"><h2>Example</h2><pre data-lang="ludic">program EventExample {
event EnemyDefeated { points: int = 0 }
var score: int = 0
@On(EnemyDefeated) handler AddScore { score = score + points }
@On(EnemyDefeated) handler PlayChime { Screen.status("enemy down") }
handler ScoreOnKey phase Update {
if Input.key() == 'k' { emit EnemyDefeated(points: 10) }
}
handler DrawWorld phase Render {
Screen.clear(Color.MidnightBlue)
Screen.draw_number(x: 4, y: 4, value: score, color: Color.Gold, scale: 1)
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>