Hi UE4-ers, I’m totally stumped as to why my score won’t update. I’ve created an Actor class which includes code to detect when a character collides with it. When collision takes place, a score should decrement from the previous score. I’m then sending the score to the AMyHUD class, where a function is called to display the current score. I initialise the score to 10, but on each hit of the Actor, the score remains at 9. I’ve blueprinted the Actor class and placed about 5 blueprint instances into the world. What the **** is going on?
MyActor.h
UCLASS()
class SIDESCROLLER1_API AMyActor : public AActor
{
GENERATED_BODY()
protected:
**int32 nLivesRemaining;**
public:
// Sets default values for this actor's properties
AMyActor(const FObjectInitializer &objectInit);
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = collider)USphereComponent *pSphere;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = collider)UStaticMeshComponent *pMesh;
UFUNCTION(BlueprintNativeEvent, Category = collider)
void OnHit(AActor *OtherActor, UPrimitiveComponent *OtherComponent, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult);
};
Here’s my MyActor.cpp
AMyActor::AMyActor(const FObjectInitializer &objectInit):Super(objectInit)
{
// 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;
pSphere = objectInit.CreateDefaultSubobject<USphereComponent>(this, TEXT("sphere"));
RootComponent = pSphere;
pSphere->bGenerateOverlapEvents = true;
pSphere->OnComponentBeginOverlap.AddDynamic(this, &AMyActor::OnHit);
pSphere->SetSphereRadius(80);
pMesh = objectInit.CreateAbstractDefaultSubobject<UStaticMeshComponent>(this, TEXT("Mesh"));
pMesh->AttachTo(pSphere);
**nLivesRemaining = 10;**
}
MyHUD.h
UCLASS()
class SIDESCROLLER1_API AMyHUD : public AHUD
{
GENERATED_BODY()
public:
AMyHUD();
****[COLOR="#FF0000"]void PrintScores(int32 lives);**[/COLOR]**
};
Here is the implementation of the OnHit Function in MyActor.cpp showing the decrement of nLivesRemaining, and a call to the function to display in MyHUD.cpp
void AMyActor::OnHit_Implementation(AActor *OtherActor, UPrimitiveComponent *OtherComponent, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
{
if (Cast<AsideScroller1Character>(OtherActor) == nullptr)
{
return;
}
this->Destroy();
**this->nLivesRemaining-=1;**
APlayerController *pPlayerController = GetWorld()->GetFirstPlayerController();
if (pPlayerController) {
AMyHUD *hud = Cast<AMyHUD>(pPlayerController->GetHUD());
**hud->PrintScores(nLivesRemaining);**
}
}
The Implementation of PrintScores in MyHUD.cpp
void AMyHUD::PrintScores(**int32 lives**)
{
FString Message;
if (lives < 0) {
Message = "You have No more lives remaining";
GEngine->AddOnScreenDebugMessage(0, 5.f, FColor::Cyan, FString(Message));
}
else
{
GEngine->AddOnScreenDebugMessage(0, 5.f, FColor::Cyan, FString::Printf(TEXT("You have %d lives remaining"), **lives**));
}
}
It all works fine (decrements on each hit) if there is only one instance of the blueprinted class. However when I place say 5 into the world the score just remains at 9.
any help is very much appreciated. Thanks