Phame utilizes an interaction-based networking system which performs all actions client-side. Certain actions (that affect other players) are deemed interactions and must be requested from the server. Then, the server will inform all players to perform the interactions in the same order.
The networking module handles connections between the client and server, and facilitates messaging between them. For Phame, a few types of messages have become standard.
Interactions are an extension of the action module, identifying certain actions as necessary to be replicated on all clients at the same time. These 'interactions' are formally requested by a single client, and then flow out to all clients by the server for same time performance.
Start by implementing actions per the Action Module | Implementation.

Networking inserts itself between the request_action() and perform_action() functions, where instead of one just calling the other, request_action() writes an interaction request to the server, who then reflects it back to all clients. Upon receiving, read_interaction() is called, which is usually responsible for returning the path to perform_action().

When requesting an interaction, all details regarding it must be specified and not up to interpretation by each client to avoid desync scenarios.
These action-specific details are captured in the interaction body, and each time the action is requested, the same types of details will be present.
For example, let's take an action request to freeze a specific instance at it's current location:
request_action = function(action) {
switch action {
case ACT_FREEZE:
// @param {Id.Instance} target Whom to freeze
var target = argument[1]
// Perform action locally
perform_action(target)
break
}
}
Before networking across clients, it would be sufficient to provide just the instance's ID. However, when that same instance could be at a different position on each client, its important to specify exactly where to freeze it. Also, GameMaker's Id.Instance is not guarenteed the same across clients, and so the object needs to be a child of par_interactable, and the instance's interactable_id is to be used instead.
Taking this into consideration, we can update the parameters to completely specify the interaction:
request_action = function(action) {
switch action {
case ACT_FREEZE:
// @param {Real} interactable_id Whom to freeze
// @param {Real} x Coordinate where to freeze
// @param {Real} y Coordinate where to freeze
var target = argument[1]
var _x = argument[2]
var _y = argument[3]
// Perform action locally
perform_action(target, _x, _y)
break
}
}
This however, is still not networked. To make this an interaction that occurs on all clients at the same time, we need to send the request to the server instead of just performing it. To package the details for the server, we use a specialized struct called interaction_body.
request_action = function(action) {
switch action {
case ACT_FREEZE:
// @param {Real} interactable_id Whom to freeze
// @param {Real} x Coordinate where to freeze
// @param {Real} y Coordinate where to freeze
var target = argument[1]
var _x = argument[2]
var _y = argument[3]
// Request action to be performed across all clients
// Customize interaction parameters, buffer is deleted after request is sent
var interaction_body = buffer_create(6, buffer_fixed, BUFFER_ALIGNMENT)
buffer_write(interaction_body, buffer_u16, target)
buffer_write(interaction_body, buffer_u16, _x)
buffer_write(interaction_body, buffer_u16, _y)
// Create and request interaction
var interaction = new stc_interaction(-1, action, interactable_id, interaction_body)
client_request_interaction(interaction)
break
}
}
In the interaction body, there are a few things that may change between each interaction. These are:
Each parameter that is required for the interaction, must be written to the buffer. To do this, add a buffer_write() for each one, specifying the bytes required with the appropriate equivalent to buffer_u16 (see all options here).
Depending on the bytes required the buffer size and type might change. Total up the number of bytes needed using the reference below and replacing 6 in buffer_create(6, buffer_fixed, BUFFER_ALIGNMENT).
| Parameter | Buffer Data Type Constant | Size |
|---|---|---|
| Real 0 - 255 | buffer_u8 | 1 |
| Real -128 - 127 | buffer_s8 | 1 |
| Real 0 - 65,535 | buffer_u16 | 2 |
| Real -32,768 to 32,767 | buffer_s16 | 2 |
If buffer_string or buffer_text is needed, then buffer_fixed must be changed to buffer_grow to account for the variable length of bytes required.
NOTE:
The values of the details themselves can be specified by the requesting client or authoritatively by the server before reflecting out. The choice of when depends on whether the client has the authority to specify the detials.
For example, when a player's block freezes in the air for DFnI, that is determined by that player's client since it needs to be predictable by the player.
See the other tab for an example.
The Interaction Available System can be utilized to replace the tracking of separate states. For example, tracking whether a lever is on or off:
Create Event
#region Actions
/// @function perform_action(action, [arguments])
/// @description Impacts the games
/// @param {real} action Constant associated with action, other arguments depend on action
perform_action = function(action) {
switch action {
case ACT_BUILD_TOWER:
var build_tower = argument[1]
instance_create_layer((mouse_x div GRID) * GRID, (mouse_y div GRID) * GRID, "lay_instances", tower_list[build_tower])
obj_game_control.money -= tower_costs[build_tower]
break
}
}
/// @function request_action(action, [arguments])
/// @description Intermediate function that validates actions
/// @param {real} action Constant associated with action, other arguments depend on action
request_action = function(action) {
switch action {
case ACT_BUILD_TOWER:
var build_tower = argument[1]
// Request action to be performed across all clients
// Customize interaction parameters, buffer is deleted after request is sent
var interaction_body = buffer_create(1, buffer_fixed, BUFFER_ALIGNMENT)
buffer_write(interaction_body, buffer_u8, build_tower)
// Create and request interaction
var interaction = new stc_interaction(-1, action, interactable_id, interaction_body)
client_request_interaction(interaction)
break
}
}
/// @function read_interaction(action, buff)
/// @description Interaction was sent over the server, so perform it
/// @param {real} action Constant associated with action
/// @param {id.buffer} buff Buffer to read arguments from
read_interaction = function(action, buff) {
buffer_seek(buff, buffer_seek_start, 0)
switch action {
case ACT_BUILD_TOWER:
var build_tower = buffer_read(buff, buffer_u8)
perform_action(action, build_tower)
break
}
}
/// @function debug_interaction(action, buff)
/// @description Interaction was sent over the server, so document it
/// @param {real} action Constant associated with action
/// @param {id.buffer} buff Buffer to read arguments from
debug_interaction = function(action, buff) {
buffer_seek(buff, buffer_seek_start, 0)
switch action {
case ACT_BUILD_TOWER:
var build_tower = buffer_read(buff, buffer_u8)
return string("Tower to build: {}", build_tower)
}
}
#endregion
To standardize the method to ensure only one request is sent at a time, the interaction available system was developed.
The system works by utilizing a ds_map, (which allows the action constants to be the keys to values) and stores a flag for each action on whether it can be requested. Depending on the implementation, the system can make an action a one-time request, or reset the flag upon performance of the action.
To add the system to an interactable, initialize the ds_map for the actions in the interactable Create Event:
interaction_available = ds_map_create()
ds_map_add(interaction_available, ACT_RESPAWN, true)
ds_map_add(interaction_available, ACT_ESCAPE, true)
Under request_action add a conditional prior to the actual request:
if interaction_available[? action] {
// Request interaction
interaction_available[? action] = false
}
The availability flag can be reset upon performance by modifiying perform_action with:
interaction_available[? action] = true
Any interactable using the interaction available system require the ds_map to be cleaned up with a Clean Up Event:
/// @description Clean up
ds_map_destroy(interaction_available)
The Interaction Available System can be utilized to replace the tracking of separate states. For example, tracking whether a lever is on or off:
Create Event
lever_on = false
previous_lever_on = lever_on
Step Event
if place_meeting(x, y, obj_character) {
lever_on = true
}
else {
lever_on = false
}
if lever_on != previous_lever_on {
previous_lever_on = lever_on
request_action(ACT_ACTIVATE, lever_on)
}
Interaction logging is built into interaction performance, with implentation determined by scr_networking_client_interface.
Add a debug_interaction function to each interactable by copying the read_interaction function, but return a string describing the optional parameters instead of performing the action with them.
Iteractions occur the same way for every client, but sometimes special feedback is required to the requesting client. For example, when asking to join the game, the resulting game_player_id is determined by the server.
Clients can request authoritatve information using an INFORMATION_CMD for things like what player_id to use for a new joining player.
obj_server receives a reliable CLIENT_CONNET message and calls server_connect_client which creates Connected_client
With Phame Networking v1.3.4, interaction structures, debug, and a standardized method to reduce requests was implemented.
Instead of specialized client request functions for each possible parameter combination, a consistent interaction structure will now be used across client request, server approvals, client receiving, and client performance of interactions.
In an interactable's request_action, the specific request_interaction_x() is now replaced with the creation of a stc_interaction.
For example,
request_interaction_u8_string(interactable_id, interaction, ready_to_start, game_player_name)
Would be changed to:
// Customize interaction parameters, buffer is deleted after request is sent
var interaction_body = buffer_create(2, buffer_grow, BUFFER_ALIGNMENT)
buffer_write(interaction_body, buffer_u8, ready_to_start)
buffer_write(interaction_body, buffer_string, game_player_name)
// Create and request interaction
var interaction = new stc_interaction(-1, action, interactable_id, interaction_body)
client_request_interaction(interaction)
This system allows infinite interaction parameter customization without requiring changes on the Phame Networking module to provide specific client request functions moving forward. Note the size and type of buffer created, 2, buffer_grow. For many cases where the input is known, the specific amount of bytes can be specified with a buffer_fixed.
For read_interaction, add buffer_seek(buff, buffer_seek_start, 0) as the first line and outside of the switch statement to address the new interaction_body buffer.