Question regarding functions

Xer0botXer0

Senpai
Hi!

So GMS 1.4 didn't have functions or rather had blank pages you pop into an event and write code in, then that runs from top to bottom.


In GMS 2.# there's no longer these pages.. rather I'll add 1 step event and only get 1 page to write code in, so I assume this is were functions come into play ?

So far in GMS 2.# I've been writing without functions, but now if I add a function does the function run without having to call it ?

I haven't tested this as I've been writing 2 hours worth of code that I haven't tested yet so the built wont be able to run.
 
You usually write functions in scripts, and you can have multiple of them in one script. Then you can call the function by going like
MyFunc();
You can also pass in arguments, but at this point I would look up how to write functions In gms2 because you can do a lot with them. And no, they don’t execute if you write them in the step event only if you CALL them in the step event.
 

NightFrost

Member
Functions are quite like how scripts used to be. You write and call them in the same manner. However there are a few differences.

Functions can take argument variables that are named. For example:
GML:
function Sum(number1, number2){
    return number1 + number2;
}
As you can see they work the same way as the old argumentX variables.

You can store more than one function in a script resource, whereas you could only put one script into a script resource. This is just a convenience, it changes nothing about how they are used.

You can even store functions in objects, though this restricts their direct call only to instances of that object; if others need to access them it must be done through object notation. Which is why you want to put general-access functions into script resource.

Just like scripts, functions can access variables of their caller's scope. That is, a function called from an instance can access that instance's variables (such as position, sprite, etc).
 

Pixel-Team

Master of Pixel-Fu
You can also declare 1 or more functions in your object's events, (commonly in the create event.) When declaring functions in an object's event, a function is written slightly different from the functions you write in the scripts.

Here you make a variable, and make it into a function:

GML:
//CREATE EVENT
my_func = function () {
    //do stuff
    show_debug_message("I did stuff!");
}
You can make another object cause this object to invoke its functions like this:

Code:
with (thatInstance) {
    my_func();
}

//OR LIKE THIS
thatInstance.my_func();
 

TsukaYuriko

☄️
Forum Staff
Moderator
To provide some alternative options...
Hi!

So GMS 1.4 didn't have functions or rather had blank pages you pop into an event and write code in, then that runs from top to bottom.
By "pages", do you mean the "Execute Code" DnD? If so, this still exists, same as before, if your event is a DnD event. If it is a GML event, you indeed only get a single code window. Or, in other words, you no longer have to manually drag an Execute Code into every event like you had to do in earlier versions if you exclusively want to work with GML.

If you want this to look the same it did in previous versions, convert your event to a DnD event (RMB on an event -> Convert to DnD) and you will be able to drag Execute Code DnDs into the event again, same as in older versions. I personally wouldn't do this as I find keyboard-friendly interfaces easier to use, but the option is there if it suits your preferences.


In GMS 2.# there's no longer these pages.. rather I'll add 1 step event and only get 1 page to write code in, so I assume this is were functions come into play ?
If you just want to organize your code into logical sections, you can use the region syntax:
GML:
#region Initializing stats

hp = 10;
atk = 2;

#endregion

#region Initializing sprites

spr = spr_player;
spr_weapon = spr_sword;

#endregion
Alternatively, you can use curly braces more liberally than you might realize for the sake of organization:
GML:
// Stats
{
    hp = 100;
    atk = 10;
}

// Sprites
{
    spr = spr_player;
    spr_weapon = spr_sword;
}
This will allow code folding accordingly, as that is entirely based on curly brace and region placement.
 
Last edited:
Top