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

50 lines
No EOL
2.6 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>cancel — Ludic</title>
<meta name="description" content="Inside a listener, veto the cancellable event being emitted.">
<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">cancel</span></div>
<div class="item-head">
<span class="kind-badge kind-keyword">keyword</span>
<h1 id="top">cancel</h1>
</div>
<code class="sig">cancel</code>
<div class="desc"><p><code>cancel</code> is used inside an <code>@On</code> listener to <b>veto</b> the <code>cancellable</code> event currently being emitted. Calling it sets the payload's cancelled flag, and once the <code>emit</code> finishes running its listeners that flag comes back to the caller — <code>emit E(…)</code> used as an expression yields <code>1</code> when any listener cancelled and <code>0</code> otherwise. Guard it behind whatever condition should block the action; listeners that do not <code>cancel</code> simply observe. <code>cancel</code> is only meaningful in a listener for an event declared <code>cancellable</code>.</p></div>
<div class="examples"><h2>Example</h2><pre data-lang="ludic">program CancelExample {
event cancellable BeforeOpenDoor { key_count: int = 0 }
var keys: int = 0
var doors_opened: int = 0
@On(BeforeOpenDoor) handler RequireKey { if key_count &lt;= 0 { cancel } }
handler TryOpen phase Update {
if Input.key() == 'o' {
if emit BeforeOpenDoor(key_count: keys) == 0 {
doors_opened = doors_opened + 1
keys = keys - 1
}
}
}
handler DrawWorld phase Render {
Screen.clear(Color.MidnightBlue)
Screen.draw_number(x: 4, y: 4, value: doors_opened, 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>