Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 38 ยท C++ Gameplay

Your First C++ Actor Class

Create your first Unreal Engine C++ Actor. Learn the generated header and source files, the constructor, BeginPlay, Tick, EndPlay and how Live Coding rebuilds it.

Blueprints are great for fast iteration, but most shipped Unreal Engine games put their core systems in C++. C++ gives you speed, access to every engine API, and code that works well with source control and code review. The good news is that you do not have to choose: the usual workflow is a C++ base class that exposes a clean interface, plus Blueprint subclasses that tweak values and wire up content.

In this lesson you create a C++ Actor, read the two files the editor generates, and learn the life cycle of an Actor in code. A note on honesty: the Unreal Editor and its compiler cannot run on this website, so the C++ blocks below are read-only and the runnable examples are small Python stand-ins that model the same ideas. Names and menu paths refer to Unreal Engine 5.x.

Prerequisites

You need a C++ toolchain: Visual Studio 2022 with the "Game development with C++" workload on Windows, Xcode on macOS, or clang plus the usual build tools on Linux. In the editor, choose Tools > New C++ Class, pick Actor as the parent, name it SpinningCube and press Create Class. The editor writes two files into your project and opens your IDE.

If the project was created as Blueprint-only, the first C++ class converts it to a code project and generates a solution file. After that, you build either from the IDE or with the editor's Live Coding (Ctrl+Alt+F11), which patches the running editor without a restart.

The header file

The generated header looks like this:

C++
#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "SpinningCube.generated.h"

UCLASS()
class MYGAME_API ASpinningCube : public AActor
{
    GENERATED_BODY()

public:
    ASpinningCube();

protected:
    virtual void BeginPlay() override;

public:
    virtual void Tick(float DeltaTime) override;
};

Several details matter here:

  • The A prefix marks a class that derives from AActor. Unreal uses prefixes everywhere: U for UObject types, F for plain structs, E for enums, I for interfaces and T for templates. The Unreal Header Tool expects them.
  • UCLASS() registers the class with Unreal's reflection system, so the editor, Blueprints, serialization and garbage collection know about it.
  • GENERATED_BODY() pulls in code the Unreal Header Tool generates into SpinningCube.generated.h. It must stay, and that .generated.h include must be the last include.
  • MYGAME_API exports the class from your game module so other modules can use it.

The source file

C++
#include "SpinningCube.h"

ASpinningCube::ASpinningCube()
{
    PrimaryActorTick.bCanEverTick = true;
}

void ASpinningCube::BeginPlay()
{
    Super::BeginPlay();
}

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

The constructor runs when the class default object is created and whenever an instance is made. Use it to create components and set defaults, and nothing else. Never spawn other Actors or look up the world there, because the constructor also runs while the editor loads, long before gameplay exists.

BeginPlay runs once when the Actor enters a running game (or is spawned into one). This is where you find other Actors, bind to events and start timers. Tick runs every frame with DeltaTime, the seconds since the previous frame. EndPlay runs when the Actor is destroyed or the level ends, and is the place to unbind and clean up.

Always call Super:: in overrides, otherwise parent-class behavior is skipped.

Actor life cycle

Constructor Components and defaults
BeginPlay Once, when play starts
Tick Every frame
EndPlay Clean up

Same order for every Actor

1 / 2

Make it spin

To rotate the cube we use DeltaTime so the speed does not depend on frame rate:

C++
void ASpinningCube::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
    AddActorLocalRotation(FRotator(0.f, 90.f * DeltaTime, 0.f));
}

At 60 fps DeltaTime is about 0.0167, so each frame adds 1.5 degrees of yaw. At 30 fps each frame adds 3 degrees. Either way, one second of play adds 90 degrees. The stand-in below runs the same arithmetic.

Python Stand-in: frame-rate independent rotation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Modelling the life cycle

The second stand-in is a tiny model of the order in which the engine calls your functions. It is not Unreal code, but it shows why the constructor must not do gameplay work.

Python Stand-in: Actor life cycle order

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Forgetting Super::BeginPlay(). Components may then never start.
  • Heavy work in Tick. Search the world once in BeginPlay, not every frame. If an Actor does not need to tick, leave bCanEverTick false.
  • Editing the .generated.h file. It is rewritten at every build.
  • Moving or renaming the class by hand. Use the editor's class wizard, or the IDE rename plus a fresh generate of project files, so reflection data stays consistent.

Once the class compiles, drag it from the Content Browser's C++ Classes folder into the level, or right-click it and choose Create Blueprint class based on SpinningCube to extend it visually. See Actors and components in the earlier chapters for the placement tools.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which function should find other Actors and bind to events when play starts?

    BeginPlay runs once when the Actor enters a running game. The constructor also runs in the editor, where the world is not ready.
  2. 2. What does the A prefix in ASpinningCube indicate?

    Unreal's naming convention uses A for Actor-derived classes, U for UObject types, F for structs and so on.
  3. 3. Why multiply rotation by DeltaTime in Tick?

    DeltaTime is the seconds since the last frame, so rate times DeltaTime gives the same movement per second regardless of fps.
  4. 4. Where must the .generated.h include be placed?

    The Unreal Header Tool requires the generated include to be the last include of the header.

Summary

  • A C++ Actor is created with Tools > New C++ Class and consists of a header and a source file.
  • UCLASS() and GENERATED_BODY() connect the class to Unreal's reflection system.
  • The constructor sets defaults and creates components; BeginPlay starts gameplay; Tick runs per frame; EndPlay cleans up.
  • Use DeltaTime for frame-rate independent motion and always call Super::.
  • Live Coding (Ctrl+Alt+F11) rebuilds code while the editor is open.

Next: Reflection: UPROPERTY, UFUNCTION and UENUM