unreal-cpp-gameplay
它能做什么
该技能讲解如何为 Unreal Engine 5 编写 C++ 玩法代码,并将其连接到编辑器。适合用 C++ 构建游戏系统的开发者。
适用场景
- ✓当你在 Unreal Engine 5 中创建 C++ Actor、Pawn、Character 或组件时。
- ✓当你需要在编辑器或 Blueprint 中显示 C++ 的属性或函数时。
- ✓当 Build.cs 文件中缺少模块依赖时。
技能链接
https://github.com/gamedev-skills/awesome-gamedev-agent-skills/blob/main/skills/unreal/unreal-cpp-gameplay/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请为我的 Unreal 角色编写一个 C++ 生命值组件,让设计师可以在编辑器中设置它。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能讲解命名前缀、将数据暴露给编辑器的反射宏、主要框架类的作用,以及如何列出模块依赖。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
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 withUPROPERTY/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/*.cppusingUCLASS, 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 withUFUNCTION(BlueprintCallable, etc.). - Create components in the constructor with
CreateDefaultSubobject<T>(TEXT("Name"))and set theRootComponent. - Know the framework roles:
AGameModeBasesets the rules + default classes;APawn/ACharacteris the controllable body;APlayerControlleris the player's will;UActorComponentis reusable behaviour. - Add module dependencies to
*.Build.cs(e.g.EnhancedInput) or unresolved-symbol link errors follow. - Verify by compiling (Live Coding
Ctrl+Alt+F11from the editor, or a full build from the IDE with the editor closed) and checking the class/properties appear in the editor. For automation tests, run headless withUnrealEditor-Cmd <Project>.uproject -ExecCmds="Automation RunTest MyGame;Quit"(RunTesttakes a test name, a dotted prefix, orGroup:<name>).
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.hnot 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*withoutUPROPERTY— the garbage collector doesn't see it and may destroy it out from under you. Track every UObject pointer withUPROPERTY(useTObjectPtrin UE5). - **Header/UPROPERTY
常见问题
反射宏有什么作用?
它们让 Unreal Engine 知道你的类和属性,使编辑器和 Blueprint 能够看到它们。
为什么类名需要字母前缀?
前缀表示基础类型,例如 A 代表 Actor,并且必须与所继承的类相匹配。
它涵盖可视化脚本吗?
不涵盖。Blueprint 可视化脚本由另一份 Blueprint 技能负责。
详情
作者的原始描述
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.
热度
- GitHub 星标
- 6.6k
- 排名
- #13854
- 许可证
- Apache-2.0
- 更新时间
- 2026/10/28
评论
- …