Synchronising instances

Before you start

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.

This post will consist of two parts. In the first part, I'll cover the ideas and theories behind instance synchronization. I'll explain what the different types of instance synchronization are, and when to use them. In the second part, I'll show how to get instance synchronization to work in your game.

In this tutorial, I'll use the word "client" a few times. A client is an instance of the game, connected to the server. For example, if there are three players playing your game, that means three clients are connected to the server. When a player moves, that moment is sent from one client to the other clients.

Different types of instance synchronization

There are three basic types of instance synchronization: One-time, Extended and Full synchronization. Every type of synchronization can be local or not local.

One-time instance synchronization synchronizes the instance exactly once. It does not synchronize it's destroy or any variable that changes after it's synchronized.

Extended instance synchronization will synchronize the instance twice: Once when calling gms_instance_sync(...), and once when the instance is destroyed. This type of synchronization does not synchronize any variables that are changed after creating it.

Full instance synchronization will synchronize the instance when calling gms_instance_sync(...), when a variable changes, and when it's destroyed. This type of synchronization is very similar to how the "player object" is synchronized. Instances will be sent to other players when they join the room.

Local instance synchronization will only synchronize the instance if it's close to the player. It'll try to predict which instances will be seen by the player, and the server will only synchronize those instances. For full instance synchronization, the instance will still be synchronized to other players, but its variables and position are only synchronized if the player is close enough to the instance.

Whether the player can see the instance or not, is determined by the "Syncing-distance". This distance can be set on the site, under Developer -> Game-settings -> Syncing distance. A good estimate for how large the Syncing distance should be, is to take the room_width or room_height (whichever one is higher) and multiply it by 1.5.

Each type can be represented by a constant:

  • One-time synchronization: is_onetime

  • Extended synchronization: is_extended

  • Full synchronization: is_full

By default, these constants are all not local. To make one of the synchronisation types local, its constant must be bitwise or'ed ('|') with the constant isc_local. (bitwise operators are outside the scope of this tutorial, there are some great tutorials about bitwise operators out there if you're interested) For example:

is_onetime | isc_local

is_extended | isc_local

is_full | isc_local

When to use what type

When creating a multiplayer game, it's important to keep the data usage to an absolute minimum. Instance synchronization can use a lot of data. Therefore, it's important you use the right type of synchronization for the right type of instance.

To decide what type of synchronization you need, you should consider whether the moment an instance is destroyed and how and when the instance moves or changes variables is predictable.

For example: A bullet is destroyed when it collides with a wall. Because a collision with the wall will happen at the same time on all clients, it is not necessary to synchronize the destroy of the bullet.

On the other hand, assume an object is destroyed when a player clicks on it. Not all players will click on the same object at the same time, so for the other clients there is no way of knowing, without explicitly sending the destroy to the other clients, whether the instance has been destroyed.

The same goes for movement or variables. If the value of the variable or the position of the object is predictable, it does not need to be sent.

The points above can be formulated in 3 questions:

  • "Is it possible to know what the value of a variable of the object is and will be without sending it?"

  • "Is it possible to know what the position of the object is and will be without sending it?"

  • "Is it possible to know at what moment in time the object will be destroyed, without sending it?"

Each different type of instance synchronization will fulfil a different need:

  • One-time synchronization: position predictable, destroy predictable, variables predictable.

  • Extended synchronization: position predictable, destroy unpredictable, variables predictable.

  • Full synchronization: position unpredictable, destroy unpredictable, variables unpredictable.

Note how not all possible combinations are covered. The three types are the most common ones. There aren't many cases where the value of variables is unpredictable, but the position is.

That's all you need to know to get started. Keep in mind you'll most likely (and should) use One-time and Extended synchronization most of the time. Full synchronization uses a lot of CPU power and data compared to the other two options, so use it with care!

Adding instance synchronization to a game

Now that I've covered the different types of synchronization and when to use them, I'll use this second part of the tutorial to show a few examples & describe how to add instance synchronization to your game. I'll be using the game created in the getting started tutorial as a start.

Make sure you've set the Syncing distance on the GameMaker Server website.



Add a call to gms_settings_declare_syncable_object right before the call to gms_settings:


gms_settings_declare_syncable_object(obj_bullet)
gms_settings(...) // This part is already in your code, you can leave it unchanged


This lets the extension know that this object can be synced to other clients. Without this code, other clients would silently ignore any synced instance of this object they receive.

I'll cover two of the three different types of instance synchronization here:

  • One-time instance synchronization

  • Full instance synchronization

Extended instance synchronization is very similar to One-time synchronization.

Shooting (One-time synchronization)

Create a new object and name it "obj_bullet" and give it a sprite.

Open obj_player, and add a Global Mouse Left Pressed event.

GM8 / GM8.1:

Add the following piece of code:

i = instance_create(x, y,obj_bullet);
i.speed = 8;
i.direction = point_direction(x, y,mouse_x, mouse_y);
i.damage = random_range(30,50);
gms_instance_sync(i, is_onetime | isc_local, “damage”);

GM:Studio:

In GM:Studio, you'll need to do a little more work when synchronizing an instance variable. Note how in the GM8 code above, the custom variable is indicated to the extension by simply passing a string containing the variable name to gms_instance_sync(...). Internally the extension will use variable_local_get to obtain the variable value. In GM:Studio variable_local_get is removed.

GameMaker Server has a special function that is used to set both the variable name and value for instance synchronisation, gms_instance_sync_var_add(...). Add the following piece of code to the Global Mouse Left Pressed event:

i = instance_create(x, y,obj_bullet);
i.speed = 8;
i.direction = point_direction(x, y,mouse_x, mouse_y);
i.damage = random_range(3050);

gms_instance_sync_var_add(“damage”,i.damage);
gms_instance_sync(i, is_onetime | isc_local);

Regardless of what type of synchronization you're using, these variables are only sent once to the other clients. The variables x, y, speed and direction are always sent.

Once the instance is created on the other clients, the User Defined 12 Event is called.

GM:Studio:

You'll also need to manually set the custom synchonized variables, because internally the extension uses variable_local_set, which is once again, removed in GM:Studio. Open obj_bullet and add this piece of code to the Event User 12 event:

damage =ds_map_find_value(variable_map, “damage”);

variable_map is a variable that contains a ds_map with all the synchronized variables in it. The ds_map is destroyed after the User Defined 12 event has executed.

The speed and direction (and any custom variables / the variable_map variable) are not set in the create event of obj_bullet. You must use the User Defined 12 event if you want to use the synchronized variables.

The game is now in a state you can run it. If you want, you can test what you have so far.

If you want, you can add walls to your game. Try adding a piece of code that'll destroy the bullet once it hits a wall. (remember, when obj_bullet hits a wall is predictable).

Try giving the player health and make the bullets do damage to the other players. Keep in mind obj_other_player is only a representation of the obj_player object on another client. Handle the collision with obj_bullet in obj_player only. To check if the player fired the bullet, use special owner variable that's automatically set after calling gms_instance_sync(...).

Spawning & synchronizing enemies (Full synchronization)

Let get on to something more advanced. Create an object and name it obj_npc. For the purpose of this tutorial, give it a completely white sprite. We'll use that later on to assign a random image_blend color to every NPC.

In the create event:

  • Set alarm[0] to 1 second

  • Set the speed to 4

Add this code to the alarm[0] event:

direction= choose(090180270);
image_blend =irandom(c_white);
alarm[0] = room_speed;

GM:Studio:

Add a call to randomize() in the GMS object or another initialization object to make sure the NPC will walk randomly every time.

Destroy the instance when it hits a bullet.

Create an object named obj_npc_spawner. Make it spawn obj_npc every 3 seconds, until there are 5 obj_npcs walking around:

Create:

alarm[0] = room_speed * 3;

Alarm[0]:

alarm[0] = room_speed *3;
if(instance_number(obj_npc) < 5)
{
 instance_create(x, y, obj_npc);
}

When tasked with changing the code above to synchronize the instance every time it's created, your first reaction would probably be to just add a gms_instance_sync below the instance_create line. Though that would work, it also creates a problem. Consider this: If 3 players are online at the same time, every single client will have the obj_npc_spawner spawn an NPC every 3 seconds. That means 3 NPCs will spawn every 3 seconds. If 5 players are online, every 3 seconds up to 5 new NPCs will spawn.

To keep the spawn rate the same, only one client should create instances. To decide which client should spawn the instances, the master player can be used. The master player is assigned by the server to one client, and will change automatically when a player logs out or other players join. Change the code to this:

alarm[0] = room_speed *3;
if(gms_self_ismaster())
{
 if(instance_number(obj_npc)< 5)
 {
 instance_create(x, y, obj_npc);
 }
}

GM:Studio:

Because image_blend is a built-in variable, it can be obtained and set by the extension automatically. There's no need to add a call to gms_instance_sync_var_add(...) in this case.

Note the alarm will go off regardless of whether the player is the master player or not. This is done so that in case the master player disconnects, the alarm on the client that becomes the next master player is still running. The gms_self_ismaster() check is done every 3 seconds on every client to make sure another client will be able to pick up seamlessly when the master player disconnects.

The NPC object itself will also need to be changed a little. If every client tried to change the direction of the NPC every second, it would get a mess very quickly. The obj_npc object would move in multiple directions at the same time, because every client set it to a different direction. That's why an instance can only be controlled by one client at a time. The instance is "owned" by a particular instance. The functions gms_instance_is_owner(id) and gms_instance_get_owner(id) will return whether the client owns the instance and what player owns the instance respectively.

Unlike the master player, instances do not change owner automatically. Every instance will need to be handed over to another player by calling gms_instance_handover(...) or gms_instance_handover_all(). The server will decide what player the instance is handed over to. This means if you don't want instances to be destroyed when a player moves to another room or logs out, you need to hand over instances in these events:

  • Room end

  • Game end

Edit the alarm[0] event of obj_npc:

if(gms_instance_is_owner(id))
{
 image_blend = irandom(c_white);
 gms_instance_set(id,“image_blend”, image_blend);
 direction = choose(0,90180270)
}
alarm[0] = room_speed;

Add this code to the step event to update the image_blend variable if the instance is not owned by the client:

if(!gms_instance_is_owner(id))
{
 image_blend = gms_instance_get(id, "image_blend");
}

Lastly, make sure the instance is handed over to another player when the owner of the instance disconnects or changes room:

Game End & Room End event:

if(gms_instance_is_owner(id))
{
 gms_instance_handover(id);
}

You may want to add an Outside Room event to obj_npc, and make it warp in both directions to keep the NPC's on the screen. Add an instance of obj_npc_spawner to rm_play.

That's it!

Finally. This tutorial is much longer than I intended to make it, but I wasn't able to make it any shorter without removing parts of the tutorial. Let me know what you think! If something is unclear, please ask about it in the comment section below, or send me a PM on the GameMaker Forums. What subject should I do next?

Replies (129)

Last message on 4 Feb 2023

alexlimitedgames on 21 Dec 2022, 21:00:51
hello size I am trying to place instances in the coins of my game and the same with the enemies. The coin is created when I place it in the room, my goal is that if someone takes the coin and it is eliminated, no one else can do it. The code for the coin is this: event collide with obj player

///Collect the coin

//Play 'Coin' sound
audio_play_sound(snd_coin, 0, false);

//Increment branch warp counter
if (instance_exists(obj_warpbranch)) {

with (obj_warpbranch) coins++;
}

//Increment coins
global.coins++;

//Get 50 points
score += 50;

//Destroy
instance_destroy();
gms_instance_sync(id, is_full)
//Create sparkle effect
with (instance_create(x,y,obj_smoke))
sprite_index = spr_sparkle;

if you did, put gms_instance_sync next to instance_destroy(); because I want it to be synchronized when the coin is destroyed when caught by a player, I would appreciate it if you help me
Size43 (Administrator) on 21 Jan 2023, 18:39:13
Apologies for the delay.

That's not how instance syncing works. You'll need to call gms_instance_sync the instance when it is created, and from that point on the instance will be tracked by the extension and synced to other clients. If you synced with is_extended or is_full, the destroy will also be synced to other clients.
alexlimitedgames on 22 Jan 2023, 00:38:57
Hi Zize43, don't worry, the delay is fine. I understand you're busy. I wanted to tell you that I did what you told me, put gms_instance_sync(id, is_extended); in the coin creation event and tried the online game,
when the player touched the coin, the game would crash and disappear from the other player's perspective.
Why is that? Help me
Size43 (Administrator) on 4 Feb 2023, 17:40:09
Please double-check that you're using the latest version of the extension. There have been some bugs in older versions where this could cause crashes.

Also, did you remove the gms_instance_sync call from the destroy event?
DinoWattz on 29 Dec 2022, 07:47:57
if it's a synced instance it should destroy itself automatically, it says that on here:https://gamemakerserver.com/en/docs/script/gms_instance_sync_destroy/
also here's a tip in case you don't know, you can make a standalone executable of the game so you can run two instances of it, then you login with different accounts and see if it's working properly (it's normal if one of the instances is delayed)
TheProhAslon on 23 Aug 2020, 23:16:06
___________________________________________
############################################################################################
ERROR in
action number 1
of Step Event0
for object GMS:

This client is syncing more than 1000 instances at the same time. There's a limit on the number of instances a client can sync. If you need more, you should destroy others.
at gml_Script_XServer_error (line 3) - show_error(argument0, false);
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_XServer_error (line 3)
called from - gml_Script_gms_step (line 171) - XServer_error(gms_action_get_argument_string(0));
called from - gml_Object_GMS_StepNormalEvent_1 (line 1) - gms_step();

I need to sync more than 1000 instances, because my procedural generation is giant. what do i do?
:(
Size43 (Administrator) on 29 Aug 2020, 11:37:30
Instance synchronization is not made for syncing entire procedurally generated worlds. You might be able to use a technique similar to what is used in the "Blocks" example you can find in the download.
Mert69 on 3 Sep 2017, 19:43:58
Hey!
Great tutorial! Yet, I still have some problems with the one time bullet sync. The bullets get synced and all, but on the other client the direction is opposite and they dont have an image_angle, please help me, thanks!

code:
var i=instance_create(lengthdir_x(x+5,point_direction(x,y,x,y)),y,oBullet)

with i
{
i.speed=26
i.direction=other.image_angle + random_range(-3,3)
i.image_angle=direction
i.damage=1

gms_instance_sync_var_add("damage",i.damage)
gms_instance_sync(i,is_onetime | isc_local) }
}
Size43 (Administrator) on 4 Sep 2017, 11:47:05
You'll need to sync the image angle manually, just like the damage.

I believe the speed can only handle a value between -10 and 10. You might need to set a smaller value.
Mert69 on 4 Sep 2017, 14:52:12
Thanks! It helped me a lot! Yet, I still have one problem; the player uses a seperate "oGun" instance to create the bullets, the gun is created in the create event of the player but when the second player joins, the gun of the second player follows the host. How do I solve that?
Size43 (Administrator) on 4 Sep 2017, 14:54:25
You can make each player be followed by just one gun like this:

// Create
my_gun = instance_create(x, y, oGun)
// Stop
with my_gun
{
    x = other.x
    y = other.y
}
Mert69 on 4 Sep 2017, 15:00:09
Thanks for the fast reply! I did this, but it isnt really what I want, I want the gun to follow the player and be visible on all the clients. Right now it is visible for both clients, but on the host's screen the other gun sticks to the host too, and on the other client's screen the host can shoot 2 bullets at the same time but he doesnt have 2 guns. Sorry if its unclear, I dont really know how to explain this.
Mert69 on 4 Sep 2017, 15:03:32
Isn't there any way to check what instance created the gun? So I can tell the gun to follow the player who created it.
Size43 (Administrator) on 4 Sep 2017, 16:05:06
You can get the owner (player id that created the instance) of a synced instance using gms_instance_get_owner(id).
Mert69 on 4 Sep 2017, 16:32:54
Hmmm when I use that, it just ignores the code.

///End step
var owner=gms_instance_get_owner(id)

if instance_exists(owner)
{
x=owner.x
y=owner.y+3.5
}
Size43 (Administrator) on 25 Sep 2017, 16:09:15
Apologies for the delay.

gms_instance_get_owner returns a player_id. You can't use that with instance_exists.

In this case, you can just use gms_instance_is_owner().
Mert69 on 4 Sep 2017, 16:33:26
So the gun doesn't stick to the player now, it just floats.
Mert69 on 6 Sep 2017, 16:37:24
Can someone please help?
Vitiik on 10 Sep 2016, 14:05:26
How to make matchmaking? I need help. I am making WW2 Platform shooter mp game and I need matchmaking.
Size43 (Administrator) on 16 Sep 2016, 09:24:19
You can use the VS mode for this. Please see the total shotgun / vs mode example in the download for an example.

You can also manually implement this using the [acticle=sessions]sessions[/article].
Vitiik on 26 Jul 2016, 14:10:40
Hello I have problem with

x = obj_player.x
y = obj_player.y
Vitiik on 1 Aug 2016, 12:50:05
When I run it. the Object with x = obj_... magnet to player and player has got two objects on same place.

Sorry for bad english
Size43 (Administrator) on 1 Aug 2016, 20:02:51
Check if the "owner" variable of the object equals gms_self_playerid(), and only update the position if that's the case.
Size43 (Administrator) on 26 Jul 2016, 17:39:40
Hey,

Please explain the problem in a little more detail. Are you seeing an error? What are you expecting to happen, and what is actually happening?
hansgamer on 5 Dec 2021, 22:08:37
I have a problem and it is that when I shoot the bullet it does not show on the other player's screen


If you want, here is the editable link so you can see the error:https://www.mediafire.com/file/c5vk7ht5uuhk77e/my++hero+academia+fangame.gmk/file
Size43 (Administrator) on 9 Dec 2021, 19:19:36
Let's cover some common issues before I take a look at the file.

Do you see any error message? If yes, could you share it?

Are you using instance sync? If yes, could you post the code that you're using to sync the instance?
NickPerson on 29 Oct 2021, 21:34:10
I have been having trouble setting things up, I would appreciate if I could get a hand with instances I should use in a situation if that is possible.
Ok so what I am trying to do is that my test game has butterflies moving around, and they move and all, my issue is that I am having trouble to get the same butterflies to appear to other players, and the butterflies does not have any object creator, they appear as the room loads and just serves as decoration for now.
I also do not have any pre sync code in the object as I removed the code once it didn't work.
So, is there a good way to handle this sort of object?
Size43 (Administrator) on 31 Oct 2021, 16:35:21
If it's just decoration I'd recommend not syncing anything at all. Syncing many moving objects constantly uses a lot of resources. Players won't have the same game open twice, so they likely won't notice the difference. It might even look a bit better, because if the instances are not synced you won't see any lag either.

If you do want to sync them, you'll have to use instance syncing.
AUGEgames on 5 Sep 2020, 01:34:25
Dear creator, I believe you made a mistake in tutorial and link towards the function:

gms_settings_declare_syncable_object is correct
and not gms_settings_declare_syncable_instance

As this was haunting me for a while and showed error. I hope I am correct and helped you ;)
Neat plugin with cool features. Just documentation can be messy at times. Cheers!
Size43 (Administrator) on 7 Sep 2020, 17:56:53
Thanks, fixed!
Helghast_01 on 28 Jun 2019, 10:49:37
what's the proper way of syncing variables such as hit points? i did have a method for this, but it starts being a pain to work with after a while, is there a way to do it fairly simple?
Size43 (Administrator) on 4 Jul 2019, 21:10:10
If they belong to a player -- just use gms_self_set and gms_other_get in the step event.
Helghast_01 on 21 Jun 2019, 13:49:19
hi,
so uh, im trying to implement bullet syncing to my game and it already is synchronizing the bullets and their variables, however due to how my collisions work i need to set a variable in the bullet object to an id of the object it's been fired by, so that it ignores his collision mask, i've come up with an idea that in order to be able to do that in mp i would have to -save the player_id of whoever fired the bullet and on user12 event look through all players and see whoever's got the player_id same as the bullet's instance_owner_id all of which is done in the user12 event, and for that im using the function gms_instance_get_owner(id) in the bullet instance to get the id of whoever fired the bullet and then i get his character's representation id on other clients' side, the problem is that for some reason the gms_instance_get_owner(id) function sometimes returns the proper value and sometimes returns just -1, whenever it returns -1 the character shoots himself because he can't set an ignored hitbox, help?
Helghast_01 on 22 Jun 2019, 01:47:57
Nevermind, I played around with it a little and turns out that changing instance_destroy() in the bullet object to a script that sets off a timer to make the bullet destroy itself with a delay while pretending to be already gone fixed the issue
vagrantwhale on 14 Mar 2019, 04:59:30
i'm using gms 1.4 and npcs don't sync to the non-master client. they spawn at the correct time, the npc health also updates correctly but they don't move at all on the non-owner...what am I missing?
Size43 (Administrator) on 25 Mar 2019, 22:53:35
Are you using full instance sync (is_full) to sync the instances?
vagrantwhale on 26 Mar 2019, 07:49:19
///
obj_enemy_spawn
///
Create Event:

execute code:

alarm[0] = room_speed * 3;

Alarm Event for alarm 0:

execute code:

alarm[0] = room_speed *3;
if(gms_self_ismaster())
{
if(instance_number(obj_enemy)< 5)
{
instance_create(x, y, obj_enemy);
}
}

///
obj_enemy
///
Create Event:

execute code:

gms_instance_sync(id,is_full);
alarm[0]=1;

Alarm Event for alarm 0:

execute code:

if(gms_instance_is_owner(id))
{
direction = choose(0,90, 180, 270)
speed=1
}
alarm[0] = room_speed;

Collision Event with object obj_bullet:

destroy the instance
Other Event: Game End:

execute code:

if(gms_instance_is_owner(id))
{
gms_instance_handover(id);
}

Other Event: Room End:

execute code:

if(gms_instance_is_owner(id))
{
gms_instance_handover(id);
}
Size43 (Administrator) on 1 Apr 2019, 17:07:38
Try setting gms_optimize_set_spc to something other than spc_supersmooth_interpolate. Does that do anything?
vagrantwhale on 1 Apr 2019, 18:46:01
On the start of the game they seem to sync, but after an enemy dies/is destroyed, he will re-spawn on both clients but only move on the master.

I tried with a few different scp:
spc_extrapolate, spc_autocombine, spc_none
Size43 (Administrator) on 2 Apr 2019, 19:24:51
How do you 'respawn' the enemies?
vagrantwhale on 2 Apr 2019, 19:53:43
I spawn them as shown in the tutorial with the enemy_spawner and alarms.

alarm[0] = room_speed *3;
if(gms_self_ismaster())
{
if(instance_number(obj_enemy)< 5)
{
instance_create(x, y, obj_enemy);
}
}
Size43 (Administrator) on 24 Apr 2019, 11:46:01
Apologies for the delay.

So the enemies that die, and the ones that respawn aren't related in any way?

Would you mind mailing me your project at gamemakerserver@outlook.com?
Ryaangu on 2 Aug 2018, 20:58:02
Hi,

I am getting some errors with my player rotation, when i press A the player walk left and the sprite rotate to left and same to right. But when the other player walk, the rotate don't work.

Here is the code:
if (move!=0) image_xscale = move;

Thanks!
Ryaangu on 4 Aug 2018, 02:37:40
I have already fixed this bug!
zuvizut on 14 Jul 2018, 01:39:48
I have been trying to sync the projectile and e_projectile objects I have created to the other game, but it doesn't even appear on the other game until the other game crashes with the error saying the other object is non-existent then the projectile appears and collides with the other object just fine.
Size43 (Administrator) on 17 Jul 2018, 16:54:24
What does your code look like?
YuBFan110 on 3 Mar 2018, 19:32:41
help i used this code in a shooter because the bullets wouldnt appear on the other persons screen but now they wont appear at all.
Size43 (Administrator) on 8 Mar 2018, 17:09:18
Do you get any errors?
Dan_The_Dragon on 10 Jan 2018, 22:42:02
not to be a grammar nazi but it seems you have a typo "is a variable is a variable" somewhere in up there
Its okay lol I'm not good at grammar either XD just thought I would let you know
Size43 (Administrator) on 11 Jan 2018, 18:41:29
Thanks, fixed!
romzelized on 5 Feb 2017, 17:01:35
hey, my game isnt syncing the objects, ive done this a couple of times, but it didnt work, can you help me?
Size43 (Administrator) on 16 Feb 2017, 15:57:44
Can you post the code you're using?
mpgame on 1 Dec 2016, 17:07:50
hey...
when I set the type of instance synchronization to "is_full", the intensive shaking is seen in movement of synched object. So that, my object moves very bad for other player... (I use the gravity, vspeed and changing x for movement). can you tell me a solution for this problem?
Size43 (Administrator) on 2 Dec 2016, 13:49:32
You can try using a different prediction method. See gms_optimize_set_spc
mpgame on 8 Nov 2016, 19:35:42
Hey again!
What is mean of "You must use the User Defined 12 event if you want to use the synchronized variables."???
For example how we can draw value of "damage" variable after adding this piece of code to the Event User 12 event:
damage =ds_map_find_value(variable_map, “damage”);
mpgame on 9 Nov 2016, 17:13:22
At the moment, I draw the "damage" value as usual, but it don't show new synced value for other player!
Size43 (Administrator) on 10 Nov 2016, 12:16:22
You can just draw the variable damage after adding the code in the User Def. 12 event.

Can you add a show_message(damage) in the User Def. 12 event to see if the value is being read correctly?
mpgame on 10 Nov 2016, 19:12:01
I added "show_message(damage)" in the User Def. 12... but show_message opened just in first time! After changing damage value, it didn't open and therefore, the value of damage don't synced in next times.

My Game Maker Studio version is 1.4.1598.
Size43 (Administrator) on 11 Nov 2016, 12:09:42
By default, variables are only synced on instance creation. If you want them to be continually synced, you'll need to use gms_instance_get and gms_instance_set.
conanphamtuan on 26 Oct 2016, 18:24:51
i think you should add flag that the sync instances in another client are the same variables with variables of the first instances
Size43 (Administrator) on 28 Oct 2016, 17:24:37
I'm sorry, I don't quite understand.
matthe815 on 1 Dec 2016, 21:50:01
Let me try to translate it to English. "I think you should add a flag that syncs another clients variables with the instance owner."
Size43 (Administrator) on 2 Dec 2016, 13:55:48
You could just use the variable syncing for this. See gms_other_get and gms_self_set.
gmfk07 on 14 Jul 2016, 20:27:46
Hey! First of all, I've just heard of GMserver and I'm looking forward to using it in future projects.

I've just been looking through the documentation, and after reading this tutorial, I have a quick question - is every instance given a client as its owner automatically by the server?
Size43 (Administrator) on 25 Jul 2016, 17:15:47
Apologies for the delay. I've been working a lot in the past few weeks, at some points up to 10 hours a day. That left very little time for me to do anything besides work.

Every instance is automatically owned by the client that created it, and will not change ownership until gms_instance_handover(_all) is called, or the player disconnects / changes session.
Bukmang on 19 Mar 2016, 19:17:10
Help me the instance doesn't get created in other client!

iD=instance_create(x, y, obAttacks);
iD.image_xscale=image_xscale;
iD.sprite_index=sprite_index;
iD.aType=aType;
iD.ddDamage=8.6;
iD.mask_index=sprite_index;

if aType=0 iD.ddDamage=8;
if aType=1 iD.ddDamage=7;
if aType=4 iD.ddDamage=10;
if aType=5 iD.ddDamage=12;
if aType=6 iD.ddDamage=9;

iD.image_speed=image_speed;
iD.hspeed=hspeed;
iD.Stick=Stick;
iD.image_index=0;

if Stick!=0 && skType[2]=1 {
iD.visible=1;
iD.sprite_index=spStickmanAttack_05;
iD.Stick=0;
}

if Stick!=0 && skType[1]=1 {
iD.visible=1;
iD.sprite_index=spStickmanAttack_04;
iD.Stick=0;
}

if serConnected=1 {
gms_instance_sync_var_add("aType", iD.aType);
gms_instance_sync_var_add("ddDamage", iD.ddDamage);
gms_instance_sync(iD, is_full);
}
Size43 (Administrator) on 26 Mar 2016, 20:54:24
Are both players in the same room?

Are both players logged in?

Is serConnected = 1 true?
Bukmang on 28 Mar 2016, 12:27:28
Yes, Yes and Yes, and I fixed, the sprite was empty so when I assisned iD.sprite_index=sprite_index it was empty so I had to do this gms_instance_sync_var_add("sprite_index", sprite_index); and it worked, and sometime the game lags or slow, why is that?
Size43 (Administrator) on 1 Apr 2016, 14:11:45
Depending on the distance you're away from the server (Amsterdam, The Netherlands) you might experience some lag. There's a discussion about that here:
http://gamemakerserver.com/en/forum/board/3/view/89/
amkgames on 15 Dec 2015, 19:43:41
I am having lronlem synchronizing allies, In my game allies follow player and I want allies to be synced to other clients and it does but ally starts following other player...
May you help me to solve this problem?
Thanks! :)
(I have one more problem but I will say that someday later.) lol
Size43 (Administrator) on 22 Dec 2015, 15:44:51
You might find gms_instance_is_owner useful :)

It sounds like the allies all start following the player object. You'll only want them to start following if the instance is owned by the current player (= if that function I linked returns true).
amkgames on 27 Dec 2015, 15:24:58
Also, how do I destroy other player's ally when other player logout?
Size43 (Administrator) on 28 Dec 2015, 22:11:41
Add a small script to check if a player_id is logged in:

//player_id_exists
for(var n = 0; n < gms_other_count(); n += 1)
{
    if gms_other_find(n) == argument0
        return true;
}
return false;


Now you can check if the player still exists, and if not destroy the follower using:

if !player_id_exists(gms_instance_get_owner(id))
{
    instance_destroy()
}
amkgames on 29 Dec 2015, 18:18:51
Where to put that code? It is not working in step event, I even tried putting it inside if gms_instance_is_owner and is not owner.
(It destroys when I put it outside of gms...owner when it creates and it does same in gms...owner but it never destroys in is not owner...)
Size43 (Administrator) on 30 Dec 2015, 15:49:43
The first code should be in a script named player_id_exists, the second part should be inside the if !gms_instance_is_owner(id) { ... } inside the ally-object.
amkgames on 1 Jan 2016, 11:11:02
That's what I have done but other player's ally never destroys and it starts following player.
I will try to fix it myself, it should work I add some variable s and make sure that it is other player's ally.
Size43 (Administrator) on 1 Jan 2016, 13:13:54
Wait, do you want the instance to be destroyed when the player logs out (which is what the code I gave you should do) or when the player goes to another room as well?
amkgames on 1 Jan 2016, 13:28:12
Both.
Btw, Happy New Year! :)
Size43 (Administrator) on 5 Jan 2016, 12:11:03
The code I gave will only work for when the player logs out -- you might have to just hide the ally when the player is in another room (the other player object will be at a position like (-10000,-10000) when the player is in another room).
amkgames on 6 Jan 2016, 07:36:20
What if I make a parent object of other player and set it parent of other player and othe ally(new object)?
Size43 (Administrator) on 11 Jan 2016, 12:31:46
I don't think this will work.
amkgames on 5 Jan 2016, 12:55:51
Something like this?:
if obj_other_player.x = -10000 and obj_other_player.y = -10000
{
//hide code
}
Size43 (Administrator) on 11 Jan 2016, 12:31:36
Just checked, you can just check if visible is set to true. Should be easier :)
amkgames on 11 Jan 2016, 13:53:04
You mean I have to use the above code and hide code should be: visible = true or false?
Size43 (Administrator) on 17 Jan 2016, 21:20:17
You can replace the check for x and y with an if other_player.visible = true.
amkgames on 18 Jan 2016, 06:02:32
Thanks, but do other players get only unvisible if went to another room?
Do they still sync to previous room?
Size43 (Administrator) on 18 Jan 2016, 17:19:50
Unless you're manually setting them to be invisible, they will only be invisible when they're in a different room.
amkgames on 11 Feb 2016, 10:16:06
Also, this error is being hateful >_<:
___________________________________________
ERROR in
action number 1
of Begin Step Event
for object __newobject79:
Retrieving owner for non-existing instance 108671.000000
amkgames on 11 Feb 2016, 10:18:15
Do my instance not exists?
Size43 (Administrator) on 16 Feb 2016, 17:54:12
It seems like you've got some instances that aren't synced properly. Maybe you're missing a gms_instance_sync somewhere?
amkgames on 16 Feb 2016, 19:15:19
I am using gms_instance_sync in create event of object_player before instance_create of obj_ally and that sync is is_full.
When I switch ally, following ally object is changed to fighting ally object and when I change fighting ally to following ally, this error occurs.
Does that mean I need to sync it again? (Of course I think. :P)
Size43 (Administrator) on 16 Feb 2016, 20:55:02
Yeah that might be the problem. Let me know if syncing it again fixes it.
amkgames on 11 Feb 2016, 09:57:20
Invisible thing is working but logout thing is still not working. :/
Size43 (Administrator) on 16 Feb 2016, 17:54:39
What's the logout thing?
amkgames on 16 Feb 2016, 19:17:37
Btw, long time no see. :P
Size43 (Administrator) on 16 Feb 2016, 20:51:23
Yeah, I've been sick for a few weeks.
amkgames on 17 Feb 2016, 05:39:37
Oh, if you are still sick, I hope you get well soon. :)
Size43 (Administrator) on 21 Feb 2016, 21:23:53
Thanks! Don't worry, it's nothing serious. :)
amkgames on 16 Feb 2016, 19:08:05
The code you gave to destroy ally when player logout.
Size43 (Administrator) on 16 Feb 2016, 20:53:15
Try setting the script and destroying the instance manually using gms_script_set_logout.
amkgames on 17 Feb 2016, 05:44:08
I will try it, looks cool! :)
Thanks!
amkgames on 18 Jan 2016, 17:27:12
Oh, Thanks! :D (I had spell mistake. :P invisible*)
amkgames on 26 Dec 2015, 13:26:45
I can't understand how to use it... Is it used something like this?:
var owner_id;
owner_id = gms_instance_get_owner(oPlayer.id);
if gms_instance_is_owner(owner_id)
{
follow(owner_id); //Example.
}
amkgames on 27 Dec 2015, 14:32:49
But I want to sync sprite index of that ally...
I've tried gms_instance_set and get... but it didn't worked.
I am using it something like this:
//Step Event of Ally object
if !gms_instance_is_owner(id)
{
sprite_index = gms_instance_get(id,"sprite_index");
}
//Then sprite changing code with movement code
//and then this:
if gms_instance_is_owner(id)
{
gms_instance_set(id,"sprite_index",sprite_index);
}
amkgames on 27 Dec 2015, 14:46:29
This the error I get when other client connects:
___________________________________________
ERROR in
action number 1
of Draw Event
for object follow_ally:
Trying to draw non-existing sprite.
Size43 (Administrator) on 28 Dec 2015, 22:08:43
gms_instance_get might return 0 if the variable is still being sent over, and not received by the other player. Add a if(sprite_exists(...)) before setting sprite_index, and it should work.
amkgames on 29 Dec 2015, 18:19:23
Thanks! :)
amkgames on 27 Dec 2015, 14:27:13
I have changed the code and it is working. :P
if gms_instance_is_owner(id)
{
follow(obj_player);
}
amkgames on 26 Dec 2015, 13:27:40
Ally's Step Event. ^
amkgames on 26 Dec 2015, 11:19:12
Sorry for the late reply, I found this mail in spam...
amkgames on 26 Dec 2015, 11:07:01
Sounds interesting, Thanks! :)
(I forgot the second problem xD, I will say when I will remember.)
amkgames on 15 Dec 2015, 19:44:02
Problem*
Aaron13ps on 28 Nov 2015, 06:05:30
Dear Size,

Is there a way to make an instance sync only locally? I am trying to find a way to create an attack that is synced only if the player is connected to a session (to prevent lag in the lobby). But I would still like the player to be able to fire his/her weapon in the main lobby but only client side in order to prevent lag. Is there any way around this without creating an entirely different object strictly for client side? (Some of the data is loaded in user_event_12)
Size43 (Administrator) on 7 Dec 2015, 11:43:17
It might be easiest to just call user event 12 manually if you don't want to sync it. I don't think gms_instance_get will return an error when the instance does not exist, but I'm not 100% sure about that so that may require some more checks.
WWW_cool on 6 Sep 2015, 11:21:07
Dear Size,

Are all build-in vars of GMS auto sync? (for example - alarms)

- Artem
Size43 (Administrator) on 6 Sep 2015, 20:57:12
Alarms are not synced. An alarm does not have a direct effect on the object, so I decided not to sync them automatically. If it changes the sprite, the sprite will be synced. If it changes position, the position will be synced and so on. You get the idea.
JamSnack on 1 Jan 2018, 06:56:01
My instance's positions are not being syncd properly. Could it be because I am using mp_potential_step?
Size43 (Administrator) on 3 Jan 2018, 11:51:59
captain_davy on 1 Jul 2015, 14:50:05
Dear Size,

I am a big fan of this tutorial and all your great work, but I am having a little trouble with it. I have chosen to make my own master/owner system using an avarage of gms_info_ping() to determine the player with the lowest latency and assigning him the role as master, to ensure that the player with the best connection is always the one syncing the instances in a local area. This has been working great so far in choosing the initial owner of a synced instance, instead of the randomly assigned gms_master_player. However, when my system finds a new master with a better connection, handing over the instance to that player has proven to be a bid of a challenge. It seems as if the only way to do this is through the custom gms_instance_handover(id) or gms_instance_handover_all(), which leaves me no choice as to who the instance should be handed over to. I hope that there is a way around this, and that you could find the time to help me out.

- Davy
Size43 (Administrator) on 2 Jul 2015, 19:47:43
There's no way to specify which player will get the instance after it's handed over. I like the idea of giving the instance to the player with the lowest ping, so I'll add it to the logic for switching instance owners. I'm not planning on adding a manual selection right now, but it may be added in the future.
SlamminSam on 4 Jun 2015, 02:22:01
Great tutorial :)
Size43 (Administrator) on 5 Jun 2015, 16:31:51
Thanks!
simi on 1 Apr 2015, 13:31:31
i try full synchroized but i get this error any help ? thanks...
___________________________________________
############################################################################################
ERROR in
Retrieving owner for non-existing instance 100005.000000
at gml_Script_XSe
action number 1
of Step Event0
for object GMS:
rver_error (line 3) - show_error(argument0, false);
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_XServer_error (line 3)
called from - gml_Script_gms_step (line 167) - XServer_error(gms_action_get_argument_string(0));
called from - gml_Object_GMS_StepNormalEvent_1 (line 1) - gms_step();
simi on 1 Apr 2015, 15:23:23
this error show up when the obj_npc is create on the obj_npc_spawner
Size43 (Administrator) on 3 Apr 2015, 11:46:38
Would you be able to mail me your project (or something that generates the same error) to gamemakerserver@outlook.com so I can take a look at it?
Miksamoo on 3 Oct 2014, 13:43:48
I have tried to do bullet. When it collides with player, it checks owner. But that gets error:

############################################################################################
ERROR in
action number 1
of Step Event0
for object GMS:

Retrieving owner for non-existing instance 100004.000000
at gml_Script_XServer_error (line 3) - show_error(argument0, false);
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_XServer_error (line 3)
called from - gml_Script_gms_step (line 159) - XServer_error(gms_action_get_argument_string(0));
called from - gml_Object_GMS_StepNormalEvent_1 (line 1) - gms_step()

I use this code on collision event with player:
if !gms_instance_is_owner(id) {
other.hp -= dmg
instance_destroy()
}
Size43 (Administrator) on 3 Oct 2014, 18:44:03
Apologies about that. That function actually only works for instances that are synced using is_full (as they can change owner). For the is_onetime and is_extended, a variable named owner is set for every instance. This variable will contain the player_id of the owner of the instance. This code does the same as the gms_instance_is_owner for fully synced instances:

if owner == gms_self_playerid()
{
    ...
}
Orbacal on 17 Aug 2014, 22:04:06
Hello. I have to use full type sync in my game to make some compex mechanics work (like charging, deflecting bullets), But that seem to occassionally freeze and crash the game for me and others. Is it possible to prevent these extreme lags?
Size43 (Administrator) on 19 Aug 2014, 20:20:51
That should not happen. If there's any way to reliably reproduce this, please let me know how. Other than that, if the game crashes (...not responding) please send me a dump file (task manager -> Right click on process -> Create dump file).