GMS 2.3+ IMPORTANT!!! GM 2.3 Users Please Read! Improper script usage generates undefined variable errors

FrostyCat

Member
Symptoms to Look For

Expand for screenshot:
1602342146679.png
  1. The error is in the Create Event for object <undefined>.
  2. The subject is <unknown object> with a variable name that's either built-in or known to be already declared.
  3. The stack trace shows the source as something starting with gml_GlobalScript.

For Question Askers

This error happens if you use the pre-2.3 script syntax in GMS 2.3 or higher. Going forward, you must make a habit of using the new script function syntax shown below.
GML:
function MY_SCRIPT_NAME(???) {
    ...
}
??? is the comma-separated list of required argument variable names (e.g. _x, _y), which will be automatically captured as local variables within the function. You can use this part to replace assignments creating local variables based on argument0...argument15 variables, if any. It can be left blank if it takes no arguments or an arbitrary number of arguments. DO NOT continue to use argument0...argument15.

... is where the body of the script function belongs. Be sure to replace all references to argument0...argument15 with the corresponding variable names you chose for ???.

For functions taking an arbitrary number of arguments, the argument array and the argument_count variable are still available as before.

If you got the code from a tutorial, remember that many tutorials have not been updated to current standards, especially those on YouTube. If you see a script written the old way in a pre-2.3 tutorial, manually update them in your work as shown above. Avoid tutorials that are meant for a different major version than the one you're using (e.g. GMS 1.x tutorials when you are using GMS 2.3).

Examples of Converted Script Functions

-Pre-2.3 SyntaxPost-2.3 Syntax
No arguments
GML:
///@func d6()
return irandom_range(1, 6);
GML:
///@func d6()
function d6() {
    return irandom_range(1, 6);
}
Fixed number of arguments
GML:
///@func instance_place_lengthdir(len, dir, obj)
///@param len
///@param dir
///@param obj
var _len = argument0;
var _dir = argument1;
var _obj = argument2;
return instance_place(x+lengthdir_x(_len, _dir), y+lengthdir_y(_len, _dir), _obj);
GML:
///@func instance_place_lengthdir(len, dir, obj)
///@param len
///@param dir
///@param obj
function instance_place_lengthdir(_len, _dir, _obj) {
    return instance_place(x+lengthdir_x(_len, _dir), y+lengthdir_y(_len, _dir), _obj);
}
Variable number of arguments
GML:
///@func sum(...)
var s = 0;
for (var i = 0; i < argument_count; ++i) {
    s += argument[i];
}
return s;
GML:
///@func sum(...)
function sum() {
    var s = 0;
    for (var i = 0; i < argument_count; ++i) {
        s += argument[i];
    }
    return s;
}

For Responders

If you see this kind of error, DO NOT respond by telling them to declare the variables in question. Teach them the new script function syntax and refer them to this helpdesk article.

Related Links

Updated 2021-05-10: Reformatted to use a side-by-side comparison table.
 
Last edited:

Slyddar

Member
Even though this post is a few months old, and while I totally applaud Frosty's effort to help new users deal with this error, I feel the examples and the language used is so high level it may isolate individuals seeking assistance. If the content and examples could be simplified it would be more beneficial, as on first viewing it definitely comes across as confusing. This might be why it didn't gain traction. Just my opinion though, and as I say, really appreciate you trying to assist considering the amount of traffic these queries generate.

EDIT : The original post was edited in May 21, and has been made much simpler to understand. If you use DnD though, I've posted an example below to help.
 
Last edited:

EvanSki

Raccoon Jam Host
If the content and examples could be simplified it would be more beneficial,
*ahem*

GMS 2.3 introduced a change to how scripts work!

Scripts now act like containers! And every script gets ran at the start of the game!

So old scripts that did code when you called the script name ex( scr_myfirstscriptbois) will now run at the start of the game, giving errors!!! :eek::eek::eek::eek:

Never fear for Functions take the place of the old scripts!! :cool::cool::cool::cool:👌👌👌

Simply take all your old terrible copy-pasted code and slam that bad boy into a function sandwhich!!
GML:
function scr_myfirstscriptbois(){

var _a = 1;
var _b = 1;

var _c = (_a + _b);

return(_c)
}
OH MY GOD!! :p:p:p:p

so now at the start of the game!, The game will create a global pointer to this script! so anytime you call scr_myfirstscriptbois!
it will refer to what was defined in the function { } and run that code! AMAZING!!!

so in short, script assets in the resource tree are now containers for functions or static variables you wish to define.
functions are called like scripts where in the past.

Thank you for coming to my evanski-talk
 
Last edited:

SSJCoder

Member
So what you are saying is... I can shout the function name and it fixes everything?
if it doesn't work try shouting at your computer and banging on it (preferably with Thor's hammer) .. eventually you will see something, then you will know you cracked the code .

/sarcasm
 
Top