[C++] If an actor has a collision hit (OnHit()), how do I call on AHUD to draw text on the screen?

I understand that I need to call on an overridden DrawText() in a subclass of AHUD. I also understand that I can pass a delegate function in a subclass of Actor by adding a dynamic multicast delegate.

However, both of these, subclass of HUD and the subclass of Actor, are two different, separate actors. One handles the text displayed on the screen, while the other is an actor I assigned as a block in a flat area that the player is tasked to spawn a projectile and hit it with the projectile.

How do you retrieve the event that fires when a projectile shot by the player to hit the Actor subclass, so that this event will pass a message to the HUD subclass and draw a string of text onto the display?

I have looked thoroughly in AnswerHub, and I couldn’t seem to find the answers that I wanted in regards to passing event messages between two separate Actors, especially with HUD and Actor in general. If one could show me how this is done, just the HUD and Actor part, I will be grateful. Anything else can be relevant, so that I can learn to apply it to other different Actors, such as Actors with Pawns, Character with another Pawn (NPC conversations, etc.).

Thanks in advance.

GetOwningPawn() returns the Pawn actor that was set in the GameMode.cpp.

Since the HUD class is set by in the GameMode.cpp, I don’t need to worry about getting the instance of the HUD class. Instead, when a collision hit has occurred, I need to call on Canvas in that actor’s OnHit function, create a CanvasTextItem and initialize it, finally put the CanvasTextItem into the Canvas->DrawItem() function for it to draw it to the HUD viewport, which is the screen.

// Get our vehicle so we can check if we are in car. If we are we don't want onscreen HUD
AGetDebtPawn* Vehicle = Cast<AGetDebtPawn>(GetOwningPawn());
if ((Vehicle != nullptr) && (Vehicle->bInCarCameraActive == false))
{
	FVector2D ScaleVec(HUDYRatio * 1.4f, HUDYRatio * 1.4f);

	// Speed
	FCanvasTextItem SpeedTextItem(FVector2D(HUDXRatio * 805.f, HUDYRatio * 455), Vehicle->SpeedDisplayString, HUDFont, FLinearColor::White);
	SpeedTextItem.Scale = ScaleVec;
	Canvas->DrawItem(SpeedTextItem);

	// Gear
	FCanvasTextItem GearTextItem(FVector2D(HUDXRatio * 805.f, HUDYRatio * 500.f), Vehicle->GearDisplayString, HUDFont, Vehicle->bInReverseGear == false ? Vehicle->GearDisplayColor : Vehicle->GearDisplayReverseColor);
	GearTextItem.Scale = ScaleVec;
	Canvas->DrawItem(GearTextItem);

So if i understand correctly, you need to execute an Event in your HUD that displays a string on screen when this projectile hit an Actor…right?

Yes. I feel like I need to work more on my explanation.

In YourActorClass.h add this:

float TakeDamage( float DamageAmount, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, AActor* DamageCauser ) override;

Then in YourActorClass.cpp add this:

float AYourActorClass::TakeDamage( float DamageAmount, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, AActor* DamageCauser )
{
    APlayerController* MyController = UGameplayStatics::GetPlayerController( GetWorld(), 0 )
    AYourHUD* MyHud = Cast<AYourHUD>(MyController->GetHUD());

    if (MyHud)
    {
        // Call MyHud->DrawText function
    }

    return Super::TakeDamage( Damage, DamageEvent, EventInstigator, DamageCauser );
}

Are you sure you can just draw from the TakeDamage function?
I thought you can only call Draw functions of a HUD object from within DrawHUD (virtual) function. Therefore you should queue information to the HUD and consume it in the (overriden) vritual DrawHUD function.