Files
GhostEngine/doc/_site/docs/systems.html
Misaki d8a7b07624 feat(graphics): improve rendering pipeline and docs
- Refactor D3D12 backend and RenderGraph module
- Update graphics RHI and core rendering components
- Add Random.hlsl shader include
- Regenerate API documentation and update user guides
2026-03-27 22:23:44 +09:00

178 lines
7.9 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Systems Overview | GhostEngine </title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="title" content="Systems Overview | GhostEngine ">
<link rel="icon" href="../favicon.ico">
<link rel="stylesheet" href="../public/docfx.min.css">
<link rel="stylesheet" href="../public/main.css">
<meta name="docfx:navrel" content="../toc.html">
<meta name="docfx:tocrel" content="toc.html">
<meta name="docfx:rel" content="../">
<meta name="loc:inThisArticle" content="In this article">
<meta name="loc:searchResultsCount" content="{count} results for &quot;{query}&quot;">
<meta name="loc:searchNoResults" content="No results for &quot;{query}&quot;">
<meta name="loc:tocFilter" content="Filter by title">
<meta name="loc:nextArticle" content="Next">
<meta name="loc:prevArticle" content="Previous">
<meta name="loc:themeLight" content="Light">
<meta name="loc:themeDark" content="Dark">
<meta name="loc:themeAuto" content="Auto">
<meta name="loc:changeTheme" content="Change theme">
<meta name="loc:copy" content="Copy">
<meta name="loc:downloadPdf" content="Download PDF">
<script type="module" src="./../public/docfx.min.js"></script>
<script>
const theme = localStorage.getItem('theme') || 'auto'
document.documentElement.setAttribute('data-bs-theme', theme === 'auto' ? (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light') : theme)
</script>
</head>
<body class="tex2jax_ignore" data-layout="" data-yaml-mime="">
<header class="bg-body border-bottom">
<nav id="autocollapse" class="navbar navbar-expand-md" role="navigation">
<div class="container-xxl flex-nowrap">
<a class="navbar-brand" href="../index.html">
<img id="logo" class="svg" src="../logo.svg" alt="GhostEngine">
GhostEngine
</a>
<button class="btn btn-lg d-md-none border-0" type="button" data-bs-toggle="collapse" data-bs-target="#navpanel" aria-controls="navpanel" aria-expanded="false" aria-label="Toggle navigation">
<i class="bi bi-three-dots"></i>
</button>
<div class="collapse navbar-collapse" id="navpanel">
<div id="navbar">
<form class="search" role="search" id="search">
<i class="bi bi-search"></i>
<input class="form-control" id="search-query" type="search" disabled placeholder="Search" autocomplete="off" aria-label="Search">
</form>
</div>
</div>
</div>
</nav>
</header>
<main class="container-xxl">
<div class="toc-offcanvas">
<div class="offcanvas-md offcanvas-start" tabindex="-1" id="tocOffcanvas" aria-labelledby="tocOffcanvasLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="tocOffcanvasLabel">Table of Contents</h5>
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" data-bs-target="#tocOffcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<nav class="toc" id="toc"></nav>
</div>
</div>
</div>
<div class="content">
<div class="actionbar">
<button class="btn btn-lg border-0 d-md-none" type="button" data-bs-toggle="offcanvas" data-bs-target="#tocOffcanvas" aria-controls="tocOffcanvas" aria-expanded="false" aria-label="Show table of contents">
<i class="bi bi-list"></i>
</button>
<nav id="breadcrumb"></nav>
</div>
<article data-uid="">
<h1 id="systems-overview">Systems Overview</h1>
<p>Systems are where all your ECS logic lives. They interact with data by iterating over entities that match an <code>EntityQuery</code>.</p>
<h2 id="the-isystem-interface">The <code>ISystem</code> Interface</h2>
<p>At its core, a system implements <code>ISystem</code>, which provides three lifecycle methods:</p>
<pre><code class="lang-csharp">public interface ISystem
{
void Initialize(ref readonly SystemAPI systemAPI);
void Update(ref readonly SystemAPI systemAPI);
void Cleanup(ref readonly SystemAPI systemAPI);
}
</code></pre>
<p>The <code>SystemAPI</code> struct provides access to the <code>World</code> and <code>TimeData</code> for the current frame.</p>
<h2 id="systembase"><code>SystemBase</code></h2>
<p>For most gameplay code, you should inherit from the abstract class <code>SystemBase</code>, which provides convenient wrappers and handles &quot;Should Update&quot; logic automatically.</p>
<pre><code class="lang-csharp">public class MovementSystem : SystemBase
{
private Identifier&lt;EntityQuery&gt; _query;
protected override void OnInitialize(ref readonly SystemAPI systemAPI)
{
// Build a query for entities that have both Position and Velocity
_query = new QueryBuilder()
.WithAllRW&lt;Position&gt;()
.WithAll&lt;Velocity&gt;()
.Build(World);
// Tell the system base to ONLY run OnUpdate if this query has at least 1 entity
RequireQueryForUpdate(_query);
}
protected override void OnUpdate(ref readonly SystemAPI systemAPI)
{
float dt = systemAPI.Time.DeltaTime;
ref var query = ref World.ComponentManager.GetEntityQueryReference(_query);
foreach (var chunk in query.GetChunkIterator())
{
var positions = chunk.GetComponentDataRW&lt;Position&gt;();
var velocities = chunk.GetComponentData&lt;Velocity&gt;();
for (int i = 0; i &lt; chunk.EntityCount; i++)
{
positions[i].Value += velocities[i].Value * dt;
}
}
}
}
</code></pre>
<h3 id="automatic-update-triggers">Automatic Update Triggers</h3>
<p>Notice the <code>RequireQueryForUpdate(_query)</code> call. <code>SystemBase</code> will skip calling <code>OnUpdate</code> if the query results are completely empty, saving processing time. <code>SystemBase</code> also invokes <code>OnStartRunning</code> and <code>OnStopRunning</code> when the system transitions between having matching entities and not.</p>
<h2 id="system-execution-order">System Execution Order</h2>
<p>By default, systems execute in the order they are added to a <code>SystemGroup</code>. To enforce execution order regardless of initialization sequence, use the <code>[UpdateAfter]</code> and <code>[UpdateBefore]</code> attributes.</p>
<pre><code class="lang-csharp">[UpdateAfter(typeof(PhysicsSystem))]
[UpdateBefore(typeof(RenderSystem))]
public class MovementSystem : SystemBase
{
// ...
}
</code></pre>
<p>When you call <code>group.SortSystems()</code>, the <code>SystemGroup</code> uses Kahn's algorithm to perform a topological sort and resolves these dependencies. If circular dependencies are detected, the engine will throw an exception.</p>
<h2 id="system-groups">System Groups</h2>
<p><code>SystemGroup</code>s implement <code>ISystem</code> themselves, meaning you can nest groups within groups to build complex hierarchical update loops (e.g. <code>FixedUpdateGroup</code>, <code>PresentationGroup</code>). GhostEngine provides a <code>DefaultSystemGroup</code> that is automatically instantiated by the <code>SystemManager</code>.</p>
</article>
<div class="contribution d-print-none">
</div>
<div class="next-article d-print-none border-top" id="nextArticle"></div>
</div>
<div class="affix">
<nav id="affix"></nav>
</div>
</main>
<div class="container-xxl search-results" id="search-results"></div>
<footer class="border-top text-secondary">
<div class="container-xxl">
<div class="flex-fill">
<span>Made with <a href="https://dotnet.github.io/docfx">docfx</a></span>
</div>
</div>
</footer>
</body>
</html>