Finally joined the forums because I don’t know what to do about this one
I’m currently working on making a multiplayer game (still fairly new to multiplayer, so this might be something simple) and I’m struggling with updating the HUD widget. Currently, I have two sets of info I need to send to the HUD when updated: stamina and health. Both of them have their own events to send info to the HUD BP from the player controller.
On both the server and client, updating the stamina works with no problem. However, when the client tries to update its health, the reference to the HUD is invalid.
I can’t figure out why it seems like the reference to the HUD is both valid and invalid at the same time. When sprinting and taking damage, the stamina continues to update correctly but the health still considers HUD invalid. HUD isn’t accessed anywhere outside of this screenshot, so I have no clue what’s causing this.
Hopefully someone can provide an answer or work around. There might be a better way to handle this entirely that circumvents the problem, so please give any suggestions you have. Thanks!
An important note: Servers will only create widgets for a local player controller, the one that is playing directly on the server. Pure/dedicated servers do not have any user interface at all. They can’t create it and they don’t have a viewport to add it on.
The probable cause (IMO) is that you are calling UpdateHUD_Health from the server on all pawns including non-locally controlled ones. These however don’t have HUDs. If my guess is correct, if you add the same check on the stamina you will get the same error, still both will work on some occasions.
The proper solution, in my experience, is to reverse the dependency entirely. Nothing outside the controller should even know about the existence of a HUD - nothing should ever call update on it or interact with it in any way. Each widget however, should have a reference to the object it displays information about and should go and directly get the data from that object.
In your case that would mean that WBP_HUD should have a BP_PlayerCharacter parameter exposed on spawn and provided to it on creation. From that point on you either:
“bind” the values from the widget to the pawn variables - Easiest to do, support and iterate on but a bit heavy on large data chunks.
Use tick to update the widget values from the character - HP and Stamina are critical and easy to get/calculate so the room for optimization is minimal.
Bind to an Event Dispatcher/Delegate “OnHit” and/or “OnAttack” to update the proper values. More complex approach. Perfect for rare events but HP and Stamina changes are not. Needs more “boiler plate” - you need to bind, unbind and call it which might be on a lot of places.