gms_instance_sync

Usage: gms_instance_sync ( id, flags, "var1", "var2"... )

Description

Syncs the given instance. id is the instance_id (built-in variable: id).

The second argument is used to specify how the instance should be synced.

is_onetime: Instance will be synced once. Use this for instances with a predictable movement and a predictable destroy, for example: bullets.

is_extended: Instance will be synced at create and at destroy. Use this for instances with an unpredictable destroy, but a predictable movement. For example: remote-detonating bombs.

is_full: The instance will be fully synced. Use this for instances with an unpredictable movement and an unpredictable destroy. For example: AI controlled NPCs.

You can add the flag 'isc_local', to only sync the instance when other players can see it. (Syntax: 'is_extended | isc_local')

The given variables "var1", "var2", etc and the x, y, speed and direction will be sent.

Example

//For the sake of this example, assume the bullet has a variable damage, and it should be synced as well.
i = instance_create(x, y, obj_bullet)
i.damage = 50;
gms_instance_sync(i, is_extended, "damage")

Replies (12)

Last message on 24 Apr 2017

Dublann on 29 Mar 2017, 21:48:59
Hi size,

Im having the gms_instance_sync(id, is_full) in the create event of enemies and some other objects, but when playing, they appear duplicated. Why is it happening?

Also, when 2 players are connected, one cant see the other but are having collisions. Also this error appears. How can I solve it?:

FATAL ERROR in
action number 1
of  Step Event0
for object GMS:
instance_exists argument 1 incorrect type (5) expecting a Number (YYGI32)
 at gml_Script_gms_step (line 256) -                     if instance_exists(__i)
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_gms_step (line 256)
called from - gml_Object_GMS_StepNormalEvent_1 (line 5) - gms_step()


THX!
Size43 (Administrator) on 16 Apr 2017, 16:20:48
Are you maybe creating & syncing the instance on each client? You should only have one client create the instance, otherwise each client will sync an additional instance.
Dublann on 23 Apr 2017, 13:40:05
Thx for the comment. The problem Im having is that the enemies are placed directly in the room, therefore I dont know how to manage it. I tried using in the create event:
if gms_self_ismaster()
{
if gms_instance_is_owner(id)
    {
    gms_instance_sync(id,is_full)
    randomize()
    if room=rm_play
    {
    path_start(path_obj_minero,choose(4+0.5,4+1,4-1),path_action_restart,1)
    }
    if room=room_level4
    {
    path_start(path_obj_minero_lvl4,choose(4+0.5,4+1,4-1),path_action_restart,1)
    }
    //Linterna
    instance_create(x,y,obj_linterna)
    }
}
else{instance_destroy()}


But it doesnt work
Size43 (Administrator) on 24 Apr 2017, 11:49:13
Remove the if gms_instance_is_owner(id) check. Instances won't have an owner until they have been synced.
Dublann on 24 Apr 2017, 19:44:10
Done, but it isnt show in the game of the other player. What should I add in step event/draw event? Im having:

Step event
if gms_self_ismaster()
{
    image_speed=1
     scr_buscar_goblin()
    if distance_to_object(obj_player)<=1
    {
    audio_play_sound(sound_goblin, 1, false);
    room_goto(room_main_centro)
    }
    if distance_to_object(obj_murcielago)<=1
    {
    x=xprevious-1
    y=yprevious-1
    }
    if( x < 0 ) x += room_width;
    if( y < 0 ) y += room_height;
    if( x > room_width ) x -= room_width;
    if( y > room_height ) y -= room_height;   
}


Draw event
if gms_self_ismaster()
{
    if distance_to_object(obj_player)<=128 or distance_to_object(obj_other_player)<=128
    {
    draw_set_font(font0)
    draw_set_color(c_red)
    draw_set_halign(fa_center)
    draw_text_outline(x,y-64,"!",2,c_white,16)
    immersion_play_effect(74);
    }
    scr_sombra()
}

I also tried changing the if gms_self_ismaster() by if gms_instance_id but nothing.
Dublann on 1 Apr 2017, 19:41:24
Hi again!

I solved the problem of not seeing the other_player object, but now, due to obj_player and obj_other_player are solids and they are created in the same position, the are colliding with each other and I cant move. I tried to modify the obj_other_player ystart, but it is not affecting. How could I solve it?

Also, I have noticed that the objects with gms_instance_sync(id, is_full) are duplicated, triplicated, etc. And their movements are not syncornized, they move independently and each player see different movements of them. How could I solve it?

Thx!!!
Dublann on 1 Apr 2017, 20:11:53
OK, I removed the solid option of the obj_other_player and now I can move them (I suppose I dont need it).

But still I am having the problems with the instances and the error that appears (see 2 messages above).

PS: is there any way to edit a post? I dont like to spam the forum :(
littlecoolpug on 17 May 2015, 01:45:21
OK so I'm trying to make a zombie game, and the zombies don't sync, (so the zombies spawn on both peoples screen but the zombies are only visible on that persons screen) I need to make it when the zombie spawns in the game it's shown on both screens so would I do this in the zombie's create event:
gms_instance_sync(self)
or what please help me thanks in advance!
littlecoolpug on 17 May 2015, 01:52:26
sorry would it be like this, not like I said before:
gms_instance_sync(self, is_full, "zombielife")
littlecoolpug on 17 May 2015, 16:22:10
hello? reply......
Size43 (Administrator) on 17 May 2015, 19:09:07
I don't get paid to do this, so I'm not available 24/7. :)

Use something like this:
gms_instance_sync_var_add("zombielife", zombielife)
gms_instance_sync(id, is_full)


self is just a value like -1, id is the actual id of the instance. Specifying variables as parameters is only supported in GM8, in GM:Studio you'll need to call gms_instance_sync_var_add before the gms_instance_sync call.
littlecoolpug on 17 May 2015, 19:37:08
oh ok, and as long as it's free I'm fine with you not being available 24/7 :) sorry about that!