SyncTimer
SyncTimer provides an efficient way to synchronize a timer between server and clients.
Unlike SyncVars, a SyncTimer only updates state changes such as start or stopping, rather than each individual delta change.
SyncTimer is a Custom SyncType, and is declared like any other SyncType.
private readonly SyncTimer _timeRemaining = new SyncTimer();Making changes to the timer is very simple, and like other SyncTypes must be done on the server.
// All of the actions below are automatically synchronized.
/* Starts the timer with 5 seconds on it.
*
* The optional boolean argument will also send
* a stop event before starting a new timer, only if
* the previous timer is still running.
*
* EG: if a timer was started with 5 seconds and
* you start a new timer with 2 seconds remaining
* a StopTimer will be sent with the remaining time
* of 2 seconds before the timer is started again at
* 5 seconds. */
_timeRemaining.StartTimer(5f, true);
/* Pauses the timer and optionally sends the current
* timer value as it is on the server. */
_timeRemaining.PauseTimer(false);
// Unpauses the current timer.
_timeRemaining.UnpauseTimer();
/* Stops the timer early and optionally sends the
* current timer value as it is on the server. */
_timeRemaining.StopTimer(false);Updating and reading the timer value is much like you would a normal float value.
Like other SyncTypes, you can subscribe to change events for the SyncTimer.
Last updated