I’m doing a multiplayer game and i’m trying to get the player’s controller so that i can spawn my UMG class.
This is what happens on BeginPlay() :
AMyPlayerController* Con = Cast<AMyPlayerController>(GetController());
if (Con) {
GLog->Log("Added HUD 2");
Con->BuildHUD();
}
AMyPlayerController is my player controller class.
I also tried using the condition IsLocallyController before this piece of code, but apparently it’s always false.
Also tried casting the class to pawn and the getting the pawn’s controller, which didn’t work either.
How do i successfully get the player’s controller in a network?
Don’t worry about local controllers and such. Remember that PlayerControllers only truly exist on the client and do not replicate. You should do a NetMode check instead. The reason we do this is because listen servers could also be a local client, and in my experience IsLocalController is super buggy. You don’t need to use IsLocalController in this context anyway.
Tried it and this GetNetMode() thing will come very handy but it still won’t print that log… it’s failing the casting for some reason… thanks for the help btw appreciate the help on my other post
I created a blueprint from the GameMode class and set that as the GameMode, also created a blueprint from the player controller class and set that as the player controller.
was being executed in my gamemode class, but i tried deleting it and setting the player controller class in the blueprint that i created from the gamemode.
The original code from my question is being executed on my character.
Not insulting at all this is probably not working because of something i’m missing anyway…
This question will sound really insulting and I don’t mean it that way. Are you performing this line of code in the class that the PlayerController is possessing or are you trying to reference the PlayerController from another unit’s class?
So here are the outlined steps for getting a custom character and controller going. If you have followed them all I am completely baffled at what the issue could be.
Create a custom game mode and assign it to your level in the World Properties panel.
Assign your custom pawn character in this game mode.
Assign your custom player controller in this game mode.
One of these might be happening:
Your chosen DefaultPlayerController class is being overridden in your blueprint game mode.
You edited the spawning code in your game mode.
The controller is null on begin play (which I highly doubt).
Is the result of GetController() (without the cast) nullptr as well?
Unfortunately I’ve never encountered the first error. If the Controller is null in BeginPlay but not in Tick you might need to hack loading the GUI (I do the same thing in my app for other reasons).
It’s ok thanks for the help anyway! actually adding the hud through the hud class doesn’t work the same way so i have to do it through the player controller! getting a lot of annoying issues in this project that i can’t fix