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

44 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>query — Ludic</title>
<meta name="description" content="Match every model instance carrying all the listed properties.">
<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#ecs">Entities &amp; the ECS</a> <span>›</span> <span class="here">query</span></div>
<div class="item-head">
<span class="kind-badge kind-keyword">keyword</span>
<h1 id="top">query</h1>
</div>
<code class="sig">query [PropA, PropB, {Tag}]</code>
<div class="desc"><p>A <code>query</code> selects every live model instance that carries <b>all</b> the listed properties, and is the core way handlers reach data. Use it in a <code>for (…) in query [...]</code> loop, where you write one loop variable per bound property in declaration order; each pass binds those properties for one matching instance and <code>self()</code> gives that instance. A <code>{Model}</code> term in tag position filters by model kind without binding a variable, so <code>query [Position, {Player}]</code> walks only players. Add a <code>where</code> clause to filter on field values (<code>where health.current > 0</code>). Matching is lazy and re-checked per instance as the loop reaches each id, not snapshotted, so spawns and despawns during the loop follow well-defined rules.</p></div>
<div class="examples"><h2>Example</h2><pre data-lang="ludic">program Collisions {
property Position { column: int = 0, row: int = 0 }
property Health { current: int = 100, maximum: int = 100 }
model Enemy { Position, Health }
handler Boot phase Start {
spawn Grunt { Position { column: 5, row: 5 }; Health { current: 10 } }
}
handler DrawWorld phase Render {
Screen.clear(Color.MidnightBlue)
for (position, health) in query [Position, Health, {Enemy}] where health.current &gt; 0 {
Screen.fill_rectangle(x: position.column * 16, y: position.row * 16, width: 16, height: 16, color: Color.Crimson)
}
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>