Skip to content

System

The System` class provides essential utilities for managing time, performance metrics, browser information, and interpolation logic within a Lua environment, facilitating tasks such as accurately track gameplay duration, manage asynchronous operations, smoothly transition values over time, and adapt behaviors based on the device the game is running on.

Usage

To use this module, add the following require at the top of your script:

local System = require 'engine/system'

Reference

__init

static __init()

__setTimes

static __setTimes(times)

Parameters

  • times

__tick

static __tick(dt)

Parameters

  • dt

getPerformanceNow

static getPerformanceNow()

Returns a high precision timestamp in milliseconds

Returns

  • number: in milliseconds

getTime

static getTime()

Returns the amount of milliseconds passed since start of playback

Returns

  • number: time in milliseconds

getDeltaTime

static getDeltaTime()

Returns the current player delta time between frames in milliseconds

Returns

  • number: delta time in milliseconds

getGameTime

static getGameTime()

Returns the current game time. This will usually be identical to getTime() except if there were pauses in the game, then the game time will be a smaller number

Returns

  • number: delta time in milliseconds

getDeltaGameTime

static getDeltaGameTime()

Returns the current game delta time between frames in milliseconds

Returns

  • number: delta time in milliseconds

sleep

static sleep(ms)

Waits for a given time in milliseconds

Parameters

  • ms (number): time in milliseconds

Returns

  • Promise: a promise that will fulfill after the time has passed

interpolate

static interpolate(startValue, endValue, duration, callback, doneCallback)

Interpolates between startValue and endValue for duration seconds and will call the callback function

Parameters

  • startValue (number): the start value to interpolate from
  • endValue (number): the end value to interpolate to
  • duration (number): the amount of seconds to take to interpolate
  • callback (function): the function to call. the only parameter this gets is the interpolated value
  • doneCallback (function): the function to call when interpolation is done. this param is optional

getUserAgentString

static getUserAgentString()

Returns the full user agent string of the browser.

Returns

  • string: full user agent string.

Example

local uaString = System.getUserAgentString()
print("User Agent:", uaString)

getUserAgentName

static getUserAgentName()

Returns the name of the browser being used.

Possible return values: - "Firefox" - "Chrome" - "Safari" - "Edge" - "Opera" - "Internet Explorer" - "Unknown"

Returns

  • string: name of the browser.

Example

local browser = System.getUserAgentName()
print("Browser Name:", browser)

isMobileDevice

static isMobileDevice()

Determines if the current device is a mobile device.

Returns

  • boolean: if the device is a mobile phone or tablet, otherwise false.

Example

if System.isMobileDevice() then
    print("Running on a mobile device")
else
    print("Running on a desktop device")
end

isTouchDevice

static isTouchDevice()

Determines if the current device supports touch input.

This is useful for games that need to handle touch events differently from mouse events.

Returns

  • boolean: if the device supports touch input, otherwise false.

Example

if System.isTouchDevice() then
    print("Touch input available")
end

getDeviceType

static getDeviceType()

Returns the type of device the user is running on.

Possible return values: - "Mobile" (Phones, tablets, etc.) - "Desktop" (Laptops, PCs)

Returns

  • string: or "Desktop", based on device detection.

Example

print("Device Type:", System.getDeviceType())

Examples

local System = require 'engine/system'
local Console = require 'engine/console'

local time = System.getTime()
Console.log(time)
local System = require 'engine/system'
local Console = require 'engine/console'

Console.log("Going to sleep for a second...")
System.sleep(1000):next(function()
    Console.log("Hello again!")
end)
local System = require 'engine/system'
local Console = require 'engine/console'

function init()
    Console.log("Going to sleep for a second...")

    -- Mark this promise as blocking, so init() is not returned from unless this promise is also fulfilled.
    -- This can be either done through System.markPromiseAsBlocking() or on the promise itself with :markAsBlocking()
    System.sleep(1000):next(function()
        Console.log("Hello again!")
    end):markAsBlocking()
end

function render()
    -- This will not execute until the sleep promise has fulfilled
end