Different color healthbars for players

Good day everyone,

I’ve been bashing my head at this for a while to no avail. (multiplayer setup) My characters have a healthbar widget on top of their heads, what I want to do is that for the owning player, that healthbar is green, for allies, you see it as blue and for enemies as red. Naturally other players see themselves in green, their allies blue and enemies red.

I setup a teamID for each player state, but the problem I ran into was that when I was trying to get controller index 0, it worked for clients, but not for the server-client thats hosting.
Because of that im not able to properly compare whether the healthbar the server-client is seeing belong to itself or to another player.
And another issue I ran into was that player can join at different times in the game, and I didnt know how to make this logic retroactive so that new players see the ones that are already in the map correctly as allies or as enemies.

If my approach is wrong please tell me how you’d do it instead
Thank you

Hi @User-bcd85d3512 I think you’re making life harder for yourself by relying on Controller Index 0. In multiplayer, especially on a listen server, that’s usually not a great way to figure out who’s actually viewing the widget.
I’d keep the TeamID on the PlayerState (replicated) and let each client work out the colour locally:

  • Health bar belongs to me → Green
  • Same TeamID → Blue
  • Different TeamID → Red

I wouldn’t replicate the colour itself, because Player A might see someone as an ally while Player B sees them as an enemy. Just replicate the TeamID and calculate the colour client-side. And for players joining late, that should already be handled if the TeamID lives in PlayerState and is replicated. New clients will receive the existing PlayerStates when they join, so as long as the widget updates when it’s created (or when TeamID changes), everyone should see the correct colours.
Let me know how you go!

Hello @McMillJon Thank you for the reply.
It’s still a bit foggy for me, I do have my teamID replicated but I dont understand how to get the references to compare first - whether healthbar belongs to me, and second - whether this other character is my ally or enemy.
Also from what I understood, you imply this logic should be in the player state, and it should just feed the color results to the widget?
Could you give me a bit more detail about your approach?

Thanks again

For those who come after

On character spawn cast to health bar and set reference to that character.

Then in the widget compare reference with owning player pawn.
To know if ally or enemy cast to player state and compare teamID
And to work retroactively on players that spawned before you, get all actors of class and rerun the logic on all of them.

Works for me