In this post I'll explain how custom variables (anything other than direction, x, y, sprite_*, for example) should be synced. This tutorial assumes you already have a basic game that shows a login box and has a "player object" and an "other player object" set up. If you don't, the getting started tutorial covers the basics of creating a new game.
Let's assume there's some kind of variable, I'll call it "clothing_sprite", that stores which sprite is drawn on top of the player sprite, like this:
We also want that sprite to appear on the other player object, and it should be the same sprite as the player is currently using. For the purposes of this tutorial, I'll assume sprites can be switched by pressing 1, 2, or 3. Add this code to the step event of the player object:
I like to keep the amount of duplicate code in my projects to an absolute minimum. I don't want to have to update the drawing code twice if I decide I want to change it. Create a new object, and name it something like "par_players". It'll be the place where code for both the player object and the other player object can be placed.
Add the code for drawing the clothing to the draw event:
Initialize the variable in the create event:
Set the parent of both the player object and the other player object to this new "par_players" object. If you've already got a draw or create event added to the player or other player object, make sure to invoke the inherited event as well:
Lastly, the variable's value will need to be sent to the other clients. You won't need to take care of sending the variable to new players that connect, or making sure you only set the variable when changing it.
Add this code to the step event of the player object:
This piece of code will tell the extension that there is a variable named clothing_sprite that will need to be sent to the other players. The extension will then do all the hard work of deciding the best way to send the variable and when to send the variable.
Note: If you know a variable is not an integer value, make sure to round(...), floor(...) or ceil(...) the value if you only use the integer part of the number. If GameMaker Server will use less bandwidth if the variable is an integer. If you're sending a number as a string, convert it to a real fist. Strings generally use more bandwidth than numbers when sending them. gms_self_set_precision(...) can also be used to specify how precise the value needs to be sent.
Add this code to the draw event of the other player object:
This will obtain the variable's value and store it in "clothing_sprite". Then, it calls the inherited draw event, which will draw the player's sprite and the clothing sprite on top. The player_id variable is automatically created by the extension, and is used to identify other players. It'll exist in every instance of the other player object created by the extension.