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 (34)

Last message on 17 Jun 2023

bruno_chachi on 24 Dec 2017, 18:57:49
is_full works on game maker 8.0?
Size43 (Administrator) on 24 Dec 2017, 19:45:20
Yes, it works.
bruno_chachi on 25 Dec 2017, 17:12:32
I have an error only whit is_full

___________________________________________
ERROR in
action number 1
of Begin Step Event
for object __newobject42:

In extension function gms_step:
Error in code at line 641:
_distance = point_distance(__iself_last_x, __iself_last_y, x, y)
^
at position 57: Unknown variable __iself_last_x
Size43 (Administrator) on 26 Dec 2017, 12:18:32
What does your syncing code look like?
bruno_chachi on 29 Dec 2017, 17:51:37
This in text file

// block
CONTROL=object_add();
object_event_add(CONTROL,ev_create,0,'
window_set_cursor(cr_cross);
');
object_event_add(CONTROL,ev_step,0,'
if(mouse_check_button_pressed(mb_left)){
var i;
i=instance_create(mouse_x-16,mouse_y-16,obj_piso);
gms_instance_sync(i,is_full);
}
if(keyboard_check_pressed(ord("X"))){
window_set_cursor(cr_default);
instance_destroy();
}
');
object_event_add(CONTROL,ev_draw,0,'
draw_set_color(c_black);
draw_set_halign(fa_left);
draw_set_valign(fa_top);
draw_text(view_xview,view_yview,"Haz click para crear bloques#Pulsa x para terminar");
');
instance_create(0,0,CONTROL);
bruno_chachi on 29 Dec 2017, 17:52:05
And the game executes the file
Size43 (Administrator) on 3 Jan 2018, 11:41:19
Are you also adding obj_piso using object_add?

The variabes __iself_last_x, __iself_last_y are set when you call gms_instance_sync, so I'm not quite sure how you're even seeing that error.
bruno_chachi on 5 Jan 2018, 17:56:58
obj_piso is in the game
Size43 (Administrator) on 7 Jan 2018, 15:51:07
When exactly do you get the error? Directly after creating the instance, or only after a while?

Are you using instance_deactivate anywhere?
bruno_chachi on 10 Jan 2018, 17:47:34
I don´t use instance_desactive.

The obj_piso creates, and then show the error
Size43 (Administrator) on 11 Jan 2018, 18:40:31
Would you mind mailing me your project (or a similar file that I can use to reproduce the error) at gamemakerserver@outlook.com?
bruno_chachi on 13 Jan 2018, 11:00:46
Okay :)
Reycko on 18 May 2023, 19:37:00
Hello, I'm trying to add syncing for particles on my game, when I try to load it though, the game crashes with an error saying that the id is a string, my code:

image_speed = 0.35

if !check_online() // shortcut of gms_info_isloggedin()
exit;


if !check_bysync() // shortcut of gms_instance_created_by_sync()
gms_instance_sync(id, is_onetime | isc_local);
Size43 (Administrator) on 17 Jun 2023, 17:10:10
Apologies for the delay. What is the exact error message you're seeing?

Did you by any chance add a constant, function, object, with the name "id"?
TheRetroBoy on 28 Nov 2019, 00:24:51
Help! When I sync an instance with is_onetime it doesn't show up for other players who join, but if I do is_full, it crashes! I don't know why!

Crash Code:


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

SERVER ERROR: Maximum number of full instance sync-instances reached.

Please note: This is a server-sided error message. To disable all GameMaker Server-errors, call gms_debug_hide_errors().
at gml_Script_XServer_step (line 408) - show_error("SERVER ERROR: " + XServer_readstring(_rr), false);
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_XServer_step (line 408)
called from - gml_Script_gms_step (line 59) - __ret = XServer_step(__current_room);
called from - gml_Object_GMS_S
Size43 (Administrator) on 30 Nov 2019, 17:27:23
Try adding a show_message call below the gms_instance_sync call. How many times does it show up? Is that about the amount of instances you were expecting to sync?
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.
Size43 (Administrator) on 2 May 2017, 13:24:48
Are you drawing anything at all if gms_self_ismaster() returns false?
Dublann on 2 May 2017, 15:14:31
Nope.

Should I remove the gms_self_ismaster? or add gms_instance_is_owner(id) in the step and draw event?
Size43 (Administrator) on 8 May 2017, 15:22:07
Well if you're not drawing anything when gms_self_ismaster returns false, you won't see the object at all. You should draw something when that's the case as well if you want to see it.
Dublann on 27 May 2017, 10:40:27
Hey Size, could you see the email I sent you?
Size43 (Administrator) on 27 May 2017, 16:20:52
Sent you a mail back :)
Dublann on 7 Jun 2017, 11:49:32
Another mail sent with a small example :)
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!