Download

Trouble with timers over the network

EDIT: I seemed to have gotten this to work, tho I’m not exactly sure what it was. I removed the client RPC, enabled Ticking, and only subtracted the delta from RemainingTime if it’s on the server.

In my game, I have a cool down timer for abilities. I want to display the remaining time on the timer on the client.

This is my blueprint for the UI text

&stc=1&d=1415232646

Initially, I just started the timer and tried to retrieve it from the blueprint however the time always returned -1, which means the timer wasn’t running (I know the timer is in fact running because it’s functional in the ability’s blueprint). From that point I assumed that the timer was only running on the server, thus couldn’t be retrieved by the client for display on the HUD. I added a client RPC method to try and start a timer on the client, however that doesn’t seem to be working. After that I tried having a variable that kept ticking down the RemainingTime variable, and set up that variable for replication…however those values are not replicated and always return the default values.

And my code:




// Header
    UFUNCTION(BlueprintCallable, Category = "Ability")
    float GetCoolDownRemainingTime();

    UPROPERTY(EditAnywhere, Category = "Ability")
    float CoolDownTime;

    UPROPERTY(BlueprintReadOnly, Replicated, Category = "Ability")
    float RemainingTime;

    UFUNCTION(Client, reliable)
    void ClientStartTimer();

    UPROPERTY(BlueprintReadOnly, Replicated, Category = "Ability")
    uint32 CoolDownActive : 1;

// Implementation

void AAbility::Tick(float Delta)
{
    Super::Tick(Delta);

    OnActionTick(Delta);

    if (CoolDownActive) 
    {
        RemainingTime -= Delta;
    }
}

void AAbility::StartCoolDown()
{
    CoolDownActive = true;
    RemainingTime = CoolDownTime;
    GetWorldTimerManager().SetTimer(this, &AAbility::CoolDownFinished, CoolDownTime, false);

    if (Role == ROLE_Authority)
    {
        ClientStartTimer();
    }
    
}

void AAbility::CoolDownFinished()
{
    CoolDownActive = false;
    OnCoolDownFinished();
}

float AAbility::GetCoolDownRemainingTime()
{
    return GetWorldTimerManager().GetTimerRemaining(this, &AAbility::CoolDownFinished);
}

void AAbility::ClientStartTimer_Implementation()
{
    CoolDownActive = true;
    RemainingTime = CoolDownTime;
    GetWorldTimerManager().SetTimer(this, &AAbility::CoolDownFinished, CoolDownTime, false);
}

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

    DOREPLIFETIME(AAbility, CoolDownActive);
    DOREPLIFETIME(AAbility, RemainingTime);

}