Unreal Engine 4 — Game Flow and Actor Lifecycle Overview


It is always important to know your tools. It is not enough to know how to use it but to know how it works (at least to some degree). Therefore, my goal for this post is to provide you with an overview of the Game Flow and Actor Lifecycle in Unreal Engine 4 (UE4).


Game Flow Breakdown

The game flow of UE4 is broken down into a series of events:

  1. Initialize the Engine
  2. Create and initialize a GameInstance
  3. Load a level
  4. Start playing

There are differences between running UE4 in editor and standalone (executable) mode. Here is a diagram of the events that occur when UE4 runs in editor and standalone mode.

UE4 game flow from the official documentation

 

Standalone

In this mode, the objects needed to play the game are created and initialized immediately after the engine initialization on startup. Objects such as GameInstance are created and initialized before starting the engine. This is distinct from creating and initializing the Engine. Once the engine’s start function is invoked, the starting map is loaded immediately. Gameplay officially begins after the creation of the appropriate Game Mode and Game State, and then follow by the Actors.

 

Editor Mode

In this mode, the engine initializes and starts immediately to allow the editor to run. However, the creation and initialization of objects such as the GameInstance are deferred until the user presses the button to initiate a Play In Editor (PIE) or Simulate In Editor (SIE) session. The actors in the level are duplicated so that any in-game changes do not affect the ones in the editor level. In addition, every object, including GameInstance, has a separate copy for each PIE instance. Once the PIE Game Instance starts, it follows the same events as Standalone mode.

 

Actor Lifecycle

In UE4, Actors are one of the main Objects you will be using in development. Therefore, it is beneficial to know about how Actor works in UE4, particularly in its lifecycle. Knowing about the lifecycle of Actor allows you to debug more efficiently and gives you more control in development. For example, you can overwrite certain functions that are part of the Actor lifecycle flow to do extra tasks in addition to its original task.

 

An Actor can come into a game in multiple ways:

  • Play In Editor (PIE)
  • Packaged or part of a map/level
  • Spawn dynamically

 

Here is a diagram of the events that occur with each of the ways how an Actor come into a game:

UE4 Actor Lifecycle from the official documentation

** Note: UE4 specific events/functions are in bold for the Load From Disk, Play In Editor, Spawning, Deferred Spawning, and Coming to the End of Life sections.

Load From Disk

This path occurs for any Actor that is already part of a level, like when LoadMap occurs or AddToWorld is called. The following events occur in the Load from Disk lifecycle:

  1. Actors in a package/level are loaded from disk
  2. When the actor has finished loading from disk, PostLoad is called. Within PostLoad is where any custom versioning and fixup behaviors should go.
  3. InitializeActorsForPlay
  4. RouteActorInitialize for any non-initialized Actors
    1. PreInitializeComponents is called before InitializeComponent is called on the Actor’s components
    2. InitializeComponent works as a helper function for creation of each component defined on the Actor
    3. PostInitializeComponents is called after the Actor’s components have been initialized
  5. BeginPlay is called when the level begins

 

Play In Editor

The Play In Editor is similar to the Load from Disk lifecycle, except that the actors are not loaded from disk; they are copied from the Editor. The following events occur from the PIE lifecycle:

  1. Actors are duplicated into a new World
  2. PostDuplicate is called
  3. InitializeActorsForPlay
  4. RouteActorInitialize for any non-initialized Actors
    1. PreInitializeComponents is called before InitializeComponent is called on the Actor’s components
    2. InitializeComponent works as a helper function for creation of each component defined on the Actor
    3. PostInitializeComponents is called after the Actor’s components have been initialized
  5. BeginPlay is called when the level begins

 

Spawning

When you are spawning (instancing) an Actor, the following events occur:

  1. SpawnActor called
  2. PostSpawnInitialize
  3. PostActorCreated is called for spawned actors after creation. Any Constructor like behaviors should go here.
  4. ExecuteConstruction
    1. OnConstruction is called. This is the construction of the Actor and is where Blueprint (BP) Actors have their components created and BP variables initialized.
  5. PostActorConstruction
    1. PreInitializeComponents is called before InitializeComponent is called on the Actor’s components
    2. InitializeComponent works as a helper function for creation of each component defined on the Actor
    3. PostInitializeComponents is called after the Actor’s components have been initialized
  6. OnActorSpawned broadcasts on UWorld. You can use this as a signal to know when the actor spawning process is complete.
  7. BeginPlay is called

 

Deferred Spawning

Deferred spawning an Actor allows you to include additional setup before BP construction script. Aside from this, the events that occur are similar to Spawning. The following events occur in Deferred Spawning lifecycle:

  1. SpawnActorDeferred is meant to spawn procedural Actors. It allows additional setup before BP construction script.
  2. Everything in SpawnActor occurs, but after PostActorCreated the following occurs:
    1. Do setup/call various “initialization functions” with a valid, but incomplete Actor instance
    2. FinishSpawningActor is called to finalize the Actor. Once completed, calls ExecuteConstruction (step 4 in Spawning) and follow the same path as Spawning

 

Coming to the End of Life

There are multiple ways that an Actor reaches end of life. They could be defeated by the player, destroyed by environment, fall off the map, and etc. Regardless of how the Actor reaches end of life, the procedure to remove their existence remains the same.

 

There are three main events that are responsible for handling the end of life of an Actor. They are:

  • Destroy
  • OnDestroy
  • EndPlay

 

Destroy

Destroy is called manually by the game anytime that an Actor is meant to be removed, but the game is still ongoing. The actor is marked pending kill and removed from the level’s array of actors.

 

OnDestroy

OnDestroy is a legacy response to Destroy. You should probably move anything here to EndPlay since it is called by level transition and other game cleanup functions.

 

EndPlay

EndPlay is called in several places to guarantee the life of an Actor is coming to an end. All the places that calls EndPlay are the following:

  • Explicit call to Destroy
  • Play in Editor ended
  • Level Transition
  • A streaming level containing the Actor is unloaded
  • The lifetime of the actor has expired
  • Application shutdown (ALL Actors are destroyed)

Regardless of which event triggered EndPlay, the Actor will be marked RF_PendingKill so that during the next garbage collection cycle it will be deallocated.


 

If you found this post helpful to you, share it with others so they can benefit too.

 

Did this post help you understand the Game Flow of UE4? What about the lifecycle of an Actor? Are there times that you made use of the information in this post?

 

Leave a comment or send me an email at steven@brightdevelopers.com. To stay in touch, follow me on Twitter.


About Steven To

Steven To is a software developer that specializes in mobile development with a background in computer engineering. Beyond his passion for software development, he also has an interest in Virtual Reality, Augmented Reality, Artificial Intelligence, Personal Development, and Personal Finance. If he is not writing software, then he is out learning something new.