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

46 lines
No EOL
2.5 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>emit — Ludic</title>
<meta name="description" content="Fire an event, running every listener; as an expression it yields the veto flag.">
<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">emit</span></div>
<div class="item-head">
<span class="kind-badge kind-keyword">keyword</span>
<h1 id="top">emit</h1>
</div>
<code class="sig">emit Name(field: value)</code>
<div class="desc"><p><code>emit Name(field: value, …)</code> fires an event: it runs every <code>@On(Name)</code> listener in declaration order, passing the named payload fields (any you omit take the event's declared defaults). It desugars to a direct call into the event's generated function, so firing an event is as cheap as calling a function. For a <code>cancellable</code> event, <code>emit</code> can be used as an <b>expression</b> that yields the cancelled flag — <code>0</code> if no listener vetoed, <code>1</code> if one did — which is the "check the decision before acting" shape: apply the effect only when the emit returns <code>0</code>.</p></div>
<div class="examples"><h2>Example</h2><pre data-lang="ludic">program EmitExample {
event cancellable BeforeSpend { amount: int = 0 }
var coins: int = 50
@On(BeforeSpend) handler RejectOverdraft { if amount &gt; coins { cancel } }
handler TrySpend phase Update {
if Input.key() == 'b' {
if emit BeforeSpend(amount: 20) == 0 { coins = coins - 20 }
}
}
handler DrawWorld phase Render {
Screen.clear(Color.MidnightBlue)
Screen.draw_number(x: 4, y: 4, value: coins, 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>