Aego Русский

aego/time

Delta and Elapsed count game time, which follows the time scale and stops while paused; Unscaled and UnscaledElapsed count real time. SetPaused and SetScale change the clock of a running game; the documentation sandbox has no world clock, so calling them there stops the script, and the example only reads the clock. See Time.

On this page

Example

package demo

import (
    "aego/log"
    "aego/math"
    "aego/time"
)

script Bob {
    @export var Amplitude float = 0.5

    func Update(dt float) {
        y := math.Sin(time.Elapsed() * 3) * Amplitude
        node.SetPosition(Vec2{0, y})
        if time.Frame() % 30 == 0 {
            log.Info("frame " + string(time.Frame()) + ", elapsed " + string(time.Elapsed()) + " s, delta " + string(time.Delta()))
        }
        if time.Frame() == 60 {
            log.Info("scale " + string(time.Scale()) + ", paused " + string(time.IsPaused()) + ", fixed step " + string(time.Fixed()))
            log.Info("real time since start " + string(time.UnscaledElapsed()) + " s")
        }
    }
}

Functions

Delta() floatReturns the game seconds since the previous frame: real time × Scale, 0 while paused. Inside FixedUpdate it is the fixed step.
Elapsed() floatReturns the game seconds since Play started. It stops while paused and follows Scale.
Fixed() floatReturns the fixed step length in seconds, 1/60 by default. Scale does not change it; it changes how many steps run.
Frame() intReturns the number of frames this world has run, paused frames included.
IsPaused() boolReports whether game time is paused.
Scale() floatReturns the time scale: 1 is normal speed, 0.5 is half speed, 0 freezes game time without pausing.
SetPaused(paused bool)Pauses or resumes game time. While paused, Update, FixedUpdate and game-time coroutines of ordinary scripts do not run.
SetScale(scale float)Sets the time scale from 0 to 8. The fixed step stays the same, so the simulation stays stable; only the number of steps changes.
Tick() intReturns the number of the current or last fixed step. It is the same number the profiler and the Event Monitor show.
Unscaled() floatReturns the real seconds since the previous frame, ignoring Scale and pause.
UnscaledElapsed() floatReturns the real seconds since Play started, pauses included.

Delta function

func Delta() float

Returns the game seconds since the previous frame: real time × Scale, 0 while paused. Inside FixedUpdate it is the fixed step.

Example

x += Speed * time.Delta()

Elapsed function

func Elapsed() float

Returns the game seconds since Play started. It stops while paused and follows Scale.

Example

y := math.Sin(time.Elapsed() * 3)

Fixed function

func Fixed() float

Returns the fixed step length in seconds, 1/60 by default. Scale does not change it; it changes how many steps run.

Example

steps := int(1 / time.Fixed())

Frame function

func Frame() int

Returns the number of frames this world has run, paused frames included.

Example

if time.Frame() % 60 == 0 {
	log.Info("tick")
}

IsPaused function

func IsPaused() bool

Reports whether game time is paused.

Example

if time.IsPaused() {

Scale function

func Scale() float

Returns the time scale: 1 is normal speed, 0.5 is half speed, 0 freezes game time without pausing.

Example

if time.Scale() < 1 {

SetPaused function

func SetPaused(paused bool)

Pauses or resumes game time. While paused, Update, FixedUpdate and game-time coroutines of ordinary scripts do not run.

Example

time.SetPaused(true)

SetScale function

func SetScale(scale float)

Sets the time scale from 0 to 8. The fixed step stays the same, so the simulation stays stable; only the number of steps changes.

Example

time.SetScale(0.25)

Tick function

func Tick() int

Returns the number of the current or last fixed step. It is the same number the profiler and the Event Monitor show.

Example

log.Info("landed on tick " + string(time.Tick()))

Unscaled function

func Unscaled() float

Returns the real seconds since the previous frame, ignoring Scale and pause.

Example

menuFade += time.Unscaled()

UnscaledElapsed function

func UnscaledElapsed() float

Returns the real seconds since Play started, pauses included.

Example

blink := math.Sin(time.UnscaledElapsed() * 6)