Class SystemsRunner
Namespace: KenseiECS
Manages a group of systems and executes them in declared order. Systems can implement any combination of IInitSystem, IRunSystem, IDestroySystem.
Supports nesting — a SystemsRunner can be added to another SystemsRunner. An inline group (AddGroup(name, runner), or Add(runner) without a name) runs as part of the parent's Run(). A phase (AddPhase(name, runner); the obsolete Add(runner, name) does the same) is run separately (FixedUpdate/LateUpdate): the parent Init()/Destroy() cascade to it, but it is run only via GetRunner(name).Run(). A runner can be registered only once: in one slot of one parent. A second registration, in the same parent or another one, and adding a runner to itself or to a runner nested in it throw InvalidOperationException in every build. Only the root runner's Run() advances the world tick; each runner cleans its own OneFrame components at the end of its own Run().
Supports named systems — enable/disable at runtime by name. Names are compared ordinally (case-sensitive: "sim" and "Sim" are different names). An unknown name throws under KENSEI_DEBUG and is silently ignored in release (SetActive does nothing, IsActive returns false, GetRunner returns null), so keep names in constants.
Lifecycle contract: Init runs each IInitSystem once, in registration order. If one throws, Init rolls back: the systems whose Init completed before the failure and that implement IDestroySystem are destroyed in reverse order (a nested runner that completed its Init is rolled back system by system the same way; one that failed has rolled itself back), the runner stays uninitialized, and the original exception is rethrown — or, if the rollback throws too, one flat AggregateException holds the original exception first, then every rollback exception, at any nesting depth, in the order they were thrown. Systems without IInitSystem are not destroyed by the rollback. The next Init starts over from the first system. Run always cleans OneFrame components, even when a system throws. Destroy runs IDestroySystem in reverse registration order, is a no-op on an uninitialized runner, and makes the runner re-initializable. A Destroy that throws does not stop the others: every system is destroyed, then the exception is rethrown (several: one flat AggregateException, in the order they were thrown).
Every run system gets a Unity ProfilerMarker; under KENSEI_DEBUG per-system timings are also recorded and exposed through GetSystemInfo.
Usage:
var shared = new SharedData();
shared.Add(new GameConfig());
var root = new SystemsRunner(world, shared)
.Add(new MovementSystem(), "movement")
.Add(new DamageSystem())
.AddGroup("combat", new SystemsRunner(world).Add(new HitSystem()))
.AddPhase("fixed", new SystemsRunner(world).Add(new PhysicsSystem()))
.OneFrame<DamageEvent>();
root.Init();
// Update: root.Run();
// FixedUpdate: root.GetRunner("fixed").Run();
// on shutdown: root.Destroy();public class SystemsRunner : IInitSystem, IRunSystem, IDestroySystem, ISystemInheritance
Implements
IInitSystem, IRunSystem, IDestroySystem, ISystem
Inherited Members
object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()
Constructors
SystemsRunner(World, SharedData)
Create a runner for the given world. Without SharedData a new empty one is created; a nested runner created this way uses its parent's SharedData once added (the root's when nested deeper, whatever order the runners were added in).
public SystemsRunner(World world, SharedData shared = null)Parameters
world World
shared SharedData
Properties
IsEnabled
Whether this runner executes its systems in Run. For a nested runner it is set by the parent's SetActive(name) or SetActive(index), or by the runner's own SetActive(bool): for a phase it is the phase's own flag, for an inline group it always equals the parent's run slot (GetSystemInfo(index).IsEnabled and IsActive(name)). A root runner is enabled unless SetActive(false) is called on it. A disabled runner does not run its systems; its OneFrame components are kept until it runs again, except those registered with OneFrame<T>(cleanWhenDisabled: true).
public bool IsEnabled { get; }Property Value
IsInitialized
True after Init completed for every IInitSystem.
public bool IsInitialized { get; }Property Value
Shared
Shared data passed to systems in Init: the one this runner was constructed with, otherwise its parent's (read through the parent at every call, so a runner added to a group before the group joined its own parent reports the root's), or, for a root runner constructed without one, the empty SharedData it created.
public SharedData Shared { get; }Property Value
SystemCount
Number of registered systems, including nested runners.
public int SystemCount { get; }Property Value
World
World this runner drives.
public World World { get; }Property Value
Methods
Add(ISystem, string)
Register a system. Automatically detects which interfaces it implements. Optional name for runtime enable/disable (SetActive/IsActive) and for the Profiler. A nested runner added without a name becomes an unnamed inline group. A nested runner passed here with a name as an ISystem (not statically typed as SystemsRunner) becomes a separate phase, like AddPhase; use AddPhase or AddGroup to say which one is meant. A nested runner constructed without SharedData inherits the parent's. A nested runner that is already registered (here or in another runner), this runner itself, or a runner this one is nested in throws InvalidOperationException in every build. Under KENSEI_DEBUG a name already used in this runner throws. Returns this for fluent chaining.
public SystemsRunner Add(ISystem system, string name = null)Parameters
system ISystem
name string
Returns
Add(SystemsRunner, string)
Register a nested runner under a name as a separate phase — same as AddPhase(name, runner). Kept for compatibility: next to AddGroup(name, runner) it reads like an inline group, but its systems do not run in this runner's Run, only via GetRunner(name).Run(). With a null name it adds an unnamed inline group, like Add(runner).
[Obsolete("With a name, Add(runner, name) creates a separate phase that this runner's Run does not run; use AddPhase(name, runner) for a phase, AddGroup(name, runner) for a named inline group, or Add(runner) for an unnamed one")]
public SystemsRunner Add(SystemsRunner runner, string name)Parameters
runner SystemsRunner
name string
Returns
AddGroup(string, SystemsRunner)
Register a nested runner as a named inline group: it runs as part of this runner's Run, at its registration position, like an unnamed Add(runner). The name is used by SetActive(name)/IsActive(name)/IndexOf(name)/GetRunner(name), by the group's ProfilerMarker and by SystemInfo.Name. Disabling the group disables its slot here and the group's IsEnabled together. The slot starts as the group is at registration: a group switched off with SetActive(false) before AddGroup is added disabled (so is an unnamed Add(runner)). Do not Run() the group separately — it already runs inside this runner. A runner can be registered only once: to run the same systems at two points of the pipeline, register two runners. One that is already registered (here or in another runner), this runner itself, or a runner this one is nested in throws InvalidOperationException in every build. Under KENSEI_DEBUG a name already used in this runner throws.
public SystemsRunner AddGroup(string name, SystemsRunner runner)Parameters
name string
runner SystemsRunner
Returns
AddPhase(string, SystemsRunner)
Register a nested runner as a separate phase (FixedUpdate/LateUpdate). The phase is not part of this runner's Run: drive it with GetRunner(name).Run(), which runs its systems and cleans its OneFrame components without advancing the tick. Init()/Destroy() cascade to it. SetActive(name)/IsActive(name) toggle and read the phase's own IsEnabled. The obsolete Add(runner, name) does the same. A runner can be registered only once: one that is already registered (here or in another runner), this runner itself, or a runner this one is nested in throws InvalidOperationException in every build. Under KENSEI_DEBUG a name already used in this runner throws.
public SystemsRunner AddPhase(string name, SystemsRunner runner)Parameters
name string
runner SystemsRunner
Returns
DelHere<T>()
Remove all components of type T at this point of the pipeline. Systems registered before DelHere see the components; systems after it do not.
public SystemsRunner DelHere<T>() where T : struct, IComponentReturns
Type Parameters
T
Destroy()
Call once on shutdown. Invokes Destroy() on all IDestroySystem in reverse registration order. No-op if Init has not completed: a failed Init has already destroyed the systems it initialized, and systems without IInitSystem are destroyed only here, after a completed Init. If a Destroy throws, the other systems are still destroyed, as described for Destroy(World).
public void Destroy()Destroy(World)
IDestroySystem entry point, used when this runner is nested in another. Invokes Destroy() on all IDestroySystem in reverse registration order, including systems without IInitSystem. No-op if Init has not completed (after a failed Init too: its rollback has destroyed what it initialized); afterwards the runner can be initialized again. If the Destroy of a system throws, every other system is still destroyed, nested runners included; then the exception is rethrown as is, or, when more than one Destroy threw, one flat AggregateException holds them all in the order they were thrown (never an AggregateException of a nested runner inside it), as for the rollback of Init. A nested runner of a class derived from SystemsRunner that implements IDestroySystem.Destroy again is destroyed through that method, called once, like any system; the systems of that runner are destroyed also when the method throws before it calls this one, or never calls it.
public void Destroy(World world)Parameters
world World
GetRunner(string)
Get a named nested SystemsRunner: a phase (to drive it from FixedUpdate/LateUpdate) or a named inline group (for inspection or SetActive(bool); it already runs inside this runner's Run). The name is compared ordinally (case-sensitive). Unknown names return null (throw under KENSEI_DEBUG).
public SystemsRunner GetRunner(string name)Parameters
name string
Returns
GetSystemInfo(int)
Information about the system registered at the given position.
public SystemsRunner.SystemInfo GetSystemInfo(int index)Parameters
index int
Returns
IndexOf(ISystem)
Position of the system in registration order (the index used by GetSystemInfo and SetActive(int)), or -1 when it is not registered in this runner (nested runners are not searched). Compares by reference.
public int IndexOf(ISystem system)Parameters
system ISystem
Returns
IndexOf(string)
Position of the system with the given name, in registration order; -1 when there is none. Finds any registered system, including one without Run (Init/Destroy only), which SetActive and IsActive cannot toggle. For a run system, an inline group or a phase, an explicit registration name is resolved exactly as SetActive(string)/IsActive(string) resolve it, so SetActive(IndexOf(name), x) and SetActive(name, x) act on the same system. Only when no system was registered under that name does it fall back to the first unnamed system whose type name (SystemInfo.Name) matches. Names are case-sensitive (ordinal). Nested runners are not searched.
public int IndexOf(string name)Parameters
name string
Returns
Init()
Call once at startup. Invokes Init() on all IInitSystem in registration order; if one throws, rolls back as described for Init(World, SharedData).
public void Init()Init(World, SharedData)
IInitSystem entry point, used when this runner is nested in another. Invokes Init() on all IInitSystem in registration order; no-op if already initialized. If an Init throws, Init rolls back: the systems whose Init completed before the failure and that implement IDestroySystem are destroyed in reverse order (a nested runner that completed its Init is rolled back system by system; one that failed has already rolled itself back). The failed system, the systems after it and systems without IInitSystem are not destroyed. The runner stays uninitialized and the exception is rethrown as is; if a rollback Destroy throws too, one AggregateException is thrown instead, with the original exception first, then the rollback exceptions in the order they were thrown, including those of nested runners (never an AggregateException inside another). The next Init starts from the first system. Under KENSEI_DEBUG throws if world or an explicit SharedData differ from the runner's own.
public void Init(World world, SharedData shared)Parameters
world World
shared SharedData
IsActive(string)
Check if a named run system, inline group or phase is enabled. The name is compared ordinally (case-sensitive). An unknown name, or the name of a system without Run, returns false in release and throws under KENSEI_DEBUG.
public bool IsActive(string name)Parameters
name string
Returns
OneFrame<T>()
Register a one-frame component type. All components of this type are removed at the end of each Run() call, after every system. Producers must therefore run before consumers within the frame; use DelHere for a cleanup point in the middle of the pipeline. Cleanup belongs to this runner's Run: while the runner is disabled (or its slot in the parent is), components of this type are kept until it runs again. Same as OneFrame<T>(cleanWhenDisabled: false).
public SystemsRunner OneFrame<T>() where T : struct, IComponentReturns
Type Parameters
T
OneFrame<T>(bool)
Register a one-frame component type, choosing what happens while the runner is skipped. The components are always removed at the end of each Run() of the enabled runner. With cleanWhenDisabled false they are kept while the runner is skipped and removed by its next enabled Run. With cleanWhenDisabled true they are also removed in every frame the runner is skipped: when the parent's Run passes the runner's disabled slot (at the slot's position in the parent's pipeline; this cascades into inline groups nested in the skipped runner), and when Run() is called on a disabled phase. A phase nobody calls Run() on is not cleaned.
public SystemsRunner OneFrame<T>(bool cleanWhenDisabled) where T : struct, IComponentParameters
cleanWhenDisabled bool
Returns
Type Parameters
T
Run()
Call every frame. On the root runner increments the tick counter, then runs all enabled systems and removes this runner's OneFrame components. Child runners do not advance the tick — only the root does.
public void Run()Run(World)
IRunSystem entry point, used when this runner is nested in another. Runs all enabled systems and removes this runner's OneFrame components, without advancing the tick. The cleanup runs also when a system threw, for every OneFrame type: if the cleanup of one type throws as well (a listener of its components), the other types are still cleaned, and the first exception is the one Run reports (the system's, when a system threw). While the runner is disabled it runs no systems and only removes the OneFrame types registered with cleanWhenDisabled. The outermost Run on the world (not one called from inside another runner's Run) then applies the listener removals queued by UnsubscribeLater<T>(), also when a system threw. Under KENSEI_DEBUG throws if world differs from the runner's own or Init has not completed.
public void Run(World world)Parameters
world World
SetActive(int, bool)
Enable or disable the system registered at the given position (run systems and nested runners). For an inline group this sets both its run slot here and the group's IsEnabled; for a phase it sets the phase's IsEnabled. A system without Run (Init/Destroy only) cannot be toggled: under KENSEI_DEBUG it throws, in release the call does nothing.
public void SetActive(int index, bool active)Parameters
index int
active bool
SetActive(bool)
Enable or disable this runner, from a reference to it (a handle on a group or phase, without its name or the parent). For an inline group it sets the group's slot in the parent together with its IsEnabled, exactly like the parent's SetActive(name, active); for a phase or a root runner it sets the runner's own IsEnabled (a disabled root runs no systems and only removes its cleanWhenDisabled OneFrame types).
public void SetActive(bool active)Parameters
active bool
SetActive(string, bool)
Enable or disable a named run system, a named inline group or a phase. Disabled systems and groups are skipped during Run(); a disabled phase's Run() runs no systems. For a group both its slot here and its IsEnabled change. The name is compared ordinally (case-sensitive). Under KENSEI_DEBUG an unknown name, or the name of a system without Run (Init/Destroy only), throws; in release the call does nothing, silently — a misspelt name is not reported.
public void SetActive(string name, bool active)Parameters
name string
active bool
Warmup()
Pre-warm the ECS: Init all systems (registers pools and filters), then exercise World internals (JIT + memory allocation) with World.Warmup. Existing entities and their data are not touched. World event listeners do not observe the temporary warmup entity, but typed pool listeners (IComponentListener) and filter listeners (IFilterListener) do — including those systems subscribe in Init: they get OnAdded/OnEntityAdded and then OnRemoved/OnEntityRemoved for it, with default component values. After warmup, World is ready — Init() has already been called.
public void Warmup()