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:
Reference
__init
__setTimes
Parameters
times
__tick
Parameters
dt
getPerformanceNow
Returns a high precision timestamp in milliseconds
Returns
number: in milliseconds
getTime
Returns the amount of milliseconds passed since start of playback
Returns
number: time in milliseconds
getDeltaTime
Returns the current player delta time between frames in milliseconds
Returns
number: delta time in milliseconds
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
Returns the current game delta time between frames in milliseconds
Returns
number: delta time in milliseconds
sleep
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
Interpolates between startValue and endValue for duration seconds and will call the callback function
Parameters
startValue(number): the start value to interpolate fromendValue(number): the end value to interpolate toduration(number): the amount of seconds to take to interpolatecallback(function): the function to call. the only parameter this gets is the interpolated valuedoneCallback(function): the function to call when interpolation is done. this param is optional
getUserAgentString
Returns the full user agent string of the browser.
Returns
string: full user agent string.
Example
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
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
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
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
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