Network - stingray.Network namespace reference - Stingray Lua API Reference

Description

The Network object is the main interface to the system used for playing multiplayer games over the network.

To use this system, you must first set it up it by calling an init_...() function that determines which network subsystem to use: init_lan_client(), init_steam_client(), [init_psn_client()], [init_xboxlive_client()] etc.

You can only have one network subsystem running at any one time.

After initializing the network, you use the lobby functionality of the network subsystem you are using to either create a new lobby or find an existing one (using a lobby browser). See the interface for the network subsystem you are using: LAN, Steam, PSN or Xbox Live.

Every frame, as long as your client is running, make sure that you call Network.update().

When you have enough players in a lobby, you can start a new GameSession: an object that manages a running multiplayer game. You use the same object no matter which network subsystem you are using.

Once you are completely done with using the network, you should call the shutdown_...() function for the network subsystem you are using: e.g. shutdown_lan_client(), shutdown_steam_client(), [shutdown_psn_client()], [shutdown_xboxlive_client()], etc.

Note that other network functionality, such as DLC, Steam [Leaderboards] and [Achievements], etc. are not found here.

Functions

Parameters

config_resource :

string

The resource name of the .network_config data file to hash.

Returns
This function does not return any values.

This can be used to determine if two peers are compatible.

Parameters
This function does not accept any parameters.
Returns

stingray.GameSession

The newly created game session.

You can only have one game session running at any one time. If you have an existing session, you must shut it down first by calling shutdown_game_session() before creating a new one.

Note: Both the server and the client should call create_game_session(): the server should do this when it starts the game, and the client when it drops into the game.

Parameters

min_peer_kbps :

number

The minimum upload speed allowed, in kbit/s.

initial_peer_kbps :

number

The initial upload speed, in kbit/s.

max_total_kbps :

number

The maximum upstream bandwidth allowed, in kbit/s. This is used to limit traffic on dedicated servers that serve several games.

Returns
This function does not return any values.
Parameters
This function does not accept any parameters.
Returns

boolean

Returns true if a fatal network error has occurred, or false otherwise.

Your game should respond to this condition by shutting down the Network object and dropping the player into offline mode.

For example, this condition may occur if the player signs out of the PlayStation Network during a game.

Parameters
This function does not accept any parameters.
Returns

stingray.GameSession

The current game session, or nil if no game session has been set up yet.

Parameters
This function does not accept any parameters.
Returns

string

A string that uniquely identifies this peer on the network, or nil if no peer ID has been set up yet.

  • In a LAN game, the id is a random number.
  • On Steam, this will be the player's Steam ID.

If no peer ID has yet been established, this function will return nil. This can happen on PlayStation4, where the peer ID isn't known until a connection to a room has been established.

Parameters

player_id :

string

The ID of the peer you want to connect to.

Returns

number

The time it takes for a ping to reach that player, or 0 if no connection to that player exists.

Parameters

peer_id :

string

Identifies the peer.

Returns

number

The size of the buffer in bytes, or 0 if the peer does not exist.

Initially, when a new peer joins a session, it is common to send some information over before the new peer can begin playing. Game object creation messages are spread over several frames so as to not overload the buffers that hold the information that has not yet been received on the other end. After all game objects have been transmitted successfully, you get a callback saying so. RPC messages have to be handled in a similar manner, if you are sending a lot of data when a peer joins a session; however, this is not handled internally by the engine. You can use this function to determine if it is best to stop sending RPC messages until there is more space left in the send buffer.

Note: Do not fill the buffer completely, since the engine may also send data that you are not aware of.

Parameters
This function does not accept any parameters.
Returns
This function does not return any values.
Parameters
This function does not accept any parameters.
Returns
This function does not return any values.

See the [ReplaySession] documentation for details.

Parameters

dt :

number

The delta time since the last call to update(), in seconds.

callback_object :

table

A table that contains a set of callback functions that are invoked to respond to events that occur during the networked game session.

Returns
This function does not return any values.

For each event or RPC message, a function of the callback_object table will be called immediately during the update() call.

In the case of RPC messages sent by other peers, the functions invoked are determined by the names of the messages you set up in your .network_config data file. See the RPC object documentation for details.

In the case of game events, there is a pre-set list of callback functions that you should implement in your callback object to handle the various kinds of game events that may occur:

  • game_object_created(id, creator_id)

Notifies you that the game object identified by id was created by peer creator_id.

  • game_object_destroyed(id, destroyer_id)

Notifies you that the game object identified by id was destroyed by peer destroyer_id.

  • game_object_migrated_to_me(id, old_owner_id)

Notifies you that you have now become the owner of the game object identified by id. The old_owner_id parameter identifies the peer that previously owned the game object.

  • game_object_migrated_away(id, new_owner_id)

Notifies you that you are no longer the owner of the game object identified by id. The new_owner_id parameter identifies the peer that now owns the game object.

  • game_object_sync_done(peer_id)

Notifies you that all game objects have been synchronized with the specified remote peer after a new peer was added, so from now on RPC calls will be ordered with respect to game object creation messages. Before this callback, you are not guaranteed that RPC calls and game object creation calls arrive in the same order they are called.

  • game_session_disconnect(host_id)

Notifies you that you have been disconnected from the game session by the game session host. Either you are kicked out, or the game session host left the game.

If you want to reduce network latency slightly, you can instead call update_receive() to receive network communications, respond to them in your game, and then update_transmit() later in the same frame.

Parameters

dt :

number

The delta time since the last call to update(), in seconds.

callback_object :

table

A table that contains a set of callback functions that are invoked to respond to events that occur during the networked game session. For details, see the description for update(). In order to reduce latency by one frame, you can call update_receive() and update_transmit(), in that order, instead of a single call to update(). That way, you may be able to respond to the received information within the same frame, before transmitting.

Returns
This function does not return any values.
Parameters
This function does not accept any parameters.
Returns
This function does not return any values.

That way, you may be able to respond to the received information within the same frame, before transmitting.

Parameters

details :

string

The string to write in the network log file.

Returns
This function does not return any values.

When you use the Network Analyzer tool to examine the network log file, these tags are shown as vertical markers in the bandwidth graphs.

Configuration

The elements in this group are related to configuring the network connection.

Parameters

message_name :

string

The name of the RPC message type you want to retrieve information about, which must correspond to the name of a message specified in your .network_config data file.

Returns

network_message_info

A table that contains metadata about the specified message type.

Parameters

object_name :

string

The name of the game object type you want to retrieve information about, which must correspond to the name of an object specified in your .network_config data file.

Returns

network_object_info

A table that contains metadata about the specified game object type.

Parameters

time :

number

The new minimum time between successive transmits, in seconds.

Returns
This function does not return any values.

The default value is 0.03.

Parameters

time :

number

The new time before sending a keep-alive message, in seconds.

Returns
This function does not return any values.

The default value is 0.5.

Parameters

time :

number

The new time before sending a keep-alive message, in seconds.

Returns
This function does not return any values.

The default value is 1.0.

Parameters

time :

number

The new maximum time before a receiver is assumed lost, in seconds.

Returns
This function does not return any values.

The default value is 60.0.

Parameters

time :

number

The new resend time to set, in seconds.

Returns
This function does not return any values.

The default value is 0.2.

Parameters

type_name :

string

The name of the data type you want to retrieve information about, which must correspond to the name of a type specified in your .network_config data file.

Returns

network_type_info

A table that contains metadata about the specified data type.

Data types

Identifiers for different data types, used by the Network.type_info() function.

ARRAY : integer

Data type for arrays.

FLOAT : integer

Data type for floating-point numbers.

IDSTRING32 : integer

Data type for IdString32.

IDSTRING64 : integer

Data type for IdString64.

INT : integer

Data type for integer numbers.

QUATERNION : integer

Data type for quaternion rotations.

RESOURCE_ID : integer

Data type for resource IDs.

STRING : integer

Data type for string values.

UINT64 : integer

Data type for 64-bit integer values.

VECTOR3 : integer

Data type for three-dimensional vector values.

Debugging

The elements in this group are related to debugging networked games.

Parameters

log_level :

integer

May be any value from the logging level group, such as MESSAGES or WARNINGS.

Returns
This function does not return any values.

Debugging log levels

Log levels that you can pass to Network.log().

SILENT : integer

No debugging output at all.

SPEW : integer

Game object updates and detailed information are printed, as well as everything above.

Development only

The elements in this group are only available in development builds.

Do not use them in your final builds.

Parameters

dump_file :

string

The path to a file that contains a dump of the network session.

Returns

stingray.GameSession

A new GameSession object set up as a "Replay Session".

See the GameSession object documentation for details.

Parameters

peer_id :

string

The ID of the peer for which to disable Quality of Service.

Returns
This function does not return any values.

You can re-enable it by calling enable_qos_peer().

Only available in development builds, for the purposes of debugging the Quality of Service system. Note that you must enable Quality of Service by calling enable_qos() in order for this function to have any effect.

Parameters

peer_id :

string

The ID of the peer for which to re-enable Quality of Service.

Returns
This function does not return any values.

Only available in development builds, for the purposes of debugging the Quality of Service system. Note that you must enable Quality of Service by calling enable_qos() in order for this function to have any effect.

Explicit connections

The elements in this group are related to configuring explicit connections.

Parameters
This function does not accept any parameters.
Returns

string[]

A table that lists the peer IDs.

The [] notation indicates that this type is an array: a table in which the keys of the members are sequential integers, and the value of each element is an instance of the type shown.
Parameters

peer_id :

string

The unique ID of the peer you want to connect to.

Returns

integer

An ID for the newly created connection

There can be at most one connection to any particular peer, so you cannot call this function if a connection to the peer already exists.

Note that directly connecting to a peer in this way is only supported by certain transport layers (e.g. Steam). In other transport layers (e.g. PSN), connections are always set up by rooms and lobbies, so there is no way to connect directly to a peer.

Parameters

peer_id :

string

The unique ID of the peer you want to disconnect from.

Returns
This function does not return any values.

Note that you cannot destroy a connection that is used by higher layers of the network stack, so you must call is_used() first in order to check that the connection can be destroyed before you call this function.

Parameters

peer_id :

string

The unique ID of the peer you want to test.

Returns

integer?

The ID of the connection to this peer, or nil if no connection exists.

The ? notation indicates that this type is optional: there may be zero or one instances of it.
Parameters

peer_id :

string

The peer whose connection will be tested.

Returns

boolean

Returns true if the connection is broken, or false otherwise.

Parameters

peer_id :

string

The peer whose connection will be tested.

Returns

boolean

Returns true if the connection is in use, or false otherwise.

You cannot explicitly destroy a connection while it is being used, so this function must return false before you can call destroy_connection(). If this function returns true, and you want to close the connection, you must first shut down any lobbies, rooms, game sessions, etc. that are using the connection to this peer.

Parameters
This function does not accept any parameters.
Returns
This function does not return any values.

LAN Management

Parameters

lobby_port :

integer

The UDP port you want to use for lobby communications. Note that this should not be the same number used as the game port in your call to init_lan_client(). Clients browsing for lobbies will specify the lobby port number, so you should use a single well-known port number for all peers running lobbies.

max_members :

integer

The maximum number of peers that can join this lobby.

Returns

stingray.LanLobby

The newly created lobby object.

When you create a new lobby using this function, you automatically join the lobby. You do not need to call join_lan_lobby().

Parameters

config :

string

The name of the .network_config resource to use for setting up the client, which determines the types of game objects and RPC messages that can be used during the session.

game_port :

integer?

The UDP port you want to use for network communication. If you specify 0 or omit this parameter, a random free port will be picked. This is good for testing multiple network nodes on the same machine, since it ensures that port numbers won't collide.

The ? notation indicates that this type is optional: there may be zero or one instances of it.

peer_id :

integer?

If specified, this number will be used as the ID to identify the client to other network nodes. If omitted, a random number is assigned. Note that the peer id must be unique to all nodes on the LAN, otherwise messages will not arrive on the proper destination.

The ? notation indicates that this type is optional: there may be zero or one instances of it.
Returns

stingray.LanClient

The new client object.

Make sure that you call Network.update() every frame after initializing the LAN client, and call Network.shutdown_lan_client() when you no longer need it.

Parameters

address :

string

The IP address and lobby port number of the host running the lobby. This string follows the format ip:port, and is typically retrieved from the information table returned by a call to LanLobbyBrowser.lobby().

lobby_port :

integer?

The port that the lobby should use on this computer.

If you specify 0 for the lobby_port, or you omit the parameter, a random free port will be used. This is good when you are testing multiple nodes on the same machine, because it means the port won't collide with the public lobby port used by create_lan_lobby(). The drawback of this approach is that if the lobby host quits and the lobby migrates to you, other nodes won't be able to find your lobby because it is running on a random port. To make the lobby publicly findable if it migrates to you, you need to use the same public port as you use in create_lan_lobby(). However, this means that you cannot run multiple network nodes on the same machine.

The ? notation indicates that this type is optional: there may be zero or one instances of it.
Returns

stingray.LanLobby

The lobby you have joined.

Parameters

lobby :

stingray.LanLobby

The lobby you want to leave.

Returns
This function does not return any values.

If you are hosting the lobby, it will migrate to a different peer in the lobby. If you are the last member of the lobby, the lobby will be removed from the network.

Parameters

client :

stingray.LanClient

The LAN client that you want to shut down.

Returns
This function does not return any values.

Steam network management

Parameters

type_lobby :

integer

The type of lobby to create. The type can be one of the lobby type constants.

max_members :

integer

The maximum number of members allowed in the lobby.

Returns

stingray.SteamLobby

The newly created Steam lobby.

When you create a lobby you automatically join it; you don't have to call join_steam_lobby().

Parameters

config :

string

The name of the * .network_config* file to use for setting up the client, which determines the types of game objects and RPC messages that can be used during the session.

Returns

stingray.SteamClient

The new Steam client object.

Note that Steam must be running before you can call this function. See the stingray.Steam object and the Steam.connected() function.

Parameters

config :

string

The name of the * .network_config* file that you want to use for setting up the server, which determines the types of game objects and RPC messages that can be used during the session.

settings :

network_steam_server_settings

A table that contains configuration settings for the server, expressed as key/value pairs.

Returns

stingray.SteamGameServer

The newly created Steam game server.

Parameters

lobby_id :

integer

The ID of the Steam lobby.

Returns

stingray.SteamLobby

A newly created SteamLobby object that represents your connection to the lobby.

Parameters

ip_address :

string

The IPv4 address in the format a.b.c.d:e, where the first four numbers are the components of the IPv4 address of the host, followed by a colon, followed by the number of the query port used by the server. For example: 127.0.0.1:27016.

password :

string?

The password expected by the server, if any. Only required for connecting to a password-protected server.

The ? notation indicates that this type is optional: there may be zero or one instances of it.
Returns

stingray.SteamGameServerLobby

A lobby object that tracks the connection to the server.

A lobby object is returned that is used to check the connection progress to the server.

Parameters

ip_address :

string

The IPv4 address in the format a.b.c.d:e, where the four numbers are the components of the IPv4 address of the host. For example: 127.0.0.1.

port :

number

The number of the query port used by the server. For example, 27016.

password :

string?

The password expected by the server, if any. Only required for connecting to a password-protected server.

The ? notation indicates that this type is optional: there may be zero or one instances of it.
Returns

stingray.SteamGameServerLobby

A lobby object that tracks the connection to the server.

A lobby object is returned that is used to check the connection progress to the server.

Parameters

lobby :

stingray.SteamLobby

The Steam lobby you want to leave.

Returns
This function does not return any values.

If you are hosting the lobby, it will migrate to a different user in the lobby. If you are the last member of the lobby, the lobby will be removed.

Parameters

server_lobby :

stingray.SteamGameServerLobby

The lobby you want to remove.

Returns
This function does not return any values.
Parameters

client :

stingray.SteamClient

The Steam client that you want to shut down.

Returns
This function does not return any values.
Parameters

server :

stingray.SteamGameServer

The Steam game server you want to shut down.

Returns
This function does not return any values.

Steam Lobby types

Constants used to represent the different types of Steam lobby.

Indicates that the lobby Can be joined by friends.
Indicates that the lobby is not visible to other friends, but can returned by a search.
Indicates that the lobby can only be joined by invitees.
Indicates that the lobby is visible in the lobby list.