gms_global_get & gms_global_set

Posted by PainiteOfficial
Hey Size! I promise this will be my last post for a while :)

I just want to know a bit more about how the global functions work with gm server.

In a singleplayer game I can use this code to change my "global.slowmotion" variable:
if (keyboard_check(vk_space)) {
global.slowmotion = 0.5;
} else global.slowmotion = 1

That's very easy right? Well I'm not so sure about multiplayer.
I know that I have to right in the step event of the oPlayer and oOtherPlayer this:
gms_global_get("global.slowmotion")
and that I have to add to my existing code:
if (keyboard_check(vk_space)) {
global.slowmotion = 0.5;
gms_global_set("global.slowmotion", 0.5)
} else {
global.slowmotion = 1
gms_global_set("global.slowmotion", 1)
}

But the variable acts extremally weird. I usually change the code, trying to fix it and every time (including this method) I get different results: values not updating, global.slowmotion returns 0 and so on.
Can you please explain what I'm doing wrong?



Also thanks so much for helping millions of people over the past years <3

Replies (1)

Last message on 6 Oct 2020

Size43 (Administrator) on 6 Oct 2020, 22:20:48
Not a problem at all!

You still need to assign the value of gms_global_get to your global variable:

global.slowmotion = gms_global_get("global.slowmotion")


(note that you do not have to start the string you pass to gms_global_get with global., you can choose a completely different name if you want)

Have you thought about what you want to happen when multiple people trigger the slow motion at the same time? I'm guessing there will be some timing issues if anyone can just trigger it at any point.

What you might want to do is to keep track of each individual that is triggering slowmotion separately rather than using a shared global variable:

var my_slowmotion = 1 - keyboard_check(vk_space) / 2
gms_self_set("slowmotion", my_slowmotion)
global.slowmotion = 1
for(var n = 0; n < gms_other_count(); n += 1)
{
    var player_id = gms_other_find(n);
    var s = gms_other_get_real(player_id, "name");
    global.slowmotion = min(global.slowmotion, s)
}