Component Initialization in Constructor
Header File
UCLASS()
class MYGAME_API AGamePawn : public APawn
{
GENERATED_BODY()
public:
UStaticMeshComponent* HighlightMesh;
AGamePawn();
};Source File
AGamePawn::AGamePawn() {
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootScene"));
HighlightMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("HighlightMesh"));
static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshFinder(TEXT("/Script/Engine.StaticMesh'/Engine/BasicShapes/Cube.Cube'"));
static ConstructorHelpers::FObjectFinder<UMaterial> MatFinder(TEXT("/Script/Engine.Material'/Game/Materials/M_Highlight.M_Highlight'"));
if (MeshFinder.Succeeded()) HighlightMesh->SetStaticMesh(MeshFinder.Object);
if (MatFinder.Succeeded()) HighlightMesh->SetMaterial(0, MatFinder.Object);
HighlightMesh->SetWorldScale3D(FVector(0.3f, 0.3f, 0.3f));
HighlightMesh->SetVisibility(false);
}Spawning Actors During Runtime
Header File
class AProjectileActor;
UCLASS()
class MYGAME_API AGamePawn : public APawn
{
GENERATED_BODY()
public:
AProjectileActor* ActiveProjectile;
};Source File
#include "ProjectileActor.h"
FTransform SpawnTransform(...); // Define location and rotation
ActiveProjectile = GetWorld()->SpawnActorDeferred<AProjectileActor>(AProjectileActor::StaticClass(), SpawnTransform);
// Finalize spawning after any necessary property initialization
ActiveProjectile->FinishSpawning(SpawnTransform);Applying Dynamic Materials and Parameters at Runtime
Header File
UCLASS()
class MYGAME_API AGamePawn : public APawn
{
GENERATED_BODY()
public:
UStaticMeshComponent* IndicatorMesh;
UMaterialInterface* BaseIndicatorMaterial;
UMaterialInstanceDynamic* RuntimeDynamicMaterial;
AGamePawn();
void ApplyIndicatorColor(const FLinearColor& NewColor);
};Source File
AGamePawn::AGamePawn() {
static ConstructorHelpers::FObjectFinder<UMaterial> MatRef(TEXT("/Script/Engine.Material'/Game/Materials/M_Indicator.M_Indicator'"));
BaseIndicatorMaterial = MatRef.Object;
}
void AGamePawn::ApplyIndicatorColor(const FLinearColor& NewColor) {
RuntimeDynamicMaterial = UMaterialInstanceDynamic::Create(BaseIndicatorMaterial, this);
RuntimeDynamicMaterial->SetVectorParameterValue(FName("BaseColor"), NewColor);
IndicatorMesh->SetMaterial(0, RuntimeDynamicMaterial);
}Invoking C++ Functions from Blueprints
Prefix the target function declaration in the header file with UFUNCTION(BlueprintCallable). After compilation, retrieve the target class instance within the Blueprint, cast it to the appropriate class, and execute the function.
Accessing C++ Variables from Blueprints
Prefix the target property declaration in the header file with UPROPERTY(BlueprintReadWrite). After compilation, obtain the class instance within the Blueprint, cast it to the appropriate class, and read or modify the exposed variable.
Executing User Widget Blueprint Functions from C++
To bypass the lack of a visual UI designer in pure C++ UserWidgets, inherit a C++ base class from UUserWidget. Override specific C++ functions within the Blueprint widget. The Blueprint passes its reference back to the C++ system via a designated function, allowing C++ to invoke the overridden Blueprint logic.
GameHUDWidget.h
UCLASS()
class MYGAME_API UGameHUDWidget : public UUserWidget
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintNativeEvent)
void OnActionCompleted();
virtual void OnActionCompleted_Implementation() override {};
};GamePawn.h
class UGameHUDWidget;
UCLASS()
class MYGAME_API AGamePawn : public APawn
{
GENERATED_BODY()
public:
UGameHUDWidget* HUDReference;
UFUNCTION(BlueprintCallable)
void RegisterHUDWidget(UGameHUDWidget* WidgetRef = nullptr);
};GamePawn.cpp
#include "GameHUDWidget.h"
void AGamePawn::RegisterHUDWidget(UGameHUDWidget* WidgetRef) {
HUDReference = WidgetRef;
// Now you can call HUDReference->OnActionCompleted();
}Playing Audio Files
Header File
UCLASS()
class MYGAME_API AGamePawn : public APawn
{
GENERATED_BODY()
public:
USoundWave* ImpactSoundArray[5];
AGamePawn();
void PlayImpactNoise();
};Source File
#include "Sound/SoundWave.h"
const TArray<FString> ImpactPaths = {
TEXT("/Script/Engine.SoundWave'/Game/Audio/Impacts/I1.I1'"),
TEXT("/Script/Engine.SoundWave'/Game/Audio/Impacts/I2.I2'"),
TEXT("/Script/Engine.SoundWave'/Game/Audio/Impacts/I3.I3'"),
TEXT("/Script/Engine.SoundWave'/Game/Audio/Impacts/I4.I4'"),
TEXT("/Script/Engine.SoundWave'/Game/Audio/Impacts/I5.I5'")
};
AGamePawn::AGamePawn() {
for (int32 Idx = 0; Idx < ImpactPaths.Num(); ++Idx) {
ConstructorHelpers::FObjectFinder<USoundWave> SoundRef(*ImpactPaths[Idx]);
if (SoundRef.Succeeded()) {
ImpactSoundArray[Idx] = SoundRef.Object;
}
}
}
void AGamePawn::PlayImpactNoise() {
const FVector Location = FVector::ZeroVector; // Placeholder for spawn location
int32 RandIdx = FMath::RandRange(0, 4);
UGameplayStatics::PlaySoundAtLocation(this, ImpactSoundArray[RandIdx], Location);
// Alternative: Runtime dynamic loading
FString AmbientPath = TEXT("/Script/Engine.SoundWave'/Game/Audio/Ambient/A1.A1'");
USoundWave* AmbientSound = LoadObject<USoundWave>(this, *AmbientPath);
if (AmbientSound) {
UGameplayStatics::PlaySound2D(GetWorld(), AmbientSound);
}
}