x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

When is it safe to access PlayerArray in GameState after a new Login to a multiplayer game?

Hey all! I am making a lobby for my multiplayer game. The lobby should show player names in two lists based on what team they are on. The host can start the game when they want or when the game is full. I'm running into an issue with updating my list of players. In my GameMode class I override PostLogin to add the new playercontroller to one of two teams located in my GameState class using a method called AddPlayerToTeam in the GameState class. The GameState method then calls a multicast function so that the host and all clients know that a new player has joined. This function simply broadcasts a multicast delegate. The UMG widget on each player has an overriden NativeConstruct method that adds a method to the multicast delegate in the GameState. This method updates the team lists now that the new player has joined. Below is the method that gets called when the UMG widget is constructed and when the multicast delegate mention above is broadcast.

 if (!ensure(TeamOneList && TeamTwoList && PlayerRowClass && GetWorld())) { return; }

 TeamOneList->ClearChildren();
 TeamTwoList->ClearChildren();

 for (APlayerState* PlayerState : GetWorld()->GetGameState()->PlayerArray)
 {
     UBoiLobbyPlayerWidget* Row = CreateWidget<UBoiLobbyPlayerWidget>(GetWorld(), PlayerRowClass);
     Row->PlayerName->SetText(FText::FromString(PlayerState->GetPlayerName()));

     ABoiPlayerState* BoiPlayerState = Cast<ABoiPlayerState>(PlayerState);
     if (BoiPlayerState && BoiPlayerState->GetTeam() == 0)
     {
         TeamOneList->AddChild(Row);
     }
     else if (BoiPlayerState && BoiPlayerState->GetTeam() == 1)
     {
         TeamTwoList->AddChild(Row);
     }

 }

The lists update correctly on my host. The first client to join the lobby doesn't see any players in either list. If a 2nd client joins, the 1st client's lists update and show the 1st client and the host. The 2ndd client shows itself on the list but neither the host or 1st client appear on the list. If a 3rd client joins, it behave similar to the 2nd client, but the 1st and 2nd clients will now see the host, the first client, and the 2nd client. While debugging, when I get to the for loop I pasted above, the PlayerArray doesn't seem to have been replicated yet. Does anyone have any advice? Or know a method that gets called when the PlayerArray is replicated that I can link to? It appears that I'm trying to update my list too early, before the client has received the list of other players.

TLDR: Trying to update a list of players on UMG widgets when a new player joins a game. PlayerArray isn't replicated in time. What can I do to ensure that the playerarray is updated so that I can update my list appropriately?

Product Version: UE 4.19
Tags:
more ▼

asked Jun 19 '18 at 02:47 AM in C++ Programming

avatar image

Roy_Pogi
1 1 2

(comments are locked)
10|2000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question