GM2.3 Function passing variable instead of value

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



Firehammer Games
That's exactly how it 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.


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:

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


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).
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?


Toolmaker of Bucuresti
Forum Staff
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...
function CountdownTimer(_time) constructor {
    time = _time;
    isTimeOver = false;

    static countdown = function() {
        if (isTimeOver)

        if (time <= 0) }
            time = 0;
            isTimeOver = true;
Then, you could create a countdown timer in object Create event:
levelTimer = new CountdownTimer(30 * 60);
And then, in Step Event keep counting down:
if (levelTimer.isTimeOver())
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