Download

Health value is not updating on Client side

Hello,
So I have a widget BP with a health bar that doesn’t update for client. The addToViewPort call is made in the Controller Constructor and the HUD is showed for all clients.



	if (IsLocalPlayerController() && HUDWidgetClass != nullptr)
	{
		MainWidget = CreateWidget<UUserWidget>(GetWorld(), HUDWidgetClass);
		if (MainWidget != nullptr) 
			MainWidget->AddToViewport();
	}


I know that the problem is because of the function “get player index by 0” but I’ve tried to replace it with almost every get functions in order to get the current character or controller(get owning player/controller,etc) but it didn’t work.:frowning:
Also if i change the index with 1 or 2, the server doesn’t work any longer and also doesn’t fix the problem for clients(i’ve expected that if i set index to 1 the client 1 to work,2 for the second client,etc).

Player index is for local multiplayer, I’m fairly sure.

The most likely thing that isn’t happening is that health is not replicated properly. Try to only change the value of Health on the server, and make sure Health is replicated.

Try having repNotify function for your health value. When that changes, update your widget?

Through my experience with replication and widgets, it’s a pain to reflect some values changed by server on a client’s widget. repNotify usually sorted things out.

https://m.youtube.com/watch?v=htdI2dh-aek Suggest giving this a watch how the handle of updating the information is done.

First of all thanks for so many responses,it means a lot to me.



I will really really try to avoid to call notify for each changed value in the HUD,because i have more to count health,stamina,ammo type,ammo left etc(and im planning to add more that will change the layout of the HUD,ex for one type of weapon add another bar). Also my character class is in c++ and i don’t know how to do it in blueprints. Is there any way that I can get the current character from the HUD on client side? I’ve tried to made the Controller class to keep the instance of the PlayerCharacter but that didnt work also since the controller get index 0 also returns null.

You should store these values in my opinion in playerstate.

yiud have the variables for each of these and in the hid cast to owning player get playerstate cast to playerstate drag a pin out to get the variable.

I did a video with getting values from all playerstates to have a scoreboard… Some of the logic is built into it though it looks more confusing then it needs to be for your use.

https://youtu.be/HivxFBB87-Y

HUD is ClientSide. So if you want the PlayerCharacter of that Client, you just call “GetPlayerCharacter” with Index 0.
The Index itself doesn’t matter online, so you can’t use 1, 2 etc, for other clients. This is only if you have local Players (split screen etc).

But since you only need the Client who owns the Widget, the Index 0 should be fine.

Now you need to make sure to replicate the Current Health. In your Widget Screenshot, I see that you are correctly getting Current and Max HP
as well as dividing them to get the 0 to 1 ratio. So that’s good.

Now you need to set the Variable (CurrentHealth) to Replicate. And of course the PlayerCharacter needs to replicate too, but i guess that’s already happening.

So are you sure that your CurrentHealth Variable is correctly replicating?

In C++, you need to add the Replicated specifier to the UPROPERTY Header and also put the Variable into the “GetLifeTimeRepliactedProps” function.
In BP it’s enough to just edit the Variable and selected “replicated”.

If that’s still not working, then please show me your Code (C++/BP) and explain to me what exactly is happening (+ upcoming errors/warnings)

Yep , the Replicated value get the job done (in c++ code), what I’ve tried before was to create a local var and replicate it in HUD blueprint but that didn’t work. Thanks a lot !:o