• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GM2.3 Function passing variable instead of value

G

Gamemaker newbie

Guest
Dear all,

I wrote the following countdown function which passes a time and a variable indicating the time is over or not.
But this is not working. Only the values are being passed. There is no update in the variable.

function countdown(time,isTimeOver){
if (time>0){
do { time--; }
until (time<=0)

time = 0;

} else {time=0;}

isTimeOver = true
}

Thanks!
 

kburkhart84

Firehammer Games
That's exactly how it works....in general variables don't get passed by reference, rather by value. Then a function can return a value which you can store in a variable if you need it. Knowing this fact, I recommend you re-work what you are doing in a different way.

Just FYI, not that I recommend you mess with it right now(you seem pretty new and this may be over your head)...but you CAN pass arrays into functions by reference. There is an additional bit of syntax for that. There is also the idea of using data structures and just passing the index into the function. But please, you should figure out a different way to handle this before getting into those more advanced concepts.
 

Binsk

Member
As kburkhart84 stated, variables get passed by value not by reference in GameMaker (aka., a copy of the variable gets put into the function so you are only modifying a copy, not the original variable). You can get by this with arrays (as these can be passed by reference). However, the simpler solution is just to return the new value and set the variable to it. For example:

Code:
function countdown(time, isTimeOver){
   // The code and stuff here
   isTimeOver = true
   return time;
}
Then when you call it, do something like this:
Code:
time = countdown(time, isTimeOver);
 

kburkhart84

Firehammer Games
The reason I didn't directly mention simply using the return value is because they are trying to modify two variables at once and you can't return two values(except as in arrays or data structures, or the new Structs in 2.3).
 
G

Gamemaker newbie

Guest
Thanks guys! I want to use return values too but it does not work in my way. I want to keep track of the time, for I want to display the count down on screen. But it does not return the time value together with the true value for isTimeOver variable. Is arrays and/or data structures the only way out?
 

Alice

Darts addict
Forum Staff
Moderator
How about making a CountdownTimer constructor instead, and add a static method for counting down?
That way you can keep the timers self-contained, instead of managing multiple time/isTimeOver variables from within the same instance.
It might feel a little advanced, but in the long run results in cleaner and easier to manage code.

You could add a Script resource for CountdownTimer...
GML:
function CountdownTimer(_time) constructor {
    time = _time;
    isTimeOver = false;

    static countdown = function() {
        if (isTimeOver)
            return;

        time--;
        if (time <= 0) }
            time = 0;
            isTimeOver = true;
        }
    }
}
Then, you could create a countdown timer in object Create event:
GML:
levelTimer = new CountdownTimer(30 * 60);
And then, in Step Event keep counting down:
GML:
levelTimer.countdown();
if (levelTimer.isTimeOver())
    restart_level();
NOTE: I didn't use do ... until, because it will keep counting down without letting any other code execute, so the countdown would be depleted immediately.
See also: this tutorial
 
Top