Client

Client is not creatable, there's only one instance of it. It can only be accessed through its globally exposed variable.

Client is storing variables and running functions on all connected user devices (clients). It contains everything needed for the local simulation.

Client is nil in the Server execution context.

Functions

Opens the chat input.

Shows the game menu.

From there, players can exit or resume the game.

Properties

function APad coming soon

Shortcut to AnalogPad.

Triggered when pressing action 1 button. (space bar by default with a keyboard)

Client.Action1 = function()
  print("action1")
end

Triggered when releasing action 1 button. (space bar by default with a keyboard)

Client.Action1Release = function()
  print("action1 released")
end

Triggered when pressing action 2 button. (left click by default when using a mouse)

(turned off when Pointer is hidden)

Client.Action2 = function()
  print("action2")
end

Triggered when releasing action 2 button. (left click by default when using a mouse)

(turned off when Pointer is hidden)

Client.Action2Release = function()
  print("action2 released")
end

Triggered when pressing action 3 button. (right click by default when using a mouse)

(turned off when Pointer is hidden)

Client.Action3 = function()
  print("action3")
end

Triggered when releasing action 3 button. (left click by default when using a mouse)

(turned off when Pointer is hidden)

Client.Action3Release = function()
  print("action3 released")
end

Triggered when what we call the "analog pad" or "A-pad" is activated.

The A-pad maps to the mouse mouvements when using a mouse.

When using a touch screen, the A-pad is activated when moving the finger after a touch down from anywhere outside the DirectionalPad.

The function receives 2 arguments dx & dy to represent the delta compared to the previous position. When both dx & dy are equal to 0.0, it means the pad is back to idle state.

(turned off when Pointer is hidden)

-- DEFAULT IMPLEMENTATION
-- (those functions can be redefined)

Client.AnalogPad = function(dx, dy)
    Player.LocalRotation.Y = Player.LocalRotation.Y + dx * 0.01
    Player.LocalRotation.X = Player.LocalRotation.X + -dy * 0.01

    if dpadX ~= nil and dpadY ~= nil then
        Player.Motion = (Player.Forward * dpadY + Player.Right * dpadX) * 10
    end
end

Client.DirectionalPad = function(x, y)
    -- storing globals here for AnalogPad
    -- to update Player.Motion
    dpadX = x dpadY = y
    Player.Motion = (Player.Forward * y + Player.Right * x) * 10
end

This function is triggered when receiving an Event.

Events are useful to establish communication between the Server and all connected Players.

Client.DidReceiveEvent = function(event)
  -- do something with the event

  -- respond (if it makes sense in the situation)
  local response = Event()
  response.content = "Here's my response!"
  response:SendTo(event.Sender)
end

Triggered when what we call the "directional pad" or "D-pad" is activated.

The D-pad maps to the direction keys when using a keyboard (WASD by default).

When using a touch screen, the D-pad is by default represented by the virtual pad that's on the left side of the screen.

The function receives 2 arguments x & y to represent 8 possible directions or the idle state:

0,0 -> IDLE
0,1 -> UP, 1,0 -> RIGHT, 0,-1 -> DOWN, -1,0 -> LEFT
√2,√2 -> UP/RIGHT, √2,-√2 -> DOWN/RIGHT, -√2,-√2 -> DOWN/LEFT, -√2,√2 -> UP/LEFT

The norm (or length) of the (x,y) vector is always 1.0, or 0.0 when idle.

-- DEFAULT IMPLEMENTATION
-- (those functions can be redefined)

Client.DirectionalPad = function(x, y)
    -- storing globals here for AnalogPad
    -- to update Player.Motion
    dpadX = x dpadY = y
    Player.Motion = (Player.Forward * y + Player.Right * x) * 10
end

Client.AnalogPad = function(dx, dy)
    Player.LocalRotation.Y = Player.LocalRotation.Y + dx * 0.01
    Player.LocalRotation.X = Player.LocalRotation.X + -dy * 0.01

    if dpadX ~= nil and dpadY ~= nil then
        Player.Motion = (Player.Forward * dpadY + Player.Right * dpadX) * 10
    end
end

See Fog.

Inputs Inputs

See Inputs.

Triggered when a message is submitted through the chat input. Receives the message (string) in parameter.

In the default implementation, messages aren't sent to other connected users.
But it's easy to do using Event and Client.DidReceiveEvent.

-- the default implementation does this:
Client.OnChat = function(message)
    print(Player.Username .. ": " .. message)
    Player:TextBubble(message, 3, true)
end

Triggered when a player joins the game.

It doens't mean the Player has spawned, just that there's a new connection.

Client.OnPlayerJoin = function(player)
  print(player.Username .. " joined the game!")
end

Triggered when a player leaves the game.

Client.OnPlayerLeave = function(player)
  print("So long " .. player.Username .. "!")
end

Triggered when the game starts.

This is the very first Lua function to be called, a good place to initialize variables.

Client.OnStart = function()
  -- add local Player to the World and place it above the center of the map
  World:AddChild(Player)
  Player.Position = {Map.Width / 2, Map.Height + 10, Map.Depth / 2}
end

Executed ~30 times per second. Provides the elapsed time in seconds as parameter.

-- executed ~30 times per second on each user device
Client.Tick = function(dt)
  print("elapsed:", dt, "seconds")
end
UI UI

See UI.

This function is triggered when the user is about to open the menu that offers to resume or leave the experience.

Client.WillOpenGameMenu = function()
  -- possible things to do:
  -- - save user data
  -- - inform other users
  -- - if in a chat experience, set status as "away from keyboard" or something
end