Game loop
Every frame, Turian runs the game through a fixed sequence of phases. Components use the familiar OnFixedUpdate, OnUpdate and OnLateUpdate callbacks, and code that is not a component joins a phase directly by implementing IGameLoopSubsystem. Knowing the order tells you where a value you read was last written, and which step is safe for simulation and which is for presentation.
The frame
Each frame runs these steps in order:
- BeginFrame: scenes that have not started yet start, input actions are read, and the subsystems that declared this phase run.
- Fixed steps: zero or more times, as the accumulated time requires. Each step runs the component
OnFixedUpdatecallbacks, then theFixedTicksubsystems, thenFixedCommit, a destroy flush, and finallyTickCompleted. - AfterFixedTicks: once per frame, even when no fixed step ran. Presentation interpolates between the last step and the next one here.
- Update: the component
OnUpdatecallbacks, then theUpdatesubsystems, then the gameplay waits that are due (async). - LateUpdate: the component
OnLateUpdatecallbacks, then theLateUpdatesubsystems, then the late waits. - Destroy and end of frame: nodes destroyed during the presentation steps leave, then
EndFrameruns, then a requested session restart applies, and input advances to the next frame.
The table lists the phases the engine defines, in the order they first run:
| Phase | How often | Runs |
|---|---|---|
BeginFrame |
once per frame | Before any fixed step. Input actions are read here. |
FixedTick |
once per fixed step | After component OnFixedUpdate. Gameplay systems such as physics. |
FixedCommit |
once per fixed step | After FixedTick. An authoritative simulation commits, and data-asset work drains. |
TickCompleted |
once per successful fixed step | After the fixed step's destroy flush. Also runs for catch-up steps. |
AfterFixedTicks |
once per frame | After every fixed step of the frame, with the presentation alpha. |
Update |
once per frame | After component OnUpdate. |
LateUpdate |
once per frame | After component OnLateUpdate. |
EndFrame |
once per frame | After the frame's destroy flush, before a requested session restart. |
Fixed and variable updates
Simulation runs on a fixed step, a constant interval of game time. Presentation runs once per frame with the frame's own time. Keep these apart:
- The fixed step is
FixedDeltaTimeseconds, 1/60 of a second by default. It is set in the project's Time Settings asset. - A frame runs as many fixed steps as the time since the last frame covers, to a maximum of
MaxTicksPerFrame(8 by default). Time beyond that is kept for later frames, so a slow frame catches up over several frames. DropBacklogchanges the catch-up: when it is on, time a frame cannot simulate within the maximum is dropped instead of kept. Servers leave it off. Clients that cannot keep up may turn it on, and the game then slows down rather than falling further behind.Alphais how far presentation time lies between the last fixed step and the next, from 0 to 1. Use it to interpolate what is drawn between two simulated states.- A paused clock runs no fixed steps. The frame phases still run, with a delta time of zero. Stepping the clock in Studio runs exactly one fixed step and one frame.
Phases do not change how many fixed steps a frame runs. Only the clock settings do.
Joining the loop as a component
A component joins the loop through its callbacks. The full lifecycle is in components; the three update callbacks map onto the phases like this:
OnFixedUpdate(float fixedDeltaTime)runs once per fixed step. Put gameplay that must be repeatable here, such as movement and physics decisions.OnUpdate(float deltaTime)runs once per frame. Put input handling, animation and effects here.OnLateUpdate(float deltaTime)runs once per frame after everyOnUpdate. Cameras that follow moving objects belong here.
namespace Usercode;
public class Drifter : Component
{
public float Speed = 2f;
public override void OnFixedUpdate(float fixedDeltaTime) =>
Node!.Position += new Vector3(Speed * fixedDeltaTime, 0, 0);
}
Because the movement uses the fixed step, it covers the same distance per simulated second, however many frames the game renders in that second.
Destruction follows the loop too. Destroy() on a component or node requested during a fixed step leaves at the end of that step, so the node does not run in the next one. A request made in a presentation step leaves at the end of the frame, before EndFrame.
Joining the loop as a subsystem
A subsystem is a service that runs in the phases it names. It implements IGameLoopSubsystem:
Phasesis a set ofGameLoopPhasesflags. The engine reads it once, when the subsystem's lifetime starts.Run(GameLoopPhase phase, in LoopTime time)is called once for each phase inPhases. Checkphasewhen the subsystem runs in more than one.LoopTimegives theTicknumber, theDeltaTime(the fixed interval in fixed phases), theUnscaledDeltaTimeand theAlpha.
namespace Usercode;
public sealed class MatchTimer : IGameLoopSubsystem
{
public double Elapsed { get; private set; }
public GameLoopPhases Phases => GameLoopPhases.FixedTick;
public void Start() { }
public void Stop() { }
public void Run(GameLoopPhase phase, in LoopTime time) => Elapsed += time.DeltaTime;
}
Register it from the game's IEngineServiceModule, in the lifetime that suits it. The services and lifetimes page shows the registration and the choice of lifetime.
Two other interfaces still work and map onto the same phases. An ITickingSubsystem with TickPhase Fixed runs in FixedTick, and one with Frame runs in Update. An IFixedTickSystem runs in FixedTick and AfterFixedTicks. A subsystem that implements IGameLoopSubsystem is driven only through it, even when it implements these as well.
The engine's own subsystems use the same mechanism. Input actions read in BeginFrame, the simulation loop commits in FixedCommit and publishes in TickCompleted, and data-asset work drains in FixedCommit.
Order within a phase
Within one phase, the application's subsystems run first, then the session's, then those of each loaded scene in the order the scenes loaded. Inside one lifetime, a subsystem runs after the subsystems it depends on. Otherwise subsystems run in registration order.
To order two subsystems that do not depend on each other, declare the ordering with After:
services.AddSubsystem(SubsystemDescriptor.Of(LifetimeScope.Session, static _ => new MatchTimer())
.After(typeof(Scoreboard)));
After is only an ordering hint. A target that is not registered is ignored, and the subsystem does not create, start or keep the target alive. A target with a shorter lifetime is rejected at startup, as is a cycle.
Gotchas
- Keep simulation state in
FixedTick,OnFixedUpdateand the phases after them. Changing gameplay state inUpdateorLateUpdatemakes it depend on the frame rate. - A component callback that throws is logged and the frame goes on. The engine logs the first failure of each callback on each component, so a callback that fails every frame does not flood the log.
- An exception thrown by a subsystem's
Runis not caught by the loop. It propagates out of the tick, so the host sees it. - In the fixed phases,
LoopTime.Tickis the step being executed or just completed. In the other phases it is the last completed step.
Next steps
- Async — waits that resume at a chosen phase.
- Services and lifetimes — registering subsystems and choosing their lifetime.
- Components — the component lifecycle.