Help with displaying 4 player health bars

I’m having a small issue with a HUD prototype I’m working on. Currently I have a shared camera with 4 health bars at the bottom of the screen, each representing the health of any of the 4 players in the game. I was able to get the health to properly display on the screen by creating the HUD in the player controller, and then calling from the player’s characters in the HUD itself like so.

The problem I am running into is that when the player’s health becomes affected, it’s not ordering it correctly on the screen, and instead will order them as shown:

The health bars are all mixed up and completely out of order with an exception to Player 1’s screen. (This is being run on a dedicated server, as you might be able to tell). I’m hoping to fix the order of the health bars on every player’s screen to repeat what is being seen on Player 1’s screen.

Thanks in advance for any help!

I think this is happening because the call to Get All Actors of Class is assuming that the returned BP_Player array is sorted in ascending order (Player1…Player4) and identically on all clients, which seems to not be the case. The quickest fix is to find a way to ensure that you’re calling Set Player Percent on the correct BP_Player. Another solution that follows the gameplay framework design more closely is to store Health info inside the PlayerState class, which can be easily retrieved on each client through the PlayerArray property on the GameState class. I think this array is sorted as you expect.

295979-annotation-2020-01-30-153250.png

Also, calling Get All Actors of Class is pretty slow operation and should be avoided inside a tick function.
Hope that helps.

Thank you, I’ll try it when I get home later

to add to that: the player state manages a unique player ID, which can be used for consistent sorting of the health bars

296005-anmerkung-2020-01-30-181902.png

With your help gentlemen, I was able to achieve the results I required.

In my gamemode I created a “Player Number” variable and assigned the integer to the length of the player array from the game state. I then went into my HUD and got the player array from the game state and for each loop I casted to the player state. Lastly, I ran a bool to check if the Player number from the player state was equal to the player I wanted, and if it was I then set the percentage of their health to that particular health bar.

Behold:

Glad it worked!

As a followup, it appears that calling player vitals variables from the player state into the HUD is causing a strange latency issue. It appears that the action of taking damage will call a print string instantly, but it takes about 1 second for the player’s health bar to update. I eliminate the issue by storing the player’s vital’s variables in the player’s pawn and then calling the “Pawn private” variable from the player state and casting it to the player’s blueprint in the HUD for the correct order.

I have also eliminated tick events for updating the HUD and instead ran a timer on loop for .001 seconds when constructing the HUD.

Good to know. Thanks for updating!