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.4+.
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-gameplay3description: Write Unreal Engine 5 C++ gameplay code: the UCLASS/UPROPERTY/UFUNCTION reflection macros, the Gameplay Framework (GameMode, Pawn, Character, PlayerController, Actor components), and the module Build.cs. Use when writing or debugging UE C++, deriving from AActor/ACharacter/ AGameModeBase, exposing properties to the editor or Blueprints, or when the user mentions Unreal C++, UCLASS, GENERATED_BODY, GameMode, ACharacter, or .Build.cs.4license: Apache-2.05---67# Unreal C++ Gameplay89Write correct UE5 gameplay C++: the reflection macros that connect C++ to the editor and10Blueprints, the Gameplay Framework class roles, and module dependencies. Targets **UE 5.4+**.1112## When to use1314- Use when creating C++ gameplay classes (`AActor`, `APawn`, `ACharacter`, `AGameModeBase`,15 `UActorComponent`), exposing properties/functions with `UPROPERTY`/`UFUNCTION`, setting up a16 GameMode's default classes, or adding a module dependency in `*.Build.cs`.17- Use when the project has a `Source/` tree with `*.h`/`*.cpp` using `UCLASS`, and `*.Build.cs`.1819**When *not* to use:** designer-facing visual logic → `unreal-blueprints`. Player input20binding details → `unreal-enhanced-input`. AI logic → `unreal-behavior-trees`. This skill owns21the C++ class/reflection foundation those build on.2223## Core workflow24251. **Name with the right prefix.** `A` = Actor-derived, `U` = `UObject`/component-derived,26 `F` = plain struct, `E` = enum, `I` = interface. The prefix must match the base class.272. **Declare the class with reflection macros.** `UCLASS()` above the class, `GENERATED_BODY()`28 as the first line in the body, and `#include "ClassName.generated.h"` as the **last**29 include in the header.303. **Expose data with `UPROPERTY`** (editor/Blueprint visibility *and* garbage-collection31 tracking) and behaviour with `UFUNCTION` (`BlueprintCallable`, etc.).324. **Create components in the constructor** with `CreateDefaultSubobject<T>(TEXT("Name"))` and33 set the `RootComponent`.345. **Know the framework roles:** `AGameModeBase` sets the rules + default classes; `APawn`/35 `ACharacter` is the controllable body; `APlayerController` is the player's will;36 `UActorComponent` is reusable behaviour.376. **Add module dependencies** to `*.Build.cs` (e.g. `EnhancedInput`) or unresolved-symbol38 link errors follow.397. **Verify** by compiling (Live Coding `Ctrl+Alt+F11` for function bodies; full rebuild for40 header/UPROPERTY changes) and checking the class/properties appear in the editor.4142## Patterns4344### 1. Minimal Actor class (header + source)4546```cpp47// Pickup.h48#pragma once49#include "CoreMinimal.h"50#include "GameFramework/Actor.h"51#include "Pickup.generated.h" // MUST be the last include5253UCLASS()54class MYGAME_API APickup : public AActor // MYGAME_API = your module's export macro55{56 GENERATED_BODY()57public:58 APickup();5960 // EditAnywhere = tweak per-instance & on the CDO; BlueprintReadWrite = BP get/set.61 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Pickup")62 int32 ScoreValue = 10;6364 // UPROPERTY on a UObject* pointer is what keeps it from being garbage-collected.65 UPROPERTY(VisibleAnywhere)66 TObjectPtr<UStaticMeshComponent> Mesh; // UE5: TObjectPtr instead of raw UStaticMeshComponent*6768 UFUNCTION(BlueprintCallable, Category = "Pickup")69 void Collect();7071protected:72 virtual void BeginPlay() override;73};74```7576```cpp77// Pickup.cpp78#include "Pickup.h"79#include "Components/StaticMeshComponent.h"8081APickup::APickup()82{83 Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));84 RootComponent = Mesh; // the mesh is this actor's root85}8687void APickup::BeginPlay() { Super::BeginPlay(); } // always call Super88void APickup::Collect() { Destroy(); }89```9091### 2. GameMode wiring its default classes9293```cpp94// MyGameMode.cpp — set in the constructor so the engine spawns your classes.95AMyGameMode::AMyGameMode()96{97 DefaultPawnClass = AMyCharacter::StaticClass();98 PlayerControllerClass = AMyPlayerController::StaticClass();99}100```101102### 3. Module dependency in Build.cs103104```csharp105// MyGame.Build.cs106PublicDependencyModuleNames.AddRange(new string[]107{108 "Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput"109});110```111112## Pitfalls113114- **`generated.h` not last / missing** — compile errors like "Cannot find generated header" or115 "Expected an include". It must be the final include in the header.116- **Forgetting `GENERATED_BODY()`** — UHT (Unreal Header Tool) errors; it must be the first117 thing inside the class body.118- **Raw `UObject*` without `UPROPERTY`** — the garbage collector doesn't see it and may destroy119 it out from under you. Track every UObject pointer with `UPROPERTY` (use `TObjectPtr` in UE5).120- **Header/UPROPERTY edits with Live Coding** — Live Coding handles function bodies, but121 changes to `UCLASS`/`UPROPERTY`/headers need a full editor restart + rebuild.122- **Wrong class prefix** — naming an Actor `UFoo` (or a component `AFoo`) breaks UHT; match the123 prefix to the base type.124- **Unresolved external symbol at link** — the module providing the API isn't in `Build.cs`125 `PublicDependencyModuleNames`.126- **Not calling `Super::`** in overridden `BeginPlay`/`Tick`/etc. skips engine setup.127128## References129130- For `UActorComponent` creation/attachment, the `UPROPERTY` garbage-collection ownership rules131 (`TObjectPtr`, `TArray<TObjectPtr<>>`, `AddToRoot`), and a replication primer, read132 `references/components-and-gc.md`.133- Primary docs: "Unreal Engine CPP Quick Start" and "Gameplay Framework"134 (`https://dev.epicgames.com/documentation/en-us/unreal-engine/gameplay-framework-in-unreal-engine`).135136## Related skills137138- `unreal-blueprints` — exposing C++ to designers; BP/C++ interop.139- `unreal-enhanced-input` — binding input in a C++ Pawn/Character.140- `unreal-behavior-trees` — C++ AI tasks driven from a behaviour tree.