Assigning the player controller for multiplayer inside pawn class

Hey Everyone

I’ve been working on a game for a while and I cannot seem how to figure out something that seems like it should be pretty straight forward.

In my pawn class I have just been using the following method to get my player controller:


PlayerController = GetWorld()->GetFirstPlayerController();

This works fine when working for a single player experience. And I even get lucky and it works for multiplayer with how I’m replicating my movement. A problem has arisen, however, because now I’m working on my HUD and I need to get the HUDs pawn by using the Player Controller. Obviously getting the FIRST player controller will not work.

In my HUD class I’m using the following proceedure to access the pawn:



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


Then I am drawing a line on the screen. I can see the line on the first players screen, but I cannot see it on others. In Fact, the draw function causes a crash unless I do the following check


if (!Player) return;

What I’ve tried

I found a post that mentioned using the following loop:




	for (FConstPlayerControllerIterator Iterator = GetWorld()->GetPlayerControllerIterator(); Iterator; ++Iterator)
	{
		PlayerController = *Iterator;

	}


This does compile but the behavior with the HUD is unchanged. I return out or I get the crash, same as using the first iterator.

I have tinkered with replicating this loop but when I do that I get really strange crashes. I’m not sure if that means it’s working and I’ve implemented other things wrong or if it’s just a really terrible idea.

My big question is How do I assign a player controller in my pawn class that I can use in my HUD class that will be valid with multiplayer?
If that’s not possible (or just a stupid thing to do) what is the proper way to assign player controllers to pawns for use in multiplayer?
I would rather not write a custom player controller class since my controls are very simple and complete within the pawn class BUT if that is the best thing to do or the only way to make it work I

Hello, you should be able to get the controller of the Pawn by using GetController() and casting it to your playercontroller class.

That does compile but in my HUD my pawn’s player controller is still NULL for player 2.