-
Notifications
You must be signed in to change notification settings - Fork 15
Networking
The following functions and constants allow you to use Steam's Networking functionality.
These functions are provided for handling sending and receiving packets:
- steam_net_packet_get_data
- steam_net_packet_get_sender_id
- steam_net_packet_get_size
- steam_net_packet_receive
- steam_net_packet_send
- steam_net_packet_set_type
- steam_net_accept_p2p_session
- steam_net_close_p2p_session
- steam_net_set_auto_accept_p2p_sessions
The following functions allow handling P2P sessions:
These are the constants used by this API:
This function copies the contents of the last received packet to the given buffer. Data is copied to the start of the buffer (position remains unaffected), meaning that if you reuse the same buffer, you should "rewind" it prior to reading.
Note
If the buffer is not big enough to fit data, it will be resized automatically (the buffer needs to be created using the buffer_grow
type).
Syntax:
steam_net_packet_get_data(buffer)
Argument | Type | Description |
---|---|---|
buffer | Buffer | The buffer to write the incoming data to. |
Returns:
Example:
while (steam_net_packet_receive())
{
steam_net_packet_get_data(inbuf);
buffer_seek(inbuf, buffer_seek_start, 0);
switch (buffer_read(inbuf, buffer_u8))
{
case 1: show_debug_message("packet ID 1"); break;
case 2: show_debug_message("packet ID 2"); break;
default: show_debug_message("unknown packet"); break;
}
}
The code above will check for an incoming packet and get its data into a buffer (resizing if necessary) and reads from it.
This function returns the Steam ID of the user that sent the last received packet. It can be used in conjunction with steam_net_packet_send to send something back and for just telling the senders apart.
Syntax:
steam_net_packet_get_sender_id()
Returns:
Example:
while(steam_net_packet_receive())
{
var _sender = steam_net_packet_get_sender_id();
buffer_seek(outbuf, buffer_seek_start, 0);
buffer_write(outbuf, buffer_u8, test_network_packet.ping);
steam_net_packet_send(_sender, outbuf);
}
The above code will show a code example.
This function returns the size of the last received packet, in bytes.
Syntax:
steam_net_packet_get_size()
Returns:
Example:
while (steam_net_packet_receive())
{
var _size = steam_net_packet_size();
show_debug_message("Received " + string(_size) + " bytes.");
}
The code above will display the size of each incoming packet.
This function attempts to get the next packet from Steam API and returns whether successfully done so. Other steam_net_* functions can then be used to get packet information/contents.
Syntax:
steam_net_packet_receive(channel=undefined)
Argument | Type | Description |
---|---|---|
channel | Real | The channel the packet was sent over. Defaults to 0. |
Returns:
Example:
while (steam_net_packet_receive())
{
// process the received packet
}
The code above will attempt to get the next packet from Steam API. This would be used every step while in lobby or just from time to time.
This function sends a packet to the given endpoint and returns whether successful (as opposed to incorrect arguments/invalid ID). If no packet type is passed in then the default value will be used. The default value can be set using the steam_net_packet_set_type function. The function returns whether or not the packet was successfully sent.
Syntax:
steam_net_packet_send(user_id, buffer, size=undefined, packet_type=undefined, channel=undefined)
Argument | Type | Description |
---|---|---|
user_id | Int64 | The target user to send the packet to |
buffer | Real | Buffer that contains the raw byte array for the packet data to send |
size | Real | The size of data to send (default -1, sends the entire buffer) |
packet_type | PacketType | The type of packet to be used |
channel | Real | The channel which acts as a virtual port to send this packet on and allows you help route a message to different systems. Defaults to 0, the default channel. |
Returns:
Example:
var _buf = buffer_create(16, buffer_grow, 1);
buffer_write(_buf, buffer_string, "Hello!");
steam_net_packet_send(steam_id, _buf, -1);
buffer_delete(_buf);
The code sample will create a buffer and write to it, sending the total length of it to the given steam_id
, the buffer is then deleted.
This function sets the default connection protocol used when sending the data packets (using the steam_net_packet_send function). It returns whether or not the default protocol was successfully set.
Syntax:
steam_net_packet_set_type(protocol)
Argument | Type | Description |
---|---|---|
protocol | PacketType | The default connection protocol to be used |
Returns:
Example:
steam_net_packet_set_type(steam_net_packet_type_reliable);
The above code will set the current connection to use the steam_net_packet_type_reliable
protocol to send data packages.
This function accepts a P2P session request from the specified user. Returns whether successful or not.
Syntax:
steam_net_accept_p2p_session(userID)
Argument | Type | Description |
---|---|---|
userID | Int64 | The User ID of the user that sent the initial packet to us. |
Returns:
Example:
if (isMyFriend(userID))
{
steam_net_accept_p2p_session(userID);
}
The code above uses a custom implemented function that will check if a given userID
is a friend and if it is it will accept the P2P session.
This function closes a P2P session with the specified user. It returns whether successful or not. Steam will automatically close sessions after a period of inactivity, but you could also do it manually if you want.
Syntax:
steam_net_close_p2p_session(user_id)
Argument | Type | Description |
---|---|---|
user_id | Int64 | The user ID of the user to close the connection with. |
Returns:
Example:
if (global.chat_closed)
{
steam_net_close_p2p_session(user_id);
}
The code above check to see if a global variable (chat_closed
) is true and if so, it will close the P2P session.
This function sets whether to auto-accept session requests coming from players in the same lobby. This is enabled by default for convenience. If you disable it, you'll need to handle the async event when someone uses the steam_lobby_join_id function.
This function operates asynchronously, which means that it does not immediately return the requested result. Instead, upon completion of the task, it will trigger the Steam Async Event.
Syntax:
steam_net_set_auto_accept_p2p_sessions(enable)
Argument | Type | Description |
---|---|---|
enable | Boolean | disable/enable auto accept sessions |
Returns:
N/A
Triggers:
Key | Type | Description |
---|---|---|
type | String | The string value "lobby_join_requested"
|
lobby_id | Int64 | The lobby unique identifier |
friend_id | Int64 | The friend unique identifier |
Example:
steam_net_set_auto_accept_p2p_sessions(false);
The code above will disable the auto accept P2P sessions functionality meaning we should deal with the requests manually. In order to do so we need to use the Steam Async Event to catch the callback:
if (async_load[?"event_type"] == "lobby_join_requested")
{
steam_net_accept_p2p_session(async_load[?"friend_id"]);
}
These constants specify the type of a Steam packet.
These constants are referenced by the following functions:
Member | Description |
---|---|
steam_net_packet_type_unreliable |
Equivalent to UDP the data may or may not be delivered, will not be resent automatically. |
steam_net_packet_type_unreliable_nodelay |
Similar to "unreliable" type, but always sent instantly (as soon as the function is called). Intended for things like streaming voice data, where you want the lowest possible latency and only care about the current data. |
steam_net_packet_type_reliable |
Equivalent to TCP, the data is warranted to be delivered in order and intact. |
steam_net_packet_type_reliable_buffer |
Similar to the "reliable" type, but utilizes Nagle's algorithm to reduce the number of packets at the cost of potential delay while the data accumulates until the sending threshold. |
GameMaker 2025