Network Variable Replication Basics and showing Changes

Hi guys,
I have some basic question about networking and replication.
Right now, when I change the player’s health value, I do it only on the server and the server replicates the updated value to the client. Now in an OnRepNotify_Function(), I update the player’s HUD to show the updated value.
This works, but it’s kinda slow.
What I mean by that, is when using a healing item, the clients healthbar takes a little while to get updated. Sometimes it’s “instant”, sometimes it feels like it takes up to half a second. Here’s some pseudo code of what I mean by that:

void ServerAddHealth(float Value)
{
    // Only called on the Server
    ServerAddHealth(Value);
    OnRep_Health();
}

void OnRep_Health()
{
    // This takes a while, causing it to look very laggy.
    UpdateHUD();
}

What I want to know: Is this the right way of doing this?

I’ve seen a different approach. Player uses healing item, change local value of health which leads to an instantly updated HUD. Now do the same on the Server. The replication of the actual value may take some time, but at least the Client sees it updated instantly. Would this be better? Here’s some pseudo code of what I mean:

void AddHealth(float Value)
{
    PlayerHealth += Value;

    if(Role < ROLE_Authority)
    {
        ServerAddHealth(Value);
    }

    UpdateHUD();
}

Is the latter method safe?
It’s been some time since I’ve used it, but it should look smoother / more responsive IIRC, right?
I’ve also heard about interpolation between updates, is this what I’m looking for? If it is, does anyone have code examples for this?
Also: Is it normal that I experience that much of a lag when using the first method?
Looking forward to your thoughts!

Update:

The “Lag” I was experiencing was due to “MinNetUpdateFrequency” being set to 2, which means when a Value is being replicated unsteadily, it’s considered exactly 2 times per second for replication. Boosted that value up and it works flawlessly now. Does anyone have some good resources about bandwidth usage and NetUpdate frequency?

1 Like