Player Controller Possess Event

How do you determine when a PlayerController has possessed a Pawn? Is there an event for this?

My PlayerController creates a HUD widget on Event BeginPlay. The HUD then needs to draw information from the Pawn that is possessed by the PlayerController. The problem is that the PlayerController doesn’t have a possessed Pawn at that stage. UE4 seems to set up the Pawn possession at some point after the PlayerController is created, which makes sense. I would like to “listen” in for that event so I can respond at the moment that UE4 assigns a Pawn to the PlayerController. Then I can notify the HUD of the new Pawn to start drawing information from to display. If at some point during game play the PlayerController changes Pawn, I can then also respond to that using the same event to notify the HUD of the new Pawn.

1 Like

maybe there is some smarter way but in any case you can create a event dispatcher and when you override the specific functions in your gamemode you can call the event dispatcher at the right time

(ue4 spawns a default pawn specified in your gamemode and you can override those specific functions inside your gamemode)

by the way why not create the widget from your hud class specified in your gamemode ?

So your saying that I should override Spawn Default Pawn For in my custom GameMode? If I override that function, then I also need to provide the functionality for creating the pawn as well. There doesn’t seem to be a parent Spawn Default Pawn For I can call to let the base GameMode create the pawn and do whatever else it needs to do, unless I’m missing it?

I’m actually not really sure where the best place is to create the HUD widget for the player. This is a multiplayer game and your comment has reminded me that the PlayerController is created for every player on the server, and I don’t want the server creating a HUD widget for each player as it should just be a client-side only HUD. So perhaps the PlayerController is not the best place after all. Could you elaborate more on where I could create the HUD widget in the GameMode for each player as they join the game?

Actually, a custom HUD blueprint is probably the best place to create it. Since each player has a HUD blueprint and the server doesn’t create one of these for each player.

Still stuck on how to determine when the PlayerController possesses a Pawn though?

you can specify a hud class inside your gamemode so if you create a new blueprint and derive from “hud”.
everything you create there will be client side like a hud usually is.

if you want to replicate a widget for a multiplayer widget you need to create a custom event and setup your replication stuff there.
if you have problems creating the hud inside the event begin, add a small delay to get sure everything has been spawned and created

creating a pawn is quite simple, use the “Spawn actor from class” node and then use your playercontroller->Posses to posses the spawned pawn. This could be the place where you can call your Event Dispatcher.
Also there is a flag “spawn as spectator” inside your gamemode which can be pretty useful if you want to have custom spawn.
check out this tutorial maybe it helps:

Guys, in C++, you should override AController::SetPawn method. It is the only virtual method that is guaranteed to fire as soon as the controller possesses a pawn.

This is what i’m looking for

Is there an equivalent in blueprint please ?

I would also like to know this as well. I’m left to do logic in the tick event until my controller is controlling a pawn =(