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

46 lines
No EOL
2.6 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>cancellable — Ludic</title>
<meta name="description" content="Marks an event whose listeners may veto it with cancel.">
<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">cancellable</span></div>
<div class="item-head">
<span class="kind-badge kind-keyword">keyword</span>
<h1 id="top">cancellable</h1>
</div>
<code class="sig">event cancellable Name { … }</code>
<div class="desc"><p><code>cancellable</code> marks an event as a <b>decision, not just a notification</b>. A plain event tells listeners something happened; a <code>cancellable</code> event is fired <b>before</b> an action so a listener can veto it by calling <code>cancel</code>. The caller reads the verdict back by using <code>emit</code> as an expression: it yields <code>0</code> when no listener vetoed and <code>1</code> when one did, so the action is applied only on <code>0</code>. This is the Bukkit / DOM <code>preventDefault</code> shape — observation becomes control — and a foreign mod vetoes the same way by setting the payload's trailing cancelled flag over the ABI.</p></div>
<div class="examples"><h2>Example</h2><pre data-lang="ludic">program CancellableExample {
event cancellable BeforeHurt { amount: int = 0 }
var current_health: int = 100
@On(BeforeHurt) handler AbsorbSmallHits { if amount &lt;= 5 { cancel } }
handler TakeDamage phase Update {
if Input.key() == 'h' {
if emit BeforeHurt(amount: 3) == 0 { current_health = current_health - 3 }
}
}
handler DrawWorld phase Render {
Screen.clear(Color.MidnightBlue)
Screen.draw_number(x: 4, y: 4, value: current_health, color: Color.Crimson, 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>