I’m working on a project that requires me to code a user HUD with a scoring system based on pickups, but the tutorial I’ve followed shows me how to create the pickup, and not translate the additional score into my widget, so I figured I’d come here and seek out help. Basically, I’ve created a coin that is supposed to award the user a score award, and then display that score on the main viewport via a HUD. I’m pretty sure I have to do something with UUserWidget and UPROPERTYs, but I’m unsure how to approach the task. Here is my code:
Coin.cpp:
#include "Coin.h"
// Sets default values
ACoin::ACoin()
{
//Declaring variables for the pickup.
fg_bCollected = false;
fg_Pitch = 0.f;
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
//Root for the pickup item.
fg_Root = CreateDefaultSubobject<USceneComponent>(TEXT("Scene Root"));
//Static Mesh compontent.
fg_StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
fg_StaticMesh->SetupAttachment(fg_Root);
//Particle system.
static ConstructorHelpers::FObjectFinder<UParticleSystem>pickupFX(TEXT("/Game/InfinityBladeEffects/Effects/FX_Forging/P_Buff_Loop"));
fg_ParticleSystem = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("PickupEffect"));
fg_ParticleSystem->SetTemplate(pickupFX.Object);
fg_ParticleSystem->SetWorldScale3D(FVector(3.0f, 3.0f, 3.0f));
//Sphere trigger.
fg_SphereCollider = CreateDefaultSubobject<USphereComponent>(TEXT("SphereTrigger"));
fg_SphereCollider->SetGenerateOverlapEvents(true);
fg_SphereCollider->SetWorldScale3D(FVector(1.0f, 1.0f, 1.0f));
fg_SphereCollider->OnComponentBeginOverlap.AddDynamic(this, &ACoin::fg_OnOverlapBegin);
fg_SphereCollider->AttachToComponent(fg_Root, FAttachmentTransformRules::SnapToTargetNotIncludingScale);
}
// Called when the game starts or when spawned
void ACoin::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ACoin::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
//Adding rotation to the pickup.
FRotator rotation = FRotator(fg_Pitch, 0.f, 0.f);
FQuat quaternion = FQuat(rotation);
fg_Pitch += 0.1f * DeltaTime;
//Applying rotation.
AddActorLocalRotation(quaternion, false, 0, ETeleportType::None);
//Check if the pickup has been collected.
if (fg_bCollected == true) {
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), fg_ParticleSystem->Template,
GetActorLocation());
Destroy();
}
}
//Logic to handle pickup and set bCollected to true.
void ACoin::fg_OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) {
if ((OtherActor != nullptr) && (OtherActor != this) && (OtherComp != nullptr)) {
fg_bCollected = true;
Score += 100;
}
}
Coin.h**:**
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "Engine.h"
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/SphereComponent.h"
#include "Components/SceneComponent.h"
#include "Kismet/GameplayStatics.h"
#include "Particles/ParticleSystemComponent.h"
#include "CoreUObject/Public/UObject/ConstructorHelpers.h"
#include "Coin.generated.h"
UCLASS()
class GAM312_PROJECT_API ACoin : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ACoin();
private:
//Bool value to store whether coin has been collected or not.
bool fg_bCollected;
//Float value to control rotation on pitch.
float fg_Pitch;
//Int value to control Score.
int Score = 0;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
//Declaring necessary UPROPERTY values for static mesh and particle components.
UPROPERTY(EditAnywhere, Category = "Components")
USceneComponent* fg_Root;
UPROPERTY(EditAnywhere, Category = "Components")
UStaticMeshComponent* fg_StaticMesh;
UPROPERTY(EditAnywhere, Category = "Components")
USphereComponent* fg_SphereCollider;
UPROPERTY(EditAnywhere, Category = "Components")
UParticleSystemComponent* fg_ParticleSystem;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
//Creating overlap UFUNCTION.
UFUNCTION()
void fg_OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
};
I appreciate any help and insight into helping me solve this issue for my game.