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

51 lines
No EOL
2.8 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>machine — Ludic</title>
<meta name="description" content="An explicit state machine over an int var — dispatches on the store&#x27;s value.">
<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#control">Control flow</a> <span>›</span> <span class="here">machine</span></div>
<div class="item-head">
<span class="kind-badge kind-keyword">keyword</span>
<h1 id="top">machine</h1>
</div>
<code class="sig">machine store { state Name { … } }</code>
<div class="desc"><p>A <code>machine</code> turns an integer store into an explicit state machine, replacing brittle <code>if phase == N</code> chains. It reads the store — a named program-scope <code>var</code> is the modern choice — and dispatches to the matching <code>state</code> block; inside a state, <code>become Name</code> transitions to another state of the same machine. States number themselves by declaration order (the first is <code>0</code>, the next <code>1</code>, and so on), so you never write magic constants, though <code>state Name = expr</code> is accepted when a state needs a specific value. Because <code>become</code> compiles to a single store back into the <code>var</code>, the whole machine lowers to plain branches with no dispatch table. Place a <code>machine</code> inside a handler so it runs each frame.</p></div>
<div class="examples"><h2>Example</h2><pre data-lang="ludic">program TurnOrder {
enum Phase { KnightMenu, KnightResolve, EnemyTurn }
var battle_phase: int = Phase.KnightMenu
handler RunTurn phase Update {
machine battle_phase {
state KnightMenu {
if Input.key() == ' ' { become KnightResolve }
}
state KnightResolve {
become EnemyTurn
}
state EnemyTurn {
become KnightMenu
}
}
}
handler DrawWorld phase Render {
Screen.clear(Color.MidnightBlue)
Screen.draw_number(x: 8, y: 8, value: battle_phase, color: Color.Gold, scale: 2)
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>