No announcement yet.

In a networked environment, when does the player's pawn actually get created?

  • Filter
  • Time
  • Show
Clear All
new posts

    Originally posted by Chumble View Post
    The creation of the HUD isn't the issue. Right now I'm creating it on the controller. The problem is the HUD relies on variables from the Pawn and trying to get the controller and pawn in sync is apparently a MASSIVE headache. I'm losing sleep over this. Seriously. I just want to continue with this project and I'm hung up on such a stupid part.

    Edit: Sorry if I'm kinda coming off as snippy. I'm very upset

    OK. I think I found another way to go about this. I didn't think I'd have to do this but now that I've done it this way.. it makes more sense.

    I focused primarily on the GameMode. I set the default pawn to be null.

    In the GameMode I used the Event OnPostLogin to spawn an actor of my character pawn. I then cast the "new player" pin to my player controller and assigned to it the spawned pawn.

    Then I used the pin from the spawned pawn to assign my reference to the player controller.

    Then I told the controller to possess the pawn and initialize the HUD. Seems to be working so far.
    Well yeah, using the OnPostLogin is a valid solution. I always spawn my Pawns by hand instead of using the GameMode. Just to have more control about it.

    "OnPostLogin"->"Spawn Pawn and Possess Server Side"->"RunOnOwningClient RPC to 'NewPlayer' PlayerController that tells the Client to Spawn the HUD".

    Other idea would be using the "OnPossessed" Events of the Pawn Class. Though that might only run on the Server, but you could do a Client RPC again.
    Open for contracted work | C++/BP (incl. Multiplayer) | Tutoring | VR

    My UE4 Blog/Page with Tutorials and more: Hit me for ALL the things!
    (Including 100+ Pages Multiplayer Network Compendium to get you started.)


      Originally posted by eXi View Post
      "OnPostLogin"->"Spawn Pawn and Possess Server Side"->"RunOnOwningClient RPC to 'NewPlayer' PlayerController that tells the Client to Spawn the HUD".
      This is exactly what I tried last night. In the controller, I create the HUD widget and save off a reference to it. For some reason on the client the created HUD widget would not return a valid reference. It's like it couldn't create it for the client for some reason. Just to be clear we're talking about the same process here...

      In GameMode:
      [OnPostLogin] --> [Authority? Needed here?] --> [Cast "New Player" to MyPlayerController] --> [Spawn MyCharacter Actor] --> [Save MyCharacter reference in MyPlayerController] --> [Save MyPlayerController reference in MyCharacter] --> [MyPlayerController possess MyCharacter] --> [Call "InitializeHud" in MyPlayerController]

      In MyPlayerController:
      [InitializeHUD (Run On Owning Client)] --> [Create HUD Widget] --> [Save HUD reference]

      I'm getting a little confused here in terms of variable replication as well. I'm not good at keeping a lot in my brain at the same time so trying to keep in mind who knows what when for each actor doesn't work very well. In GameMode, do I need to specify authority for the OnPostLogin? Your document states that the GameMode only exists on the server so I don't think Authority should be needed there. I'm pretty sure I will need to have the MyCharacter reference in MyPlayerController replicated though. If the GameMode is setting that reference on the server and the HUD (which only exists on the client) needs to access it, I will need to replicate it. I don't think I need to replicate the MyPlayerController reference on MyCharacter though.

      One thing that's been confusing me too is the methods of obtaining a player controller and pawn. On page 39 of your document you mention the use of "GetPlayerController(0)" which I was previously told never to use in a multiplayer environment. But your document seems to indicate that as long as it's not being run on a dedicated server, it will return the player controller being used for that client (or listen server "client").

      So I'm not sure what the best practice is here. In my GameMode, I have the absolute correct PlayerController and Character for each connecting player. I tell the controller who the player is and vice versa. Now, in the HUD I will also need to get a reference to MyCharacter. Should I go about this by referencing the PlayerController's reference? Or should I look to do a "GetPlayerController"->"Get Controlled Pawn"? It should give me the same reference either way but I'm almost tempted to say the first method is better because it ensures consistency. On the other hand, if something goes wrong with that reference, everything will break since it relies on it. Although I guess in that case the real problem is the reference not being available.


        GameMode only exists on the Server, ergo no Authority Check needed.

        Possessing should give the Controller, on Server and Client side, the "ControlledPawn", so you don't need to save it.
        From the Pawn side, you can do "GetController", which should also work on Clients.

        To get the correct Client Controller on a Client, you just need GetPlayerController0. Clients only have their own, so that's fine.

        You could also call "GetController" on the Pawn, but if the Pawn isn't controlled, that will be null. GetPlayerController0 will give you the correct
        PlayerController as long as its up and valid.

        So normally, there is no need to replicate Pawn/Controller references.

        You could check out my "Ballbump" Game. I think I got some HUD spawning when a new player joins. Maybe that helps:

        Open for contracted work | C++/BP (incl. Multiplayer) | Tutoring | VR

        My UE4 Blog/Page with Tutorials and more: Hit me for ALL the things!
        (Including 100+ Pages Multiplayer Network Compendium to get you started.)


          Here's my current setup:

          Click image for larger version

Name:	Enn2zHj.png
Views:	1
Size:	140.5 KB
ID:	1108360

          Click image for larger version

Name:	ccRoiLp.png
Views:	1
Size:	114.5 KB
ID:	1108361

          Click image for larger version

Name:	1WqoILF.png
Views:	1
Size:	35.8 KB
ID:	1108362

          I confirmed the GameMode code is executed once for the server and once for the client. But the InitializeHUD custom event that is set to run on owning client doesn't run for the client. Is it possible that the client does not own its own player controller? I don't know how I would fix it if it was.

          I'm downloading the ball bump project now to take a look at how that's setup.

          Edit: Ok .. so turns out I needed to make the InitializeHUD event reliable. I guess that makes sense. I'm not sure why it was consistently not working when it wasn't ticked off though. The HUD elements aren't working quite yet but at least that code is executing. One step closer.

          Edit2: GetControlledPawn in my HUD isn't returning a valid value on the client. I added a .2 second delay in my controller before it sets up the client's HUD and that seemed to be long enough to allow the value to propagate. I don't like that as a solution but I think it's the best I'm gonna get for now.

          Edit3: And.. everything works! I did have one more hiccup. In my InitializeHUD that was running only on the owning client, I was saving a reference to my HUD. But, since it runs only on the owning client, the server doesn't set that reference. I had code in place where the server would update a value and tell the HUD to refresh which I needed to change into a "Run on owning client" RPC before getting the HUD reference. I could just have the server save the HUD reference but I don't really need to.

          As of right now, everything is working just as it should. All my implemented features are functional. Time to make some more and break everything

          Thank you so much for your help on this. It's frustrating coming home after a long day of work and trying to relax with a project just to be stopped over and over. It makes my little free time much less fulfilling.
          Last edited by Chumble; 06-15-2016, 07:13 PM.