Infohazard.Core 1.4.1
Infohazard Core Utility Library
Loading...
Searching...
No Matches
Infohazard.Core.PassiveTimer Struct Reference

A lightweight timer that does not need to be updated each frame. More...

Public Types

enum  TimeMode
 The various modes available for timers. More...
 

Public Member Functions

 PassiveTimer (float interval, TimeMode mode=TimeMode.Scaled, bool initialize=true)
 Construct a PassiveTimer with the given interval, which will be both the initial and repeat interval. More...
 
 PassiveTimer (float initialInterval, float interval, TimeMode mode=TimeMode.Scaled, bool initialize=true)
 Construct a PassiveTimer with the given interval. More...
 
void Initialize ()
 Initialize the timer. More...
 
bool TryConsume ()
 If the current interval has ended, reset the interval and return true, else return false without reset. More...
 
void StartInterval ()
 Restart the interval, so that the timer starts counting down from its repeat interval. More...
 
void EndInterval ()
 End the interval, so the timer is in the expired state. More...
 

Properties

float InitialInterval [get, set]
 Initial interval to set the timer for in seconds. More...
 
float Interval [get, set]
 The repeat interval for the in seconds. More...
 
TimeMode Mode [get, set]
 What value for time that the timer uses (scaled, unscaled, or realtime). More...
 
bool IsIntervalEnded [get]
 Whether the timer is in the expired state, meaning the current interval has elapsed. More...
 
float IntervalStartTime [get, set]
 The start time for the current interval. More...
 
float IntervalEndTime [get]
 The time at which the current interval will end (or has ended). More...
 
float TimeSinceIntervalEnded [get]
 Time that has passed since the current interval ended (or, if not ended, a negative value). More...
 
bool IsInitialized [get, private set]
 Whether the timer is initialized. More...
 
bool HasIntervalStarted [get, private set]
 Whether an interval has started yet. More...
 
float TimeUntilIntervalEnd [get]
 The time in seconds until the current interval ends. More...
 
float RatioUntilIntervalEnd [get]
 A ratio going from one at interval start to zero at interval end. More...
 
float TimeSinceIntervalStart [get]
 The time in seconds since the current interval started. More...
 
float RatioSinceIntervalStart [get]
 A ratio going from zero at interval start to one at interval end. More...
 
bool DidIntervalEndThisFrame [get]
 Whether the current interval ended during the current frame. More...
 
float CurrentTimeWithoutPause [get]
 The current time read from Unity, based on the Mode. More...
 
float CurrentTime [get]
 The current time read from Unity, taking into account time that the PassiveTimer has spent paused. More...
 
float DeltaTime [get]
 The delta time read from Unity, based on the Mode. More...
 
float PausedTime [get, private set]
 The time that the PassiveTimer has spent in a paused state. More...
 
bool IsPaused [get, set]
 Get or set whether the PassiveTimer is paused. More...
 

Detailed Description

A lightweight timer that does not need to be updated each frame.

Can be serialized directly in the inspector or created in code. If it is assigned in the inspector, you must call Initialize in Start/Awake/OnEnable/OnSpawned.

A PassiveTimer can be in one of four states:

  • It has not yet been initialized (uninitialized state).
  • An interval is active and counting down (counting state).
  • The timer is paused (paused state).
  • An interval has expired (expired state).

Member Enumeration Documentation

◆ TimeMode

The various modes available for timers.

Constructor & Destructor Documentation

◆ PassiveTimer() [1/2]

Infohazard.Core.PassiveTimer.PassiveTimer ( float  interval,
TimeMode  mode = TimeMode.Scaled,
bool  initialize = true 
)

Construct a PassiveTimer with the given interval, which will be both the initial and repeat interval.

Parameters
intervalThe initial and repeat interval.
modeTime mode to use.
initializeWhether to initialize the timer and start counting immediately.

◆ PassiveTimer() [2/2]

Infohazard.Core.PassiveTimer.PassiveTimer ( float  initialInterval,
float  interval,
TimeMode  mode = TimeMode.Scaled,
bool  initialize = true 
)

Construct a PassiveTimer with the given interval.

Parameters
initialIntervalThe initial interval.
intervalThe repeat interval.
modeTime mode to use.
initializeWhether to initialize the timer and start counting immediately.

Member Function Documentation

◆ EndInterval()

void Infohazard.Core.PassiveTimer.EndInterval ( )

End the interval, so the timer is in the expired state.

◆ Initialize()

void Infohazard.Core.PassiveTimer.Initialize ( )

Initialize the timer.

You must call this when your script initializes if the timer was assigned in the inspector.

◆ StartInterval()

void Infohazard.Core.PassiveTimer.StartInterval ( )

Restart the interval, so that the timer starts counting down from its repeat interval.

◆ TryConsume()

bool Infohazard.Core.PassiveTimer.TryConsume ( )

If the current interval has ended, reset the interval and return true, else return false without reset.

This is useful to create ability cooldowns or weapon fire rates. See the following example:

if (AbilityButtonPressed() && AbilityTimer.TryConsume()) {
UseAbility();
}
Returns
Whether the interval was ended and has been reset.

Property Documentation

◆ CurrentTime

float Infohazard.Core.PassiveTimer.CurrentTime
get

The current time read from Unity, taking into account time that the PassiveTimer has spent paused.

◆ CurrentTimeWithoutPause

float Infohazard.Core.PassiveTimer.CurrentTimeWithoutPause
get

The current time read from Unity, based on the Mode.

Exceptions
ArgumentOutOfRangeExceptionIf Mode is invalid.

◆ DeltaTime

float Infohazard.Core.PassiveTimer.DeltaTime
get

The delta time read from Unity, based on the Mode.

Exceptions
ArgumentOutOfRangeExceptionIf Mode is invalid or realtime.

◆ DidIntervalEndThisFrame

bool Infohazard.Core.PassiveTimer.DidIntervalEndThisFrame
get

Whether the current interval ended during the current frame.

This can be used to create actions that happen only once, the moment a timer expires.

◆ HasIntervalStarted

bool Infohazard.Core.PassiveTimer.HasIntervalStarted
getprivate set

Whether an interval has started yet.

◆ InitialInterval

float Infohazard.Core.PassiveTimer.InitialInterval
getset

Initial interval to set the timer for in seconds.

This interval begins when Initialize is called, or when the timer is created from a non-default constructor.

◆ Interval

float Infohazard.Core.PassiveTimer.Interval
getset

The repeat interval for the in seconds.

This interval begins when StartInterval or TryConsume is used.

◆ IntervalEndTime

float Infohazard.Core.PassiveTimer.IntervalEndTime
get

The time at which the current interval will end (or has ended).

◆ IntervalStartTime

float Infohazard.Core.PassiveTimer.IntervalStartTime
getset

The start time for the current interval.

◆ IsInitialized

bool Infohazard.Core.PassiveTimer.IsInitialized
getprivate set

Whether the timer is initialized.

◆ IsIntervalEnded

bool Infohazard.Core.PassiveTimer.IsIntervalEnded
get

Whether the timer is in the expired state, meaning the current interval has elapsed.

◆ IsPaused

bool Infohazard.Core.PassiveTimer.IsPaused
getset

Get or set whether the PassiveTimer is paused.

It is not necessary to pause timers to account for the game pausing, as long as they are using realtime. This allows an individual timer to be paused separately from the rest of the game.

◆ Mode

TimeMode Infohazard.Core.PassiveTimer.Mode
getset

What value for time that the timer uses (scaled, unscaled, or realtime).

◆ PausedTime

float Infohazard.Core.PassiveTimer.PausedTime
getprivate set

The time that the PassiveTimer has spent in a paused state.

◆ RatioSinceIntervalStart

float Infohazard.Core.PassiveTimer.RatioSinceIntervalStart
get

A ratio going from zero at interval start to one at interval end.

◆ RatioUntilIntervalEnd

float Infohazard.Core.PassiveTimer.RatioUntilIntervalEnd
get

A ratio going from one at interval start to zero at interval end.

◆ TimeSinceIntervalEnded

float Infohazard.Core.PassiveTimer.TimeSinceIntervalEnded
get

Time that has passed since the current interval ended (or, if not ended, a negative value).

◆ TimeSinceIntervalStart

float Infohazard.Core.PassiveTimer.TimeSinceIntervalStart
get

The time in seconds since the current interval started.

◆ TimeUntilIntervalEnd

float Infohazard.Core.PassiveTimer.TimeUntilIntervalEnd
get

The time in seconds until the current interval ends.


The documentation for this struct was generated from the following file: