System Ordering
This page explains how to define system execution order using attributes and groups.
Overview
Ghost.Entities systems are managed through SystemManager and grouped by SystemGroup.
- Add systems to a group.
- Declare relative ordering with attributes.
- Call
SortSystems()before initialization/update loops.
Basic Example
internal class TestSystemA : SystemBase
{
protected override void OnInitialize(ref readonly SystemAPI systemAPI)
{
Console.WriteLine("TestSystemA Initialized");
}
}
[UpdateAfter(typeof(TestSystemA))]
internal class TestSystemB : SystemBase
{
protected override void OnInitialize(ref readonly SystemAPI systemAPI)
{
Console.WriteLine("TestSystemB Initialized");
}
}
Registration and sort:
var world = World.Create();
var group = world.SystemManager.GetSystem<DefaultSystemGroup>();
group.AddSystem<TestSystemB>();
group.AddSystem<TestSystemA>();
group.SortSystems();
Even though TestSystemB is added first, [UpdateAfter(typeof(TestSystemA))] ensures TestSystemA runs earlier after sorting.
Initialization/update execution is driven by the runtime loop (via internal system manager entry points).
When to Use UpdateAfter and UpdateBefore
Use ordering attributes when one system depends on side effects from another system in the same update phase.
UpdateAfter(typeof(X)): this system must run afterX.UpdateBefore(typeof(X)): this system must run beforeX.
Prefer expressing only required dependencies. Over-constraining can reduce scheduler flexibility.
Practical Guidance
- Keep systems small and focused on one responsibility.
- Place related systems in the same
SystemGroupfor predictable ordering. - Sort once after registration changes (startup/bootstrap phase).
- Use clear naming to make ordering intent obvious.
For API details, see generated docs for SystemBase, SystemGroup, SystemManager, UpdateAfterAttribute, and UpdateBeforeAttribute in doc/api/.