Unreal C++ Gameplay
Write correct UE5 gameplay C++: the reflection macros that connect C++ to the editor and
Blueprints, the Gameplay Framework class roles, and module dependencies. Targets UE 5.8.
When to use
- Use when creating C++ gameplay classes (
AActor, APawn, ACharacter, AGameModeBase,
UActorComponent), exposing properties/functions with UPROPERTY/UFUNCTION, setting up a
GameMode's default classes, or adding a module dependency in *.Build.cs.
- Use when the project has a
Source/ tree with *.h/*.cpp using UCLASS, and *.Build.cs.
When not to use: designer-facing visual logic → unreal-blueprints. Player input
binding details → unreal-enhanced-input. AI logic → unreal-behavior-trees. This skill owns
the C++ class/reflection foundation those build on.
Core workflow
- Name with the right prefix.
A = Actor-derived, U = UObject/component-derived,
F = plain struct, E = enum, I = interface. The prefix must match the base class.
- Declare the class with reflection macros.
UCLASS() above the class, GENERATED_BODY()
as the first line in the body, and #include "ClassName.generated.h" as the last
include in the header.
- Expose data with
UPROPERTY (editor/Blueprint visibility and garbage-collection
tracking) and behaviour with UFUNCTION (BlueprintCallable, etc.).
- Create components in the constructor with
CreateDefaultSubobject<T>(TEXT("Name")) and
set the RootComponent.
- Know the framework roles:
AGameModeBase sets the rules + default classes; APawn/
ACharacter is the controllable body; APlayerController is the player's will;
UActorComponent is reusable behaviour.
- Add module dependencies to
*.Build.cs (e.g. EnhancedInput) or unresolved-symbol
link errors follow.
- Verify by compiling (Live Coding
Ctrl+Alt+F11 for function bodies; full rebuild for
header/UPROPERTY changes) and checking the class/properties appear in the editor.
Patterns
1. Minimal Actor class (header + source)
// Pickup.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Pickup.generated.h" // MUST be the last include
UCLASS()
class MYGAME_API APickup : public AActor // MYGAME_API = your module's export macro
{
GENERATED_BODY()
public:
APickup();
// EditAnywhere = tweak per-instance & on the CDO; BlueprintReadWrite = BP get/set.
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Pickup")
int32 ScoreValue = 10;
// UPROPERTY on a UObject* pointer is what keeps it from being garbage-collected.
UPROPERTY(VisibleAnywhere)
TObjectPtr<UStaticMeshComponent> Mesh; // UE5: TObjectPtr instead of raw UStaticMeshComponent*
UFUNCTION(BlueprintCallable, Category = "Pickup")
void Collect();
protected:
virtual void BeginPlay() override;
};
// Pickup.cpp
#include "Pickup.h"
#include "Components/StaticMeshComponent.h"
APickup::APickup()
{
Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
RootComponent = Mesh; // the mesh is this actor's root
}
void APickup::BeginPlay() { Super::BeginPlay(); } // always call Super
void APickup::Collect() { Destroy(); }
2. GameMode wiring its default classes
// MyGameMode.cpp — set in the constructor so the engine spawns your classes.
AMyGameMode::AMyGameMode()
{
DefaultPawnClass = AMyCharacter::StaticClass();
PlayerControllerClass = AMyPlayerController::StaticClass();
}
3. Module dependency in Build.cs
// MyGame.Build.cs
PublicDependencyModuleNames.AddRange(new string[]
{
"Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput"
});
Pitfalls
generated.h not last / missing — compile errors like "Cannot find generated header" or
"Expected an include". It must be the final include in the header.
- Forgetting
GENERATED_BODY() — UHT (Unreal Header Tool) errors; it must be the first
thing inside the class body.
- Raw
UObject* without UPROPERTY — the garbage collector doesn't see it and may destroy
it out from under you. Track every UObject pointer with UPROPERTY (use TObjectPtr in UE5).
- Header/UPROPERTY edits with Live Coding — Live Coding handles function bodies, but
changes to
UCLASS/UPROPERTY/headers need a full editor restart + rebuild.
- Wrong class prefix — naming an Actor
UFoo (or a component AFoo) breaks UHT; match the
prefix to the base type.
- Unresolved external symbol at link — the module providing the API isn't in
Build.cs
PublicDependencyModuleNames.
- Not calling
Super:: in overridden BeginPlay/Tick/etc. skips engine setup.
References
- For
UActorComponent creation/attachment, the UPROPERTY garbage-collection ownership rules
(TObjectPtr, TArray<TObjectPtr<>>, AddToRoot), and a replication primer, read
references/components-and-gc.md.
- Primary docs: "Unreal Engine CPP Quick Start" and "Gameplay Framework"
(
https://dev.epicgames.com/documentation/en-us/unreal-engine/gameplay-framework-in-unreal-engine).
Related skills
unreal-blueprints — exposing C++ to designers; BP/C++ interop.
unreal-enhanced-input — binding input in a C++ Pawn/Character.
unreal-behavior-trees — C++ AI tasks driven from a behaviour tree.
1---
2name: unreal-cpp-gameplay
3description: >
4 Write Unreal Engine 5 C++ gameplay code: the UCLASS/UPROPERTY/UFUNCTION reflection macros,
5 the Gameplay Framework (GameMode, Pawn, Character, PlayerController, Actor components), and
6 the module Build.cs. Use when writing or debugging UE C++, deriving from AActor/ACharacter/
7 AGameModeBase, exposing properties to the editor or Blueprints, or when the user mentions
8 Unreal C++, UCLASS, GENERATED_BODY, GameMode, ACharacter, or .Build.cs.
9---
10
11# Unreal C++ Gameplay
12
13Write correct UE5 gameplay C++: the reflection macros that connect C++ to the editor and
14Blueprints, the Gameplay Framework class roles, and module dependencies. Targets **UE 5.8**.
15
16## When to use
17
18- Use when creating C++ gameplay classes (`AActor`, `APawn`, `ACharacter`, `AGameModeBase`,
19 `UActorComponent`), exposing properties/functions with `UPROPERTY`/`UFUNCTION`, setting up a
20 GameMode's default classes, or adding a module dependency in `*.Build.cs`.
21- Use when the project has a `Source/` tree with `*.h`/`*.cpp` using `UCLASS`, and `*.Build.cs`.
22
23**When *not* to use:** designer-facing visual logic → `unreal-blueprints`. Player input
24binding details → `unreal-enhanced-input`. AI logic → `unreal-behavior-trees`. This skill owns
25the C++ class/reflection foundation those build on.
26
27## Core workflow
28
291. **Name with the right prefix.** `A` = Actor-derived, `U` = `UObject`/component-derived,
30 `F` = plain struct, `E` = enum, `I` = interface. The prefix must match the base class.
312. **Declare the class with reflection macros.** `UCLASS()` above the class, `GENERATED_BODY()`
32 as the first line in the body, and `#include "ClassName.generated.h"` as the **last**
33 include in the header.
343. **Expose data with `UPROPERTY`** (editor/Blueprint visibility *and* garbage-collection
35 tracking) and behaviour with `UFUNCTION` (`BlueprintCallable`, etc.).
364. **Create components in the constructor** with `CreateDefaultSubobject<T>(TEXT("Name"))` and
37 set the `RootComponent`.
385. **Know the framework roles:** `AGameModeBase` sets the rules + default classes; `APawn`/
39 `ACharacter` is the controllable body; `APlayerController` is the player's will;
40 `UActorComponent` is reusable behaviour.
416. **Add module dependencies** to `*.Build.cs` (e.g. `EnhancedInput`) or unresolved-symbol
42 link errors follow.
437. **Verify** by compiling (Live Coding `Ctrl+Alt+F11` for function bodies; full rebuild for
44 header/UPROPERTY changes) and checking the class/properties appear in the editor.
45
46## Patterns
47
48### 1. Minimal Actor class (header + source)
49
50```cpp
51// Pickup.h
52#pragma once
53#include "CoreMinimal.h"
54#include "GameFramework/Actor.h"
55#include "Pickup.generated.h" // MUST be the last include
56
57UCLASS()
58class MYGAME_API APickup : public AActor // MYGAME_API = your module's export macro
59{
60 GENERATED_BODY()
61public:
62 APickup();
63
64 // EditAnywhere = tweak per-instance & on the CDO; BlueprintReadWrite = BP get/set.
65 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Pickup")
66 int32 ScoreValue = 10;
67
68 // UPROPERTY on a UObject* pointer is what keeps it from being garbage-collected.
69 UPROPERTY(VisibleAnywhere)
70 TObjectPtr<UStaticMeshComponent> Mesh; // UE5: TObjectPtr instead of raw UStaticMeshComponent*
71
72 UFUNCTION(BlueprintCallable, Category = "Pickup")
73 void Collect();
74
75protected:
76 virtual void BeginPlay() override;
77};
78```
79
80```cpp
81// Pickup.cpp
82#include "Pickup.h"
83#include "Components/StaticMeshComponent.h"
84
85APickup::APickup()
86{
87 Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
88 RootComponent = Mesh; // the mesh is this actor's root
89}
90
91void APickup::BeginPlay() { Super::BeginPlay(); } // always call Super
92void APickup::Collect() { Destroy(); }
93```
94
95### 2. GameMode wiring its default classes
96
97```cpp
98// MyGameMode.cpp — set in the constructor so the engine spawns your classes.
99AMyGameMode::AMyGameMode()
100{
101 DefaultPawnClass = AMyCharacter::StaticClass();
102 PlayerControllerClass = AMyPlayerController::StaticClass();
103}
104```
105
106### 3. Module dependency in Build.cs
107
108```csharp
109// MyGame.Build.cs
110PublicDependencyModuleNames.AddRange(new string[]
111{
112 "Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput"
113});
114```
115
116## Pitfalls
117
118- **`generated.h` not last / missing** — compile errors like "Cannot find generated header" or
119 "Expected an include". It must be the final include in the header.
120- **Forgetting `GENERATED_BODY()`** — UHT (Unreal Header Tool) errors; it must be the first
121 thing inside the class body.
122- **Raw `UObject*` without `UPROPERTY`** — the garbage collector doesn't see it and may destroy
123 it out from under you. Track every UObject pointer with `UPROPERTY` (use `TObjectPtr` in UE5).
124- **Header/UPROPERTY edits with Live Coding** — Live Coding handles function bodies, but
125 changes to `UCLASS`/`UPROPERTY`/headers need a full editor restart + rebuild.
126- **Wrong class prefix** — naming an Actor `UFoo` (or a component `AFoo`) breaks UHT; match the
127 prefix to the base type.
128- **Unresolved external symbol at link** — the module providing the API isn't in `Build.cs`
129 `PublicDependencyModuleNames`.
130- **Not calling `Super::`** in overridden `BeginPlay`/`Tick`/etc. skips engine setup.
131
132## References
133
134- For `UActorComponent` creation/attachment, the `UPROPERTY` garbage-collection ownership rules
135 (`TObjectPtr`, `TArray<TObjectPtr<>>`, `AddToRoot`), and a replication primer, read
136 `references/components-and-gc.md`.
137- Primary docs: "Unreal Engine CPP Quick Start" and "Gameplay Framework"
138 (`https://dev.epicgames.com/documentation/en-us/unreal-engine/gameplay-framework-in-unreal-engine`).
139
140## Related skills
141
142- `unreal-blueprints` — exposing C++ to designers; BP/C++ interop.
143- `unreal-enhanced-input` — binding input in a C++ Pawn/Character.
144- `unreal-behavior-trees` — C++ AI tasks driven from a behaviour tree.