I have the an actor who is firing bullets and I have a score widget. I want to update the score when the bullet hits the target. I want to do this using C++. Thus far I can update the score when the widget is initialized.
// Fill out your copyright notice in the Description page of Project Settings.
#include “MyUserWidget.h”
#include “Components/TextBlock.h”
void UMyUserWidget::NativeConstruct()
{
Super::NativeConstruct();
// Here is where I typically bind delegates,
// and set up default appearance
TextBlock_0->SetText(FText::FromString(TEXT("Hello world!")));
}
// Fill out your copyright notice in the Description page of Project Settings.
#include “ShootTarget.h”
#include “BulletActor.h”
#include “FloatingActor.h”
// Sets default values
AShootTarget::AShootTarget()
{
// 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;
StaticMesh = CreateDefaultSubobject(FName(“StaticMesh”));
}
// Called when the game starts or when spawned
void AShootTarget::BeginPlay()
{
Super::BeginPlay();
StaticMesh->OnComponentHit.AddDynamic(this, &AShootTarget::HitMesh);
}
void AShootTarget::HitMesh(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp,
FVector NormalImpulse, const FHitResult& Hit)
{
ABulletActor* TempBullet = Cast(OtherActor);
if (TempBullet)
{
iCurrentScore += 1;
AFloatingActor* TempChar = Cast<AFloatingActor>(TempBullet->GetOwner());
if (TempChar)
{
UE_LOG(LogTemp, Warning, TEXT("target hit"));
}
}
}