Multiplayer: I have a (base) class that inherits from pawn. I have a player controller class that inherits from PlayerController. In the player controller class I have a reference to an instance of the pawn base class so I don’t have to cast to the base class each time I do something in the controller using this variable
This variable IS replicated. Why? The setup I have does NOT use a movement component on the pawn. Instead, input is captured in the controller and calls events on the pawn for manipulation. I use a SwitchHasAuthority macro at the end of each function in the pawn to run on server / multicast. This seems to work as I can test and see movement on clients and server.
Specifically, In GameMode, I drag off the controller from OnPostLogin and call Possess. Back in the Player Controller, in OnPossessed, I use the passed in Pawn, cast it to the appropriate type, and set the replicated variable. I then call a FUNCTION (not an event) to create a couple of Widgets. It is here I have problems. The problem comes when I want to access this variable from Client PlayerControllers.
I have a Main widget, and inside that there lives another Widget. The Main and Sub Widget have variables to refrence the pawn and are exposed on Create Widget. So, after setting the replicated variable in OnPossess on player controller, I use this variable as an argument to the Widget variable. It works perfectly fine on the server, but on the client the variable is null. I can’t quite understand why and i’ve been reading documentation, trying to find posts, anything that could help me. I have exhausted myself.
I am reaching out to you for help.
Sone things I have tried:
Instead of calling the function from Controller.OnPossess, In the GameMode after I call Possess on Controller target, I call an event on PlayerController set to RunOnOwningClient. This calls the function, but has the exact same outcome: client shows variable is null while server’s variable (and setup) are good.
In the Widget, I can use GetOwningPlayerPawn, but cannot use that on Initialize or Construct as it does not return anything. In Tick, I can test to see if the Widget pawn base VARIABLE is valid, and if not, then call GetOwningPlayerPawn, cast it, and set the Widget variable. Obviously this is a hack with tick checking this every frame. I did it as a test…
On the controller side, I call GetControlledPawn but on the client, that’s null (on server that function works fine).