Unreal Cpp Gameplay

作者 gamedev-skillsd4b0e35550c5无许可证1.3K 个星标收录于 2026年10月8日更新于 2026年10月8日仓库11天前更新

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.

AI 生成的概览

指导编写虚幻引擎 5 的 C++ 玩法代码:反射宏、Gameplay Framework 类以及 Build.cs 模块。

功能
该技能提供编写面向 UE 5.8 的虚幻引擎 5 玩法 C++ 的说明。内容涵盖 UCLASS、UPROPERTY、UFUNCTION 与 GENERATED_BODY 反射宏、类名前缀、组件创建、Gameplay Framework 各类的职责,以及 Build.cs 中的模块依赖。它还列出常见陷阱与验证步骤,并指向一份关于 Actor 组件与垃圾回收的随附参考文档。
适用场景
适用于创建或调试 AActor、APawn、ACharacter、AGameModeBase、UActorComponent 等 UE C++ 玩法类。也适合需要向编辑器或蓝图暴露属性与函数、配置 GameMode 默认类,或在 Build.cs 中添加模块依赖的场景。
运行要求
不含脚本,仅为说明文档。需要具备 Source 目录与 Build.cs 文件的虚幻引擎 5 C++ 项目,以及用于验证的编译器或编辑器构建流程。代理会读取一份随附参考文件。

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

  1. 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.
  2. 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.
  3. Expose data with UPROPERTY (editor/Blueprint visibility and garbage-collection tracking) and behaviour with UFUNCTION (BlueprintCallable, etc.).
  4. Create components in the constructor with CreateDefaultSubobject<T>(TEXT("Name")) and set the RootComponent.
  5. 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.
  6. Add module dependencies to *.Build.cs (e.g. EnhancedInput) or unresolved-symbol link errors follow.
  7. 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)

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 Supervoid APickup::Collect()   { Destroy(); }

2. GameMode wiring its default classes

cpp
// 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

csharp
// MyGame.Build.csPublicDependencyModuleNames.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.

来源与署名

来源:gamedev-skills/awesome-gamedev-agent-skills位于skills/unreal/unreal-cpp-gameplay提交d4b0e35

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架