TExcelTAD
Home › Unreal Engine 5 with C++ › Actor Lifecycle & Tick Order

Actor Lifecycle & Tick Order

Knowing when your code runs is as important as what it does. Most "it works in the editor but crashes in the game" bugs are lifecycle bugs: code that ran before the thing it needed existed.

From constructor to EndPlay

For an actor spawned at runtime, the main calls are:

Step Runs Use it for
Constructor when the class default object (CDO) is created at engine start, and for each new instance creating default subobjects (components), setting defaults. No gameplay, no world access.
PostInitializeComponents() after all components are initialized wiring components to each other
BeginPlay() when the actor starts play (level start, after spawn, when a World Partition cell streams in) gameplay setup: find other actors, bind delegates, start timers
Tick(float DeltaSeconds) every frame, if ticking is enabled continuous work that really is per-frame
EndPlay(EEndPlayReason::Type) on Destroy(), level change, streaming out, PIE end unbind delegates, clear timers, save state
BeginDestroy() / destructor when the garbage collector reclaims it, some time later rarely touched in gameplay code

Components follow the same pattern: InitializeComponent() (if bWantsInitializeComponent is set), BeginPlay(), TickComponent(), EndPlay().

Warning: The constructor also runs for the CDO, the class default object that Unreal creates once per class to hold default values. GetWorld() returns null there, and so does every other world query. Code such as GetWorld()->SpawnActor in a constructor crashes the editor at startup, sometimes before you can open the project to fix it.

A Blueprint's Construction Script (OnConstruction in C++) is different again: it runs in the editor every time a placed actor is moved or edited. Use it for visual setup only (procedural meshes, splines), never for gameplay state.

Tick: groups and prerequisites

Every frame, the world ticks objects in tick groups, in this order:

Group Typical use
TG_PrePhysics the default; most gameplay, movement input
TG_StartPhysics / TG_DuringPhysics / TG_EndPhysics around the physics simulation; DuringPhysics for work that can run in parallel with it
TG_PostPhysics reading physics results: a camera following a ragdoll, weapon aim after movement
TG_PostUpdateWork after everything else in the frame

Within a group, the order is not guaranteed. If one object must tick after another, add a prerequisite:

cpp
AArenaTurret::AArenaTurret()
{
    PrimaryActorTick.bCanEverTick = true;
    PrimaryActorTick.TickGroup = TG_PostPhysics;     // aim after targets have moved
    PrimaryActorTick.TickInterval = 0.1f;            // 10 times a second is plenty for a turret
}

void AArenaTurret::BeginPlay()
{
    Super::BeginPlay();
    if (Target)
    {
        AddTickPrerequisiteActor(Target);            // Target's tick finishes before ours
    }
}

Also useful: SetActorTickEnabled(false) while idle, PrimaryActorTick.bStartWithTickEnabled = false, and AddTickPrerequisiteComponent. Always call Super::BeginPlay(), Super::Tick(DeltaSeconds) and Super::EndPlay(Reason): the base classes do real work there (Blueprint events fire from them).

A model of the tick order

This model has the three ingredients of Unreal's tick scheduler: groups, prerequisites, and an interval that skips frames. Inside each group it orders by prerequisites (a topological sort) and otherwise by registration order. The real engine guarantees nothing beyond the prerequisites, and that is the point to remember.

cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <string>
#include <vector>

enum Group { PrePhysics, DuringPhysics, PostPhysics };
const char* GroupName[] = {"PrePhysics", "DuringPhysics", "PostPhysics"};

struct TickFunction {
    std::string Name;
    Group TickGroup = PrePhysics;
    double Interval = 0;                 // 0 = every frame
    double Cooldown = 0;
    std::vector<std::string> Prereqs;
};

void TickFrame(std::vector<TickFunction>& all, double dt, int frame) {
    std::cout << "frame " << frame << ":";
    for (int g = PrePhysics; g <= PostPhysics; g++) {
        if (g == DuringPhysics) std::cout << " [physics]";
        std::set<std::string> done;
        bool progress = true;
        while (progress) {                               // repeatedly run whatever is ready
            progress = false;
            for (auto& t : all) {
                if (t.TickGroup != g || done.count(t.Name)) continue;
                bool ready = true;
                for (auto& p : t.Prereqs)
                    for (auto& o : all)
                        if (o.Name == p && o.TickGroup == g && !done.count(p)) ready = false;
                if (!ready) continue;
                done.insert(t.Name);
                progress = true;
                t.Cooldown -= dt;
                if (t.Cooldown > 1e-9) continue;          // interval not reached: skip this frame
                t.Cooldown = t.Interval;
                std::cout << " " << t.Name;
            }
        }
    }
    std::cout << "\n";
}

int main() {
    std::vector<TickFunction> ticks = {
        {"Turret", PostPhysics, 0.05, 0, {}},
        {"CameraBoom", PostPhysics, 0, 0, {"Character"}},
        {"HealthRegen", PrePhysics, 0, 0, {}},
        {"Character", PrePhysics, 0, 0, {"PlayerController"}},
        {"PlayerController", PrePhysics, 0, 0, {}},
        {"Cloth", DuringPhysics, 0, 0, {}},
    };
    for (int f = 1; f <= 4; f++) TickFrame(ticks, 1.0 / 30.0, f);
}
frame 1: HealthRegen PlayerController Character [physics] Cloth Turret CameraBoom
frame 2: HealthRegen PlayerController Character [physics] Cloth CameraBoom
frame 3: HealthRegen PlayerController Character [physics] Cloth Turret CameraBoom
frame 4: HealthRegen PlayerController Character [physics] Cloth CameraBoom

Notice that Character ran after PlayerController because of its prerequisite, although it was registered earlier, and that CameraBoom lists Character as a prerequisite, but Character is in an earlier group, so the group order already satisfies it. The turret with a 0.05 s interval at 30 FPS (0.033 s frames) runs on some frames and skips others.

Test yourself

Why must a constructor never call GetWorld()->SpawnActor?

  1. Spawning is slow
  2. The constructor also runs for the class default object, where there is no world
  3. Constructors can't call functions
  4. SpawnActor is Blueprint-only

Two actors tick in TG_PrePhysics. Which one ticks first?

  1. The one spawned first
  2. Alphabetical order
  3. It is not guaranteed unless you add a tick prerequisite
  4. The one with the lower TickInterval

A camera should follow a ragdolling character without lagging a frame. Which tick group fits?

  1. TG_PrePhysics
  2. TG_DuringPhysics
  3. TG_PostPhysics
  4. It doesn't matter