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. If you're used to the old way or got code from an old tutorial, this is a habit that you must change going forward.

Pre-2.3 script syntax has only one subroutine per script, and parameters are accepted through argumentN (denoting a set number of parameters) or argument[N] (denoting a variable number of parameters).
GML:
///@func d6()
return irandom_range(1, 6);
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);
Post-2.3 script syntax can have any number of subroutines per script, delimited by function NAME(...) { ... } blocks. Parameters are accepted between the brackets (denoting up to a set number of parameters) or inside the braces using argument[N] as before. In particular, script code outside of those blocks are run anonymously at the beginning of the game.
GML:
///@func d6()
function d6() {
    return irandom_range(1, 6);
}
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);
}
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).

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
 

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.
 

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
 

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