unreal-cpp-gameplay
>
它会碰到什么
这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。
技能内容
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/.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 with UFUNCTION (BlueprintCallable, etc.).
- Create components in the constructor with
CreateDefaultSubobject<T>(TEXT("Name"))and
set the RootComponent.
- Know the framework roles:
AGameModeBasesets 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+F11for 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.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
UObjectwithoutUPROPERTY* — 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 componentAFoo) 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 overriddenBeginPlay/Tick/etc. skips engine setup.
References
- For
UActorComponentcreation/attachment, theUPROPERTYgarbage-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.
想直接用这个技能?
本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。
它属于哪个仓库
skills/unreal/unreal-cpp-gameplay/SKILL.md