Module game

Game related variables and functions.

Functions

getPlayer (index) Gets a player object by their index.
getAllPlayers () Gets a list of all players existing in the game.
newTimer (interval, maxCount, listener) Returns a game timer which calls listener every interval milliseconds up to maxCount times.
destroyAllTimers () Destroys all game timers created by newTimer.
requestUserData (callback) Requests the player's saved userdata for the level from the server.
saveUserData (userdata, callback) Requests to save the player's userdata for the level onto the server.

Tables

settings A list the player's settings and client information.

Fields

level The current level the game is played on.
playersCount The amount of players in the game.
playerCount [Alias of playersCount] The amount of players in the game.
elapsedMS How many milliseconds have passed since Lua started running.
init The event handler for game init.
start The event handler for game start.
tick The event handler for game tick.
gameEvent The handler for gameEvents sent via player.postGameEvent.
playerRemoved The event handler for player removing, whether by finishing or forfeiting the match.


Functions

getPlayer (index)
Gets a player object by their index.

Remote player objects' variables are read-only. Exceptions are metadata and visual effects, which can be both set and get. Note: remote metadata and visual effects are still local only.

Parameters:

  • index int Index of the player.

Returns:

    Returns the local/remote player object.

See also:

Usage:

    game.getPlayer(1)
getAllPlayers ()
Gets a list of all players existing in the game.

Remote player objects' variables are read-only. Exceptions are metadata and visual effects, which can be both set and get. Note: remote metadata and visual effects are still local only.

Returns:

    An AS3 array of all players in the game.

See also:

Usage:

    local players = totable(game.getAllPlayers())
    for i, v in pairs(players) do
        v.setmetadata("index", i)
    end
newTimer (interval, maxCount, listener)
Returns a game timer which calls listener every interval milliseconds up to maxCount times.

Unlike player.newTimer and other tick handlers, these timers can run before the game has finished initializing and after the player is dead.

The timer cannot be triggered on the same tick it is created, nor can it be triggered mid-tick by changing its properties.

Parameters:

  • interval number How many milliseconds must pass to complete an interval.
  • maxCount int How many intervals will be completed. Set to -1 for infinite intervals.
  • listener function The listener to be called every time an iteration is completed.

Returns:

    timer The created timer object.

See also:

Usage:

    alienSpawnTimer = game.newTimer(1000 * 10, 9999999, function()
        -- Spawns a new alien every 10 seconds
        game.level.newAlien(1, os.time(), toobject{})
    end)
destroyAllTimers ()
Destroys all game timers created by newTimer.

See also:

requestUserData (callback)
Requests the player's saved userdata for the level from the server. Provides the userdata or an error message.

This is an asynchronous method; it will not pause the script while the request is processing. Instead, it will later run the given callback function once the request has completed or failed.

tolua method must be used for every variable you get from the userdata, as well as for the error message.

Parameters:

  • callback function The function that will be called once the request has completed or failed.

See also:

Usage:

    game.requestUserData(function(data, error)
        if tolua(error) ~= nil then
            player.chat("Error loading user data: " .. tostring(tolua(error)), 0xFF0000)
        else
            player.speed = tolua(data.speed)
            player.accel = tolua(data.accel)
            player.jump = tolua(data.jump)
        end
    end)
saveUserData (userdata, callback)
Requests to save the player's userdata for the level onto the server.

This is an asynchronous method; it will not pause the script while the request is processing. Instead, it will later run the given callback function once the request has completed or failed.

toobject method must be used for the userdata if it is a table. tolua method must be used for the error message.

Parameters:

  • userdata The userdata for the level that will be saved to the server.
  • callback function The function that will be called once the request has completed or failed.

See also:

Usage:

    local userdata = {
        speed = tolua(player.speed),
        accel = tolua(player.accel),
        jump = tolua(player.jump)
    }
    
    game.saveUserData(toobject(userdata), function(error)
        if tolua(error) ~= nil then
            player.chat("Error saving user data: " .. tostring(tolua(error)), 0xFF0000)
        end
    end)

Tables

settings
A list the player's settings and client information.

Fields:

  • isMobile Is the client running on iOS or Android? Read-only.
  • isGuest Is the player's sound muted? Read-only.
  • isMuted Is the player's sound muted? Read-only.
  • drawBackgrounds Does the player have art backgrounds / art lua enabled? Read-only.
  • soundVolume The player's sound volume, expressed as a value ranging from 0 - 100. Read-only.
  • musicVolume The player's sound volume, expressed as a value ranging from 0 - 100. Read-only.

Fields

level
The current level the game is played on. Read-only.
playersCount
The amount of players in the game. Read-only.

See also:

playerCount
[Alias of playersCount] The amount of players in the game. Read-only.

See also:

elapsedMS
How many milliseconds have passed since Lua started running. Read-only.
init
The event handler for game init. A drawing lock may be obtained from this event.

The drawing lock will prevent drawing completion until all existing drawing locks are disposed of.

See also:

Usage:

    local lock = nil
    game.init.addListener(function(event)
        lock = event.acquireLock()
    
        game.requestUserData(function(data, error)
            data = tolua(data)
            error = tolua(error)
            -- Do something here
            lock.dispose()
        end)
    end)
start
The event handler for game start.

See also:

tick
The event handler for game tick.

See also:

gameEvent
The handler for gameEvents sent via player.postGameEvent.

Provides a gameEvent, from which you can get data and source (the player-sender).

tolua method must be used for every variable you get from the gameEvent.

Usage:

    game.gameEvent.addListener(function(event)
        local data = tolua(event.data)
        local sender = tolua(event.source)
        -- Do something here
    end)
playerRemoved
The event handler for player removing, whether by finishing or forfeiting the match. Provides the object of the removed player.

Usage:

    game.playerRemoved.addListener(function(removedPlayer)
        -- Do something here
    end)
generated by LDoc 1.5.0 Last updated 2023-11-25 17:53:31