unreal-cpp-gameplay
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUnreal C++ Gameplay
Unreal C++ 游戏玩法开发
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+.
编写正确的UE5游戏玩法C++代码:实现C++与编辑器及蓝图连接的反射宏、游戏玩法框架类的角色,以及模块依赖。适用于 UE 5.4+ 版本。
When to use
使用场景
- Use when creating C++ gameplay classes (,
AActor,APawn,ACharacter,AGameModeBase), exposing properties/functions withUActorComponent/UPROPERTY, setting up a GameMode's default classes, or adding a module dependency inUFUNCTION.*.Build.cs - Use when the project has a tree with
Source//*.husing*.cpp, andUCLASS.*.Build.cs
When not to use: designer-facing visual logic → . Player input
binding details → . AI logic → . This skill owns
the C++ class/reflection foundation those build on.
unreal-blueprintsunreal-enhanced-inputunreal-behavior-trees- 适用于创建C++游戏玩法类(、
AActor、APawn、ACharacter、AGameModeBase)、通过UActorComponent/UPROPERTY暴露属性/函数、设置GameMode的默认类,或在UFUNCTION中添加模块依赖时。*.Build.cs - 适用于项目包含使用的
UCLASS目录下Source//*.h文件,以及*.cpp文件的场景。*.Build.cs
不适用于: 面向设计师的可视化逻辑 → 请使用。玩家输入绑定细节 → 请使用。AI逻辑 → 请使用。本技能涵盖上述技能所依赖的C++类/反射基础。
unreal-blueprintsunreal-enhanced-inputunreal-behavior-treesCore workflow
核心工作流程
- Name with the right prefix. = Actor-derived,
A=U/component-derived,UObject= plain struct,F= enum,E= interface. The prefix must match the base class.I - Declare the class with reflection macros. above the class,
UCLASS()as the first line in the body, andGENERATED_BODY()as the last include in the header.#include "ClassName.generated.h" - Expose data with (editor/Blueprint visibility and garbage-collection tracking) and behaviour with
UPROPERTY(UFUNCTION, etc.).BlueprintCallable - Create components in the constructor with and set the
CreateDefaultSubobject<T>(TEXT("Name")).RootComponent - Know the framework roles: sets the rules + default classes;
AGameModeBase/APawnis the controllable body;ACharacteris the player's will;APlayerControlleris reusable behaviour.UActorComponent - Add module dependencies to (e.g.
*.Build.cs) or unresolved-symbol link errors follow.EnhancedInput - Verify by compiling (Live Coding for function bodies; full rebuild for header/UPROPERTY changes) and checking the class/properties appear in the editor.
Ctrl+Alt+F11
- 使用正确的前缀命名。= 继承自Actor的类,
A= 继承自U/组件的类,UObject= 普通结构体,F= 枚举,E= 接口。前缀必须与基类匹配。I - 使用反射宏声明类。类上方添加,类体第一行添加
UCLASS(),头文件中最后一个引用必须是GENERATED_BODY()。#include "ClassName.generated.h" - 使用暴露数据(支持编辑器/蓝图可见性及垃圾回收追踪),使用
UPROPERTY暴露行为(如UFUNCTION等)。BlueprintCallable - 在构造函数中创建组件,使用并设置
CreateDefaultSubobject<T>(TEXT("Name"))。RootComponent - 了解框架角色:设置规则及默认类;
AGameModeBase/APawn是可控制的实体;ACharacter代表玩家的操作意志;APlayerController是可复用的行为组件。UActorComponent - 在中添加模块依赖(例如
*.Build.cs),否则会出现未解析符号的链接错误。EnhancedInput - 验证代码:编译(函数体可使用实时编码;头文件/UPROPERTY更改需完全重建),并检查类/属性是否在编辑器中显示。
Ctrl+Alt+F11
Patterns
代码示例
1. Minimal Actor class (header + source)
1. 最小Actor类(头文件 + 源文件)
cpp
// 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;
};cpp
// 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(); }cpp
// Pickup.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Pickup.generated.h" // 必须是最后一个引用
UCLASS()
class MYGAME_API APickup : public AActor // MYGAME_API = 你的模块导出宏
{
GENERATED_BODY()
public:
APickup();
// EditAnywhere = 可在实例及CDO上调整;BlueprintReadWrite = 蓝图可读写。
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Pickup")
int32 ScoreValue = 10;
// UObject*指针上的UPROPERTY用于防止被垃圾回收。
UPROPERTY(VisibleAnywhere)
TObjectPtr<UStaticMeshComponent> Mesh; // UE5: 使用TObjectPtr替代原始UStaticMeshComponent*
UFUNCTION(BlueprintCallable, Category = "Pickup")
void Collect();
protected:
virtual void BeginPlay() override;
};cpp
// Pickup.cpp
#include "Pickup.h"
#include "Components/StaticMeshComponent.h"
APickup::APickup()
{
Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
RootComponent = Mesh; // 该网格体是此Actor的根组件
}
void APickup::BeginPlay() { Super::BeginPlay(); } // 务必调用父类方法
void APickup::Collect() { Destroy(); }2. GameMode wiring its default classes
2. GameMode配置默认类
cpp
// MyGameMode.cpp — set in the constructor so the engine spawns your classes.
AMyGameMode::AMyGameMode()
{
DefaultPawnClass = AMyCharacter::StaticClass();
PlayerControllerClass = AMyPlayerController::StaticClass();
}cpp
// MyGameMode.cpp — 在构造函数中设置,以便引擎生成你的类。
AMyGameMode::AMyGameMode()
{
DefaultPawnClass = AMyCharacter::StaticClass();
PlayerControllerClass = AMyPlayerController::StaticClass();
}3. Module dependency in Build.cs
3. Build.cs中的模块依赖
csharp
// MyGame.Build.cs
PublicDependencyModuleNames.AddRange(new string[]
{
"Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput"
});csharp
// MyGame.Build.cs
PublicDependencyModuleNames.AddRange(new string[]
{
"Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput"
});Pitfalls
常见陷阱
- not last / missing — compile errors like "Cannot find generated header" or "Expected an include". It must be the final include in the header.
generated.h - Forgetting — UHT (Unreal Header Tool) errors; it must be the first thing inside the class body.
GENERATED_BODY() - Raw without
UObject*— the garbage collector doesn't see it and may destroy it out from under you. Track every UObject pointer withUPROPERTY(useUPROPERTYin UE5).TObjectPtr - Header/UPROPERTY edits with Live Coding — Live Coding handles function bodies, but
changes to /
UCLASS/headers need a full editor restart + rebuild.UPROPERTY - Wrong class prefix — naming an Actor (or a component
UFoo) breaks UHT; match the prefix to the base type.AFoo - Unresolved external symbol at link — the module providing the API isn't in
Build.cs.PublicDependencyModuleNames - Not calling in overridden
Super::/BeginPlay/etc. skips engine setup.Tick
- 未放在最后/缺失 —— 出现类似“无法找到生成的头文件”或“预期包含文件”的编译错误。它必须是头文件中的最后一个引用。
generated.h - 忘记添加—— 出现UHT(Unreal Header Tool)错误;它必须是类体中的第一行内容。
GENERATED_BODY() - 未添加的原始
UPROPERTY—— 垃圾回收器无法识别该指针,可能会在你不知情的情况下销毁对象。使用UObject*追踪每个UObject指针(UE5中使用UPROPERTY)。TObjectPtr - 使用实时编码修改头文件/UPROPERTY —— 实时编码仅处理函数体,/
UCLASS/头文件的更改需要完全重启编辑器并重建项目。UPROPERTY - 错误的类前缀 —— 将Actor命名为(或将组件命名为
UFoo)会破坏UHT;前缀必须与基类类型匹配。AFoo - 链接时出现未解析外部符号 —— 提供API的模块未添加到的
Build.cs中。PublicDependencyModuleNames - 未调用—— 重写
Super::/BeginPlay等方法时跳过父类调用会导致引擎初始化不完整。Tick
References
参考资料
- For creation/attachment, the
UActorComponentgarbage-collection ownership rules (UPROPERTY,TObjectPtr,TArray<TObjectPtr<>>), and a replication primer, readAddToRoot.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
- 关于的创建/附着、
UActorComponent垃圾回收所有权规则(UPROPERTY、TObjectPtr、TArray<TObjectPtr<>>)以及复制入门知识,请阅读AddToRoot。references/components-and-gc.md - 官方文档:“Unreal Engine CPP快速入门”和“游戏玩法框架”()。
https://dev.epicgames.com/documentation/en-us/unreal-engine/gameplay-framework-in-unreal-engine
Related skills
相关技能
- — exposing C++ to designers; BP/C++ interop.
unreal-blueprints - — binding input in a C++ Pawn/Character.
unreal-enhanced-input - — C++ AI tasks driven from a behaviour tree.
unreal-behavior-trees
- —— 向设计师暴露C++功能;蓝图/C++交互。
unreal-blueprints - —— 在C++ Pawn/Character中绑定输入。
unreal-enhanced-input - —— 由行为树驱动的C++ AI任务。
unreal-behavior-trees