unreal-engine-cpp-pro
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUnreal Engine C++ Pro
Unreal Engine C++ Pro
This skill provides expert-level guidelines for developing with Unreal Engine 5 using C++. It focuses on writing robust, performant, and standard-compliant code.
本 skill 提供了使用 C++ 进行 Unreal Engine 5 开发的专家级指南,重点是编写稳健、高性能且符合标准的代码。
When to Use
何时使用
Use this skill when:
- Developing C++ code for Unreal Engine 5.x projects
- Writing Actors, Components, or UObject-derived classes
- Optimizing performance-critical code in Unreal Engine
- Debugging memory leaks or garbage collection issues
- Implementing Blueprint-exposed functionality
- Following Epic Games' coding standards and conventions
- Working with Unreal's reflection system (UCLASS, USTRUCT, UFUNCTION)
- Managing asset loading and soft references
Do not use this skill when:
- Working with Blueprint-only projects (no C++ code)
- Developing for Unreal Engine versions prior to 5.x
- Working on non-Unreal game engines
- The task is unrelated to Unreal Engine development
在以下情况下使用本 skill:
- 为 Unreal Engine 5.x 项目开发 C++ 代码
- 编写 Actor、Component 或派生自 UObject 的类
- 优化 Unreal Engine 中对性能至关重要的代码
- 调试内存泄漏或垃圾回收问题
- 实现向 Blueprint 暴露的功能
- 遵循 Epic Games 的编码标准与约定
- 使用 Unreal 的反射系统(UCLASS、USTRUCT、UFUNCTION)
- 管理资产加载和软引用
在以下情况下不要使用本 skill:
- 处理仅使用 Blueprint 的项目(无 C++ 代码)
- 开发 Unreal Engine 5.x 之前的版本
- 使用非 Unreal 游戏引擎进行开发
- 任务与 Unreal Engine 开发无关
Core Principles
核心原则
-
UObject & Garbage Collection:
- Always use for
UPROPERTY()member variables to ensure they are tracked by the Garbage Collector (GC).UObject* - Use if you need to keep a root reference outside of a UObject graph, but prefer
TStrongObjectPtr<>generally.addToRoot() - Understand the check vs
IsValid().nullptrhandles pending kill state safely.IsValid()
- Always use
-
Unreal Reflection System:
- Use ,
UCLASS(),USTRUCT(),UENUM()to expose types to the reflection system and Blueprints.UFUNCTION() - Minimize when possible; prefer
BlueprintReadWritefor state that shouldn't be trampled by logic in UI/Level BPs.BlueprintReadOnly
- Use
-
Performance First:
- Tick: Disable Ticking () by default. Only enable it if absolutely necessary. Prefer timers (
bCanEverTick = false) or event-driven logic.GetWorldTimerManager() - Casting: Avoid in hot loops. Cache references in
Cast<T>().BeginPlay - Structs vs Classes: Use structs for data-heavy, non-UObject types to reduce overhead.
F
- Tick: Disable Ticking (
-
UObject 与垃圾回收:
- 对 成员变量始终使用
UObject*,以确保垃圾回收器(GC)能够跟踪它们。UPROPERTY() - 如果需要在 UObject 对象图之外保留根引用,请使用 ;但通常更推荐使用
TStrongObjectPtr<>。addToRoot() - 理解 检查与
IsValid()检查之间的区别。nullptr能够安全处理 pending kill 状态。IsValid()
- 对
-
Unreal 反射系统:
- 使用 、
UCLASS()、USTRUCT()、UENUM()将类型暴露给反射系统和 Blueprint。UFUNCTION() - 尽可能减少 的使用;对于不应被 UI/Level BPs 中的逻辑覆盖的状态,优先使用
BlueprintReadWrite。BlueprintReadOnly
- 使用
-
性能优先:
- Tick:默认禁用 Tick()。仅在绝对必要时启用。优先使用计时器(
bCanEverTick = false)或事件驱动逻辑。GetWorldTimerManager() - Casting:避免在热循环中调用 。在
Cast<T>()中缓存引用。BeginPlay - 结构体与类:对于数据密集型的非 UObject 类型,使用 结构体以降低开销。
F
- Tick:默认禁用 Tick(
Naming Conventions (Strict)
命名约定(严格)
Follow Epic Games' coding standard:
- Templates: Prefix with (e.g.,
T,TArray).TMap - UObject: Prefix with (e.g.,
U).UCharacterMovementComponent - AActor: Prefix with (e.g.,
A).AMyGameMode - SWidget: Prefix with (Slate widgets).
S - Structs: Prefix with (e.g.,
F).FVector - Enums: Prefix with (e.g.,
E).EWeaponState - Interfaces: Prefix with (e.g.,
I).IInteractable - Booleans: Prefix with (e.g.,
b).bIsDead
遵循 Epic Games 的编码标准:
- 模板:使用 作为前缀(例如
T、TArray)。TMap - UObject:使用 作为前缀(例如
U)。UCharacterMovementComponent - AActor:使用 作为前缀(例如
A)。AMyGameMode - SWidget:使用 作为前缀(Slate 控件)。
S - 结构体:使用 作为前缀(例如
F)。FVector - 枚举:使用 作为前缀(例如
E)。EWeaponState - 接口:使用 作为前缀(例如
I)。IInteractable - 布尔值:使用 作为前缀(例如
b)。bIsDead
Common Patterns
常见模式
1. Robust Component Lookup
1. 健壮的组件查找
Avoid in . Do it in or .
GetComponentByClassTickPostInitializeComponentsBeginPlaycpp
void AMyCharacter::PostInitializeComponents() {
Super::PostInitializeComponents();
HealthComp = FindComponentByClass<UHealthComponent>();
check(HealthComp); // Fail hard in dev if missing
}避免在 中调用 。应在 或 中执行。
TickGetComponentByClassPostInitializeComponentsBeginPlaycpp
void AMyCharacter::PostInitializeComponents() {
Super::PostInitializeComponents();
HealthComp = FindComponentByClass<UHealthComponent>();
check(HealthComp); // Fail hard in dev if missing
}2. Interface Implementation
2. 接口实现
Use interfaces to decouple systems (e.g., Interaction system).
cpp
// Interface call check
if (TargetActor->Implements<UInteractable>()) {
IInteractable::Execute_OnInteract(TargetActor, this);
}使用接口解耦系统(例如交互系统)。
cpp
// Interface call check
if (TargetActor->Implements<UInteractable>()) {
IInteractable::Execute_OnInteract(TargetActor, this);
}3. Async Loading (Soft References)
3. 异步加载(软引用)
Avoid hard references () for massive assets which force load orders. Use or .
UPROPERTY(EditDefaultsOnly) TSubclassOf<AActor>TSoftClassPtrTSoftObjectPtrcpp
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TSoftClassPtr<AWeapon> WeaponClassToLoad;
void AMyCharacter::Equip() {
if (WeaponClassToLoad.IsPending()) {
WeaponClassToLoad.LoadSynchronous(); // Or use StreamableManager for async
}
}避免对大型资产使用硬引用(),因为硬引用会强制决定加载顺序。请使用 或 。
UPROPERTY(EditDefaultsOnly) TSubclassOf<AActor>TSoftClassPtrTSoftObjectPtrcpp
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TSoftClassPtr<AWeapon> WeaponClassToLoad;
void AMyCharacter::Equip() {
if (WeaponClassToLoad.IsPending()) {
WeaponClassToLoad.LoadSynchronous(); // Or use StreamableManager for async
}
}Debugging
调试
- Logging: Use with custom categories.
UE_LOGcppDEFINE_LOG_CATEGORY_STATIC(LogMyGame, Log, All); UE_LOG(LogMyGame, Warning, TEXT("Health is low: %f"), CurrentHealth); - Screen Messages:
cpp
if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Died!")); - Visual Logger: extremely useful for AI debugging. Implement .
IVisualLoggerDebugSnapshotInterface
- 日志记录:使用 并定义自定义日志类别。
UE_LOGcppDEFINE_LOG_CATEGORY_STATIC(LogMyGame, Log, All); UE_LOG(LogMyGame, Warning, TEXT("Health is low: %f"), CurrentHealth); - 屏幕消息:
cpp
if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Died!")); - Visual Logger:对 AI 调试极为有用。实现 。
IVisualLoggerDebugSnapshotInterface
Checklist before PR
PR 前检查清单
- Does this Actor need to Tick? Can it be a Timer?
- Are all members wrapped in
UObject*?UPROPERTY - Are hard references (TSubclassOf) causing load chains? Can they be Soft Ptrs?
- Did you clean up verified delegates in ?
EndPlay
- 这个 Actor 是否需要 Tick?能否改用 Timer?
- 是否所有 成员都使用了
UObject*包裹?UPROPERTY - 硬引用(TSubclassOf)是否造成了加载链?能否改为 Soft Ptr?
- 是否已在 中清理并核验委托?
EndPlay
Limitations
限制
- Use this skill only when the task clearly matches the scope described above.
- Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.
- 仅当任务明确符合上述范围时使用本 skill。
- 不要将输出视为替代针对具体环境的验证、测试或专家评审的手段。
- 如果缺少必要的输入、权限、安全边界或成功标准,请停止操作并请求澄清。