47 lines
No EOL
2.6 KiB
HTML
47 lines
No EOL
2.6 KiB
HTML
<!doctype html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<title>function — Ludic</title>
|
||
<meta name="description" content="A function — reusable logic called positionally or with named arguments.">
|
||
<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">function</span></div>
|
||
<div class="item-head">
|
||
<span class="kind-badge kind-keyword">keyword</span>
|
||
<h1 id="top">function</h1>
|
||
</div>
|
||
<code class="sig">function name(a: T, b: T) -> R { … }</code>
|
||
<div class="desc"><p>A <code>function</code> declares a function: a reusable block of logic with typed parameters and a return type, written <code>-> R</code> (use <code>-> void</code> for one that returns nothing). Call it positionally, <code>heal(2, 10)</code>, or with named arguments, <code>heal(amount: 2, maximum: 10)</code>, which reads more clearly at the call site and is the house style. Functions live at program scope alongside handlers and may be called from any handler; they can <code>spawn</code>, run queries, and read program-scope state. Use them to factor out logic shared by several handlers so each handler stays short.</p></div>
|
||
|
||
<div class="examples"><h2>Example</h2><pre data-lang="ludic">program Healer {
|
||
property Health { current: int = 100, maximum: int = 100 }
|
||
model Player { Health }
|
||
|
||
function heal(amount: int, maximum: int) -> int {
|
||
let restored = amount * 2
|
||
if restored > maximum { return maximum }
|
||
return restored
|
||
}
|
||
|
||
handler Boot phase Start {
|
||
spawn Hero { Health { current: 10, maximum: 100 } }
|
||
}
|
||
|
||
handler Recover phase Update {
|
||
for (health) in query [Health, {Player}] {
|
||
health.current = heal(amount: health.current, maximum: health.maximum)
|
||
}
|
||
}
|
||
}</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> |