Game development: Unreal Engine

Sefism.

Chapter 02

Programming for games

How game code is structured in your engine: the script lifecycle, components, exposed variables, events and the language itself.

Script lifecycle

Also called Awake, _ready, _process, BeginPlay, lifecycle method

What
The functions the engine calls on your code at set moments: once when the object is created or enters play, then every frame, then at a fixed physics rate, and finally when it is destroyed.
Why
Putting code in the right callback avoids most beginner bugs: setup in the start callback, per-frame logic in update, physics in the fixed-rate callback.
C++
void APlayerShip::BeginPlay()
{
  Super::BeginPlay();
}

void APlayerShip::Tick(float DeltaTime)
{
  Super::Tick(DeltaTime);
}

Watch out

Reading another object in the earliest callback before it has initialised itself, which gives null references.

Components and composition

Also called ECS, composition over inheritance

What
Building game objects from small parts: a health component, a mover, a sprite, a collider. Unity uses components on GameObjects, Godot uses child nodes, Unreal uses Actor Components.
Why
Small reusable parts let you make a new enemy by combining existing pieces, instead of a deep class tree where every change breaks something.

Watch out

One giant player script of 2,000 lines that handles input, health, animation, sound and UI.

Exposed variables

Also called SerializeField, @export, UPROPERTY EditAnywhere

What
Variables marked so they appear in the editor, where you or a designer can tune them without touching code.
Why
Game feel is found by tweaking numbers such as speed, jump height and cooldowns while the game runs. Exposing them makes tuning fast.
C++
UPROPERTY(EditAnywhere, Category = "Movement")
float MoveSpeed = 600.f;

UPROPERTY(EditDefaultsOnly, Category = "Combat")
TSubclassOf<AActor> BulletClass;

Watch out

Changing values in the editor while in play mode and losing them all when you stop.

Events, signals and dispatchers

Also called observer pattern, delegates

What
A way for an object to announce that something happened, such as "health changed", so that any number of listeners can react without the sender knowing about them.
Why
It keeps code decoupled: the health bar, the sound and the achievement system all listen to the player instead of the player calling each one.
C++
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(
  FOnHealthChanged, int32, NewHealth);

UPROPERTY(BlueprintAssignable)
FOnHealthChanged OnHealthChanged;

// when hit
OnHealthChanged.Broadcast(Health);

Watch out

Forgetting to disconnect or unsubscribe when a listener is destroyed, which causes errors or leaks.

Blueprints

Also called Blueprint visual scripting

What
Unreal's node-based visual scripting: events such as BeginPlay or an input action connect through wires to functions, variables and flow control, all inside the editor.
Why
Blueprints are the fastest way to prototype in Unreal and many shipped games use them heavily. Starting with them teaches the engine before C++ adds compile times.

Watch out

Building huge spaghetti graphs in one Event Graph instead of splitting logic into functions, macros and components.

UCLASS, UPROPERTY and UFUNCTION

Also called Unreal reflection, Unreal C++, reflection system

What
Macros that register your C++ classes, variables and functions with Unreal's reflection system, so the editor, Blueprints, saving and garbage collection can see them.
Why
The common pattern is core systems in C++ exposed to Blueprints for tuning and content. Without the macros, Blueprints cannot call your code.
C++
UCLASS()
class AEnemy : public ACharacter
{
  GENERATED_BODY()
public:
  UPROPERTY(EditAnywhere, BlueprintReadWrite)
  int32 Health = 3;

  UFUNCTION(BlueprintCallable)
  void TakeHit(int32 Damage);
};

Watch out

Holding a raw pointer to a UObject without UPROPERTY, so the garbage collector frees it and your pointer dangles.

Unreal gameplay framework

Also called GameMode, Pawn, PlayerController

What
Unreal's built-in roles: the GameMode holds the rules, a Pawn or Character is the body the player controls, the PlayerController turns input into commands, and the HUD or widgets show information.
Why
Putting logic where Unreal expects it makes multiplayer, respawning and switching characters much easier later.

Watch out

Putting game rules on the player character, then struggling when the player dies and the character is destroyed.

State machine

Also called FSM, finite state machine

What
Modelling an object as one state at a time, such as idle, running, jumping or attacking, with clear rules for moving between states.
Why
It replaces a tangle of booleans (isJumping, isAttacking, isHurt) with one variable, so impossible combinations cannot happen. Enemy AI and animation both use it.
Example
An enemy patrols until it sees the player, chases while in range, attacks when close, and returns to patrol if the player escapes for three seconds.

Watch out

Adding a new boolean for every behaviour until the player can attack while dead.

Debugging games

Also called logging, breakpoints, debug draw

What
Printing values to the console, pausing on breakpoints in your IDE, and drawing debug lines, shapes and text in the game view to see what the code thinks is happening.
Why
Game bugs are often visual or timing related, so seeing a ray or a hitbox on screen finds them faster than reading code.
C++
UE_LOG(LogTemp, Warning,
  TEXT("Health %d"), Health);
DrawDebugLine(GetWorld(), Start, End,
  FColor::Red, false, 1.f);

Watch out

Leaving hundreds of log lines running every frame, which slows the game and hides the message you need.

02 · Programming for games2 / 10