HUD not working with multi-player. C++

Information

I am working on getting mult-player and have been working on the HUD. Right now I’m just mocking up a simple HUD with the Canvas.

I am using a pawn class as my main “player”

I am using the built-in multi-player testing right from the client in the engine. I am testing with 2 players but have done tests with more.

I have linked my HUD through my game mode: HUDClass = ATestHUD::StaticClass();

I can successfully draw to the “1st” Player ONLY using the canvas function (For example: Canvas->K2_DrawLine(...))

The Problem

All subsequent players beyond the “1st” Player (who is also the server, and NO players if I have “Run Dedicated Server” Checked) are inaccessible in my HUD class.

What I am doing

I get my pawn Like so:

void ATestHUD::BeginPlay()
{
	Super::BeginPlay();

	if (GetOwningPlayerController() != NULL)
	{
		Player = Cast<ATestPawn>(GetOwningPlayerController()->GetPawn());
		
	}

}

I then use the “Player” variable to get the screen location of the mouse and so forth. As stated. This works well for the “1st” player.

I then do the following:

void ATestHUD::DrawHUD()
{
	Super::DrawHUD();
	MyDraw();

}

void ATestHUD::MyDraw()
{
	if (!Player) return;

	Canvas->K2_DrawLine(Player->GetPlayerScreenLocation(), Player->GetFireDirection() * 550.0f + playerLoc, 1.5f, FLinearColor(0.4f, 0.4f, 0.4f, 0.1f));
}

The small check if (!Player) return; saves me from crashes but is also the reason why subsequent players are not getting their HUDs drawn. So Obviously my Player variable is only valid for the “1st” Player.

How can I make it so I can draw my HUD on anyone who jumps in the game?

Thank you for checking it out.

I have solved this problem. I don’t know if anyone else will have the same problem but I’ll try to give an explanation of what went wrong and how I managed to fix it.

Essentially the problem lies with my using the BeginPlay() Function.

It works perfectly for the first person to load because once he’s ready to play that function is called.

Subsequent players can show up any time, but by then BeginPlay() is long since called and it won’t be setting the pawn for you anymore.

I solved this by having my PAWN tell my HUD who he is. I did so like this in my pawn class:

auto hud = PlayerController->MyHUD;
auto TestHUD = Cast<ATestHUD>(hud);
TestHUD->Player = this;

I do this in my SetupPlayerInputComponent(...) Function. I don’t know if that’s the perfect place to do it but it’s late enough that the HUD is ready and waiting.

Now, anytime a new player enters the game, He tells the HUD who he is, and DrawHUD function (checking to see if a player exists) can finally see that the player does exist and begin drawing his HUD.

I hope this helps anyone else having this problem.

Thank you.