Player-to-player messages

Player-to-player (p2p) messages are a useful mechanism when creating more advanced games. A p2p message is essentially a "packet" with an ID and a few arguments.

Sending p2ps


You can send p2p messages by calling gms_p2p_send. It requires a player_id to whom the message will be sent, an id, and optionally up to 14 arguments. You can choose the ID to be anything between 0 and 255. It's useful to make macros for these IDs, so that you can easily see what an ID is supposed to do. In my examples, I always add macros prefixed with p2p_.

Receiving p2ps


To receive p2ps you'll need to set a script using gms_script_set_p2p. That script will be called whenever the game receives a p2p message from another player. The general structure of that script should look something like this:

var mid = argument0, sender = argument1, args = argument2;
switch mid
{
    case p2p_first:
        ....
        break;
    case p2p_second:
        ....
        break;
    default:
        show_error("Unknown p2p message with message id " + string(mid) + " received!", true)
}


The piece of code between the case [id]: and the break statement is the code that will be executed when a p2p message with that id is received. I'll annotate future code with just '// p2p_...' to keep it readable. It should be inside the p2p script, inside the switch-statement.

Example use-cases


Keeping kill count
So you want to keep track of how many kills everyone makes. You'll need a way to let the killer know that he killed someone. You can do this by sending a p2p message whenever a player dies to its killer. For example:

// obj_player, event: collision with synced bullet

gms_p2p_send(p2p_got_kill, other.owner) // other.owner is automatically set by the extension

// p2p_got_kill, inside p2p script:

show_message("I killed " + string(gms_other_get(sender, 'name')) + "!")
score += 1 // Or whatever else you want to do


Special sending targets


Since update 1.9.9.1 GameMaker Server supports sending P2P messages to more than one player at a time. The following special values can be used instead of a player_id:


  • gms_p2p_all_in_room - sends the message to all players in the same room as you

  • gms_p2p_all_in_session - sends the message to all players in the same session

  • gms_p2p_all_in_game - sends the message to all players in your game that are running the same game version, which you set in gms_settings



Note in particular 'gms_p2p_all_in_game', which can now be used to communicate across different sessions. One use case for this might be broadcasting global admin messages.

Replies (6)

Last message on 17 Oct 2020

amkgames on 5 Apr 2015, 21:21:01
What is player ro player message? Is it whisper? Or PM?
Forgeio on 5 Jun 2017, 05:38:09
It's actually not an actual message that you would send to someone, it is a way of sending data to (an)other player(s) instead of to the server.
Alexand3er78 on 12 Oct 2020, 16:14:59
How can you know the player_id of a player?
Size43 (Administrator) on 17 Oct 2020, 17:04:21
By default, each other player object has the variable player_id set to the player ID. You can also use gms_other_find in combination with gms_other_count to iterate over all players in the session.
Size43 (Administrator) on 6 Apr 2015, 11:17:46
It's a way to send a custom message to another player. It's not a chat message, but a set of values that can be strings or doubles.
amkgames on 5 Apr 2015, 21:21:24
I mean To*