Predicted Modules

Predicted Timer Module

The TimerModule is a built-in module for handling networked countdowns. It manages the synchronization of time between server and client, ensuring that timers roll back correctly during prediction corrections.

Basic Usage

Instantiate the module within your PredictedIdentity. By default, the timer automatically ticks down by delta every simulation tick.

public class Bomb : PredictedIdentity
{
    private TimerModule _fuseTimer;

    protected override void LateAwake()
    {
        // Create the timer (default: automatic countdown)
        _fuseTimer = new TimerModule(this);
        
        // Hook into events
        _fuseTimer.onTimerEnded += Explode;
        _fuseTimer.onPredictedTimerUpdated_View += UpdateTimerUI;
    }

    public void Activate()
    {
        // Only the server (or owner, if allowed) needs to start it
        if (isServer)
            _fuseTimer.StartTimer(5.0f);
    }

    private void Explode() { /* Boom */ }

    private void UpdateTimerUI(float timeRemaining)
    {
        // Use the event value for smooth UI updates
        _uiText.text = timeRemaining.ToString("F1");
    }
}

Manual Ticking

You can disable automatic counting to control the timer manually (e.g., for a charging weapon that only advances while a button is held).

protected override void LateAwake()
{
    // Enable manual tick mode
    _chargeTimer = new TimerModule(this, manualTick: true);
}

protected override void Simulate(ref MyState state, float delta)
{
    if (state.isCharging)
    {
        // Manually advance the timer
        // Use -delta to count down, or +delta to count up
        _chargeTimer.TickTimer(-delta); 
    }
}

API References

MemberDescription
Properties
remainingThe current authoritative time remaining.
isTimerRunningReturns true if the timer is currently active (value is not null).
predictedViewTimerThe smoothed time value for visual use (Update/LateUpdate).
Methods
StartTimer(float time)Sets the timer to the specified value and starts it.
StopTimer(bool silent)Stops the timer. If silent is true, onTimerEnded is not invoked.
TickTimer(float amount)Manually adjusts the timer by the given amount.
Events
onTimerEndedInvoked when the timer reaches 0.
onPredictedTimerUpdated_ViewInvoked when the visual time changes. Use this for UI.
onVerifiedTimerUpdated_ViewInvoked when the authoritative state is updated.