Loading...
Loading...
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.
npx skill4agent add gamedev-skills/awesome-gamedev-agent-skills unreal-cpp-gameplayAActorAPawnACharacterAGameModeBaseUActorComponentUPROPERTYUFUNCTION*.Build.csSource/*.h*.cppUCLASS*.Build.csunreal-blueprintsunreal-enhanced-inputunreal-behavior-treesAUUObjectFEIUCLASS()GENERATED_BODY()#include "ClassName.generated.h"UPROPERTYUFUNCTIONBlueprintCallableCreateDefaultSubobject<T>(TEXT("Name"))RootComponentAGameModeBaseAPawnACharacterAPlayerControllerUActorComponent*.Build.csEnhancedInputCtrl+Alt+F11// 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(); }// MyGameMode.cpp — set in the constructor so the engine spawns your classes.
AMyGameMode::AMyGameMode()
{
DefaultPawnClass = AMyCharacter::StaticClass();
PlayerControllerClass = AMyPlayerController::StaticClass();
}// MyGame.Build.cs
PublicDependencyModuleNames.AddRange(new string[]
{
"Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput"
});generated.hGENERATED_BODY()UObject*UPROPERTYUPROPERTYTObjectPtrUCLASSUPROPERTYUFooAFooBuild.csPublicDependencyModuleNamesSuper::BeginPlayTickUActorComponentUPROPERTYTObjectPtrTArray<TObjectPtr<>>AddToRootreferences/components-and-gc.mdhttps://dev.epicgames.com/documentation/en-us/unreal-engine/gameplay-framework-in-unreal-engineunreal-blueprintsunreal-enhanced-inputunreal-behavior-trees