I am working on creating a simple coin-pickup game using UE5 and C++.
I watched a YouTube video, and I wrote codes following the video. In the video, there is no error message, but there is in mine.
I am new to UE5 and C++. Also, I am new to post a question in this community so I am not sure if it is ok to post whole code here or not, but I will.
Why GENERATED_BODY() got error in my codes? Actually, the codes in the video, this line does not have error message.
Using Visual Studio.
the error message that the line got: this declaration has no storage class or type specifier.
Header file
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "CPT_CoinPickupActor.generated.h"
#include "NiagaraFunctionLibrary.h"
#include "NiagaraComponent.h"
class UStaticMeshComponent;
class URotatingMovementComponent;
class USphereComponent;
class UNiagaraSystem;
class USoundBase;
UCLASS()
class COINPICKUPTUTORIAL_API ACPT_CoinPickupActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ACPT_CoinPickupActor();
UFUNCTION()
void OnBeginOverlapComponentEvent(
UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult
);
protected:
UPROPERTY(EditDefaultsOnly, Category = "Coin Pickup Tutorial")
TObjectPtr<UStaticMeshComponent> MeshComponent;
UPROPERTY(EditDefaultsOnly, Category = "Coin Pickup Tutorial")
TObjectPtr<URotatingMovementComponent> RotatingMovementComponent;
UPROPERTY(EditDefaultsOnly, Category = "Coin Pickup Tutorial")
TObjectPtr<USphereComponent> ColliderComponent;
UPROPERTY(EditDefaultsOnly, Category = "Coin Pickup Tutorial")
TObjectPtr<UNiagaraSystem> OnPickupEffect;
UPROPERTY(EditDefaultsOnly, Category = "Coin Pickup Tutorial")
TObjectPtr<USoundBase> PickSound;
UPROPERTY(EditDefaultsOnly, Category = "Coin Pickup Tutorial")
float VolumeMultiplier{ 0.5 };
UPROPERTY(EditDefaultsOnly, Category = "Coin Pickup Tutorial")
float PickEffectSpawnOffset{ 90 };
};
CPP
#include "CPT_CoinPickupActor.h"
#include "Components/SphereComponent.h"
#include "GameFramework/Character.h"
#include "GameFramework/RotatingMovementComponent.h"
#include "Kismet/GameplayStatics.h"
#include "NiagaraFunctionLibrary.h"
#include "NiagaraComponent.h"
// Sets default values
ACPT_CoinPickupActor::ACPT_CoinPickupActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
ColliderComponent = CreateDefaultSubobject<USphereComponent>("ColliderComponent");
SetRootComponent(ColliderComponent);
ColliderComponent->SetGenerateOverlapEvents(true);
ColliderComponent->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
ColliderComponent->SetCollisionResponseToAllChannels(ECR_Ignore);
ColliderComponent->SetCollisionResponseToChannel(ECC_Pawn, ECR_Overlap);
ColliderComponent->OnComponentBeginOverlap.AddDynamic(
this, &ACPT_CoinPickupActor::OnBeginOverlapComponentEvent
);
MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>("MeshComponent");
MeshComponent->SetupAttachment(ColliderComponent);
MeshComponent->SetCollisionEnabled(ECollisionEnabled::NoCollision);
MeshComponent->SetCollisionResponseToAllChannels(ECR_Ignore);
MeshComponent->SetGenerateOverlapEvents(false);
RotatingMovementComponent = CreateDefaultSubobject<URotatingMovementComponent>("RotatingMovementComponent");
}
void ACPT_CoinPickupActor::OnBeginOverlapComponentEvent(
UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult
)
{
if (!Cast<ACharacter>(OtherActor)) return;
if (PickSound)
{
UGameplayStatics::PlaySoundAtLocation(
this, PickSound, GetActorLocation(), VolumeMultiplier);
}
if (OnPickupEffect)
{
const FVector Offset = GetActorUpVector() * PickEffectSpawnOffset;
UNiagaraFunctionLibrary::SpawnSystemAtLocation(
this, OnPickupEffect, OtherActor->GetActorLocation() + Offset);
}
Destroy();
}
Build file
using UnrealBuildTool;
public class CoinPickupTutorial : ModuleRules
{
public CoinPickupTutorial(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "EnhancedInput" });
PrivateDependencyModuleNames.AddRange(new string[] { "Niagara" });
}
}