Game C++ Standards
1. Memory Management
- •Unreal Garbage Collection:
- •Use
UPROPERTY()on allUObject*member variables to prevent them from being garbage collected. - •Use
TWeakObjectPtr<>if you do not want to hold a hard reference.
- •Use
- •Smart Pointers:
- •For non-UObjects, use
TSharedPtr<>,TSharedRef<>,TUniquePtr<>. - •Avoid raw
new/deleteunless absolutely necessary (and wrapped immediately).
- •For non-UObjects, use
2. Unreal Header Tool (UHT)
- •Macros: Ensure
GENERATED_BODY()is at the top of the class. - •Reflection: Use
UPROPERTY()on variables you want to expose to Blueprints or the Editor.- •
EditAnywhere,BlueprintReadWrite,Category="Config"are standard.
- •
- •Functions: Use
UFUNCTION()to expose to Blueprints.- •
BlueprintCallable,BlueprintPure.
- •
3. Performance
- •Ticking:
- •Disable
PrimaryActorTick.bCanEverTick = false;if the actor does not need to update every frame. - •Use Timers (
GetWorld()->GetTimerManager()) for periodic events instead of checkingDeltaTimein Tick.
- •Disable
- •Casts:
- •Avoid
Cast<T>(Obj)inTick(). Cache references inBeginPlay().
- •Avoid
- •Logs:
- •Use
UE_LOG(LogTemp, Warning, TEXT("Message %s"), *StringVariable);sparseley in hot loops.
- •Use
4. Data Structures
- •TArray: Standard dynamic array.
- •TMap: Key-Value pairs.
- •TSet: Unique elements.
- •FString vs FName vs FText:
- •
FString: Mutable, expensive. Manipulation. - •
FName: Immutable, fast comparison. IDs/Keys. - •
FText: Localization. UI display.
- •
5. Code Style
- •Braces: BSD/Allman style (brace on new line).
- •Const: Use
constcorrectness whenever possible. - •Headers: Include minimal headers in
.h. Useforward declarations(class AMyActor;) to reduce compile times.