Download

UI - Replicating value for widget

The base Character has an integer value called Fuel which is replicated, because I want everyone to know about it.

.h


    UPROPERTY(ReplicatedUsing=OnRep_Fuel)
    int Fuel;

.cpp


void ABaseCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
    Super::GetLifetimeReplicatedProps(OutLifetimeProps);

    DOREPLIFETIME(ABaseCharacter, Fuel) 
}

Fuel can be modified by the following function in the cpp:


void ABaseCharacter::ModifyPlayerFuel(int DeltaFuel)
{
    Fuel = Fuel + DeltaFuel;

    OnRep_Fuel();
}

void ABaseCharacter::OnRep_Fuel()
{
    OnFuelChanged(Fuel);
}

.h


void ModifyPlayerFuel(int DeltaFuel);

UFUNCTION()
void OnRep_Fuel();

UFUNCTION(BlueprintImplementableEvent, Category="Fuel")
void OnFuelChanged(int Fuel);

So far so good. This (seems to) replicate the Fuel value just fine.

But I want to have the value floating above the players’ heads. I’ve hooked this up, and you can see the Blueprints for BP_BaseCharacter:

But the values only change for the player whose screen it is. You can see below (2P w/ dedicated server) that the Fuel value for Client 1 is 1 and for Client 2 is -4, however each only sees their own Fuel value updating in the widget.

Where have I gone wrong??

Update: I fixed it. Since it wasn’t making it to the server (replication makes no sense to me) I had to create a server function to force it.

.h


UFUNCTION(Server, Reliable, WithValidation)
void ServerModifyPlayerFuel(int DeltaFuel);

.cpp


void ABaseCharacter::ModifyPlayerFuel(int DeltaFuel)
{
    if (GetLocalRole() < ROLE_Authority)
    {
        ServerModifyPlayerFuel(DeltaFuel); // You are not the authority. Ask the server to change your fuel
    }

    if (GetLocalRole() == ROLE_Authority)
    {
        Fuel = Fuel + DeltaFuel;

        OnFuelChanged(Fuel);
    }
}

void ABaseCharacter::ServerModifyPlayerFuel_Implementation(int DeltaFuel)
{
    ModifyPlayerFuel(DeltaFuel);
}