Quantum 3 3.1.0

Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
Quantum.GameTimer Struct Reference

Built-in timer struct for game logic that counts FrameBase.GameTick instead of the frame number. Pausing or slowing down the game time (see FrameBase.GameTimeScale) pauses or slows down this timer. More...

Public Member Functions

readonly FP DurationNormalized (FrameBase frame)
 The progress of the timer between 0 (just started) and 1 (expired), e.g. to display a cooldown. More...
 
readonly FP ElapsedSeconds (FrameBase frame)
 How much game time since the timer has started in seconds. More...
 
readonly FP ElapsedTicks (FrameBase frame)
 How many game ticks since the timer was started. More...
 
readonly override int GetHashCode ()
 Overrides the hash code generation of this type. More...
 
readonly bool HasStoppedThisTick (FrameBase frame)
 Has the timer expired during this frame. More...
 
readonly bool IsRunning (FrameBase frame)
 Is the timer set and still running. More...
 
readonly FP OvershootSeconds (FrameBase frame)
 How much game time the game tick has passed TargetTick by in seconds. More...
 
readonly FP OvershootTicks (FrameBase frame)
 How many game ticks the game tick has passed TargetTick by, e.g. to place a spawn at the exact expiry point. More...
 
readonly FP RemainingSeconds (FrameBase frame)
 How much game time is remaining until the timer expires in seconds. More...
 
readonly FP RemainingTicks (FrameBase frame)
 How many game ticks are remaining until the timer expires. More...
 
void Restart (FrameBase frame, bool keepOvershoot=false)
 Restarts the timer with the same Duration. More...
 

Static Public Member Functions

static GameTimer FromSeconds (FrameBase frame, FP durationInSeconds)
 Create a new timer from a delay in seconds of game time. More...
 
static GameTimer FromTicks (FrameBase frame, FP durationInTicks)
 Create a new timer from a delay in game ticks. More...
 
static void Serialize (void *ptr, IDeterministicFrameSerializer serializer)
 Serializes a GameTimer into a FrameSerializer to write or read from a frame snapshot. More...
 

Public Attributes

readonly FP Duration => TargetTick - StartTick
 The duration of the timer in ticks, TargetTick - StartTick. More...
 
readonly bool IsSet => TargetTick >= StartTick && TargetTick.RawValue > 0
 If the timer is has been set. A valid timer has a TargetTick greater than 0 and the target tick TargetTick greater than or equal to the StartTick. More...
 
FP StartTick
 The game tick at which the timer has started. More...
 
FP TargetTick
 The game tick at which the timer will run out. More...
 

Static Public Attributes

static readonly GameTimer None = default
 Empty timer. More...
 
const int SIZE = FP.SIZE * 2
 The size of the component (or struct/type) in-memory inside the Frame data-buffers or stack (when passed as value parameter). Not related to the snapshot payload this occupies, which is bit-packed and compressed. More...
 

Detailed Description

Built-in timer struct for game logic that counts FrameBase.GameTick instead of the frame number. Pausing or slowing down the game time (see FrameBase.GameTimeScale) pauses or slows down this timer.

Member Function Documentation

◆ DurationNormalized()

readonly FP Quantum.GameTimer.DurationNormalized ( FrameBase  frame)
inline

The progress of the timer between 0 (just started) and 1 (expired), e.g. to display a cooldown.

Parameters
frameThe current Frame instance.
Returns
The elapsed ticks divided by the Duration, clamped to [0, 1]. 1 for a set timer with a duration of 0, 0 if the timer is not set.

◆ IsRunning()

readonly bool Quantum.GameTimer.IsRunning ( FrameBase  frame)

Is the timer set and still running.

Parameters
frameThe current Frame instance.
Returns
true if the timer is valid and the current game tick is smaller than TargetTick. false otherwise.

◆ HasStoppedThisTick()

readonly bool Quantum.GameTimer.HasStoppedThisTick ( FrameBase  frame)

Has the timer expired during this frame.

Parameters
frameThe current Frame instance.
Returns
true if the timer is valid and the game tick reached or passed TargetTick during this frame. false otherwise.

Useful for one time operations that happen only at timer expiry. Only true once per expiry, also while the game tick is paused or slowed down. Never true while paused: a timer with a duration of 0 started while paused stops in the first frame the game tick advances again.

◆ RemainingTicks()

readonly FP Quantum.GameTimer.RemainingTicks ( FrameBase  frame)
inline

How many game ticks are remaining until the timer expires.

Parameters
frameThe current Frame instance.
Returns
TargetTick - current game tick. Never returns a negative value.

◆ RemainingSeconds()

readonly FP Quantum.GameTimer.RemainingSeconds ( FrameBase  frame)
inline

How much game time is remaining until the timer expires in seconds.

Parameters
frameThe current Frame instance.
Returns
Divides the remaining ticks by the simulation rate (FrameBase.UpdateRate). Never returns a negative value.

To get the real seconds until expiry divide by FrameBase.GameTimeScale (if it is above 0), which is only exact if the time scale does not change until the timer expires. While paused the timer does not expire.

◆ ElapsedTicks()

readonly FP Quantum.GameTimer.ElapsedTicks ( FrameBase  frame)
inline

How many game ticks since the timer was started.

Parameters
frameThe current Frame instance.
Returns
Returns current game tick - the start tick. Never returns a negative value.

◆ ElapsedSeconds()

readonly FP Quantum.GameTimer.ElapsedSeconds ( FrameBase  frame)
inline

How much game time since the timer has started in seconds.

Parameters
frameThe current Frame instance.
Returns
Divides the elapsed ticks by the simulation rate (FrameBase.UpdateRate). Never returns a negative value.

To get the real seconds since the start divide by FrameBase.GameTimeScale (if it is above 0), which is only exact if the time scale has not changed since the timer started.

◆ OvershootTicks()

readonly FP Quantum.GameTimer.OvershootTicks ( FrameBase  frame)
inline

How many game ticks the game tick has passed TargetTick by, e.g. to place a spawn at the exact expiry point.

Parameters
frameThe current Frame instance.
Returns
Current game tick - TargetTick. 0 while the timer is running or not set.

◆ OvershootSeconds()

readonly FP Quantum.GameTimer.OvershootSeconds ( FrameBase  frame)
inline

How much game time the game tick has passed TargetTick by in seconds.

Parameters
frameThe current Frame instance.
Returns
Divides the overshoot ticks by the simulation rate (FrameBase.UpdateRate). 0 while the timer is running or not set.

◆ Restart()

void Quantum.GameTimer.Restart ( FrameBase  frame,
bool  keepOvershoot = false 
)
inline

Restarts the timer with the same Duration.

Parameters
frameThe current Frame instance.
keepOvershootIf true and the target is reached, the timer restarts at its previous TargetTick instead of the current game tick, keeping the ticks the game tick has already passed the target by. Use it for repeating timers (e.g. restarted when HasStoppedThisTick(FrameBase)) so the interval does not drift when the game tick does not land exactly on the target (e.g. while slowed down). A timer that is still running restarts at the current game tick in both modes. If the game tick passed more than one interval this frame (e.g. time scale above 1 and a short interval), the new target is already reached: HasStoppedThisTick(FrameBase) only reports it during this frame, so restart in a loop (while (timer.HasStoppedThisTick(f)) { timer.Restart(f, true); }) to handle every interval. Restarting only once leaves the target behind the game tick and the timer never stops again.

A timer with a duration of 0 (or the 1 raw unit nudge) restarts to stop in the next frame the game tick advances, in both modes. Restarting it to the current game tick would stop it again during this frame (an endless restart loop) or never again.

◆ FromSeconds()

static GameTimer Quantum.GameTimer.FromSeconds ( FrameBase  frame,
FP  durationInSeconds 
)
inlinestatic

Create a new timer from a delay in seconds of game time.

The seconds are converted with the simulation rate (FrameBase.UpdateRate) instead of FrameBase.DeltaTime, so whole seconds are exact whole ticks at any rate (e.g. 1 second at 60 Hz is exactly 60 ticks, 1/60 cannot be stored exactly by FP).

Parameters
frameThe current Frame instance.
durationInSecondsThe game time in seconds to wait before the timer expires.
Returns
A new GameTimer instance with the specified duration.

To expire after real seconds multiply them by FrameBase.GameTimeScale, which is only exact if the time scale does not change until the timer expires. While paused the timer does not expire.

◆ FromTicks()

static GameTimer Quantum.GameTimer.FromTicks ( FrameBase  frame,
FP  durationInTicks 
)
inlinestatic

Create a new timer from a delay in game ticks.

Parameters
frameThe current Frame instance.
durationInTicksThe game ticks to wait before the timer expires.
Returns
A new GameTimer instance with the specified duration.

A duration of 0 started while paused (or before the first frame) gets a duration of 1 raw unit instead, so HasStoppedThisTick(FrameBase) reports it in the first frame the game tick advances again. Without it the target would be the game tick the next advance starts from, which is outside the range a stop is reported for.

◆ Serialize()

static void Quantum.GameTimer.Serialize ( void *  ptr,
IDeterministicFrameSerializer  serializer 
)
inlinestatic

Serializes a GameTimer into a FrameSerializer to write or read from a frame snapshot.

Parameters
ptrThe pointer to the GameTimer.
serializerThe FrameSerializer instance into which the struct will be serialized.

◆ GetHashCode()

readonly override int Quantum.GameTimer.GetHashCode ( )
inline

Overrides the hash code generation of this type.

Returns
A hash code of the current state of this instance.

Member Data Documentation

◆ SIZE

const int Quantum.GameTimer.SIZE = FP.SIZE * 2
static

The size of the component (or struct/type) in-memory inside the Frame data-buffers or stack (when passed as value parameter). Not related to the snapshot payload this occupies, which is bit-packed and compressed.

◆ None

readonly GameTimer Quantum.GameTimer.None = default
static

Empty timer.

◆ TargetTick

FP Quantum.GameTimer.TargetTick

The game tick at which the timer will run out.

◆ StartTick

FP Quantum.GameTimer.StartTick

The game tick at which the timer has started.

◆ IsSet

readonly bool Quantum.GameTimer.IsSet => TargetTick >= StartTick && TargetTick.RawValue > 0

If the timer is has been set. A valid timer has a TargetTick greater than 0 and the target tick TargetTick greater than or equal to the StartTick.

◆ Duration

readonly FP Quantum.GameTimer.Duration => TargetTick - StartTick

The duration of the timer in ticks, TargetTick - StartTick.