Announcement

Collapse
No announcement yet.

[Basic guide] How to select a Player Start in multiplayer

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    [Basic guide] How to select a Player Start in multiplayer

    I'm inexperienced with UE4, but I wanted to help any other newbies by presenting a basic solution to this problem which took me a while to solve.
    So, you want a simple multiplayer game, but the players seem to be spawning at random Player Starts. This means that half the time some of your players are not spawning! Here's how to fix it:

    Open your game mode. Hover your cursor over the plus sign next to 'Functions' on the left, select 'Override' and then click 'Choose Player Start'.
    Now, you can add logic to select an unused Player Start. You can create an array of actors and set it to the Player Starts array when BeginPlay is called in the Event Graph, like so:
    Click image for larger version

Name:	Screenshot (11).png
Views:	1
Size:	183.7 KB
ID:	1224532

    Then, you can go back to your ChoosePlayerStart function and get the first item, setting it as the currently chosen Player Start as an actor variable and removing it from the array, so that it won't be chosen again.
    Click image for larger version

Name:	Screenshot (8).png
Views:	1
Size:	288.0 KB
ID:	1224530

    Of course, it would be easy to use a random number for the index when getting a Player Start from the array:
    Click image for larger version

Name:	Screenshot (10).png
Views:	1
Size:	254.4 KB
ID:	1224531

    Now that you have the basic logic for a working system, you can improve upon it and adjust it to your needs. I hope this helps somebody
    Edit: this works fine when testing with a dedicated server. However, switching to a listen server seems to cause an issue that I haven't managed to fix.

    In C++, you can override the ChoosePlayerStart function with this:

    Header
    virtual AActor* ChoosePlayerStart_Implementation(AController* Player) override;

    And then define the function so that it returns an AActor* in the .cpp file.
    Last edited by TheGreatCabbage; 06-30-2017, 11:00 AM.

    #2
    Thanks for this! I'm fairly experienced with blueprint but somehow never thought of doing this.
    I do things.

    Comment


      #3
      OMG This is so helpful! Thx for sharing!

      Comment


        #4
        Really nice, thank you, Cabbage.

        Just a hint for anybody who is interested: It seems that BeginPlay is not guaranteed to run before ChoosePlayerStart, is it? (I tested with UE 4.18.) So maybe you have to set the variable PlayerStarts within ChoosePlayerStart and ensure the assignment is only done once by an additional bool variable.

        Comment


          #5
          Originally posted by phisigma View Post
          Really nice, thank you, Cabbage.

          Just a hint for anybody who is interested: It seems that BeginPlay is not guaranteed to run before ChoosePlayerStart, is it? (I tested with UE 4.18.) So maybe you have to set the variable PlayerStarts within ChoosePlayerStart and ensure the assignment is only done once by an additional bool variable.
          That is true. I am trying to run a dedicated server and Choose Player Start gets called before BeginPlay

          Comment

          Working...
          X