IMPORTANT!!! GM 2022+ and 2.3+ users please read! Obsolete script syntax causes undefined variable errors

FrostyCat

Redemption Seeker
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

You are getting this error because you are using obsolete script syntax, which appear in older tutorials and reference material for GMS 2.2 or below published before 2020. If you use GML Code, you must wrap your script body using the new function keyword.
GML:
function your_script_name(/* Script arguments here */) {
    /* Script body here */
}
If you use GML Visual, please see Slyddar's instructions for using the Define a New Function action. The rest of this article deals with GML Code.

Script Functions without Arguments

Leave the script arguments part blank, and paste the script body between the braces.

Example: Write a function d6() that returns the result of a 6-sided die roll.

BeforeAfter
GML:
return irandom_range(1, 6);
GML:
function d6() {
    return irandom_range(1, 6);
}

Script Functions with Arguments

If your script function takes a fixed number of arguments, name and list them between the brackets (usual variable naming rules apply), and replace their occurrences in the script body. You should no longer refer to argument0...argument15 or set up separate local variables for them.

Example:
Write a function add(a, b) that returns a+b.

BeforeAfter
GML:
var a = argument0;
var b = argument1;
return a+b;
GML:
function add(a, b) {
    return a+b;
}

Script Functions with Unknown Number of Arguments

If your script function takes an unknown number of arguments, leave the script arguments part blank, and continue using arguments and argument_count in the body as before.

Example: Write a function sum(...) that returns the sum of all arguments passed into it.

BeforeAfter
GML:
var s = 0;
for (var i = 0; i < argument_count; ++i) {
    s += argument[i];
}
return s;
GML:
function sum() {
    var s = 0;
    for (var i = 0; i < argument_count; ++i) {
        s += argument[i];
    }
    return s;
}

Script Functions with Trailing Optional Arguments

If your script function has some required arguments followed by a fixed number of optional arguments:
  • Name and list the required arguments between the brackets, just as you would for a script function with a fixed number of arguments.
  • Name and list the optional arguments afterwards, in the form argumentName=defaultValue for each.
You no longer need to refer to argument_count, use argument[n], or set up separate local variables for arguments.

Example:
Write a function draw_set_props(col, [halign], [valign]) that sets at once the font colour (required), horizontal alignment (optional), and vertical alignment (optional). The horizontal alignment should default to fa_left, and the vertical alignment should default to fa_top.

BeforeAfter
GML:
var col = argument[0];
var halign = fa_left;
var valign = fa_top;
if (argument_count >= 2) {
    halign = argument[1];
}
if (argument_count >= 3) {
    valign = argument[2];
}
draw_set_colour(col);
draw_set_halign(halign);
draw_set_valign(valign);
GML:
function draw_set_props(col, halign=fa_left, valign=fa_top) {
    draw_set_colour(col);
    draw_set_halign(halign);
    draw_set_valign(valign);
}

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 2023-12-10: Added link to Slyddar's instructions for GML Visual and new instructions for trailing optional arguments. Also retitled the article to reflect the new version numbering convention.
 
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 Lord
Forum Staff
Moderator
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:
S

SSJCoder

Guest
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
 
ok so instead of calling the script with this: ProcessCommands (name,data); what should i use?
that description is above my experience level

this is the error i get

___________________________________________
############################################################################################
ERROR in
action number 1
of Create Event
for object <undefined>:

string_pos argument 2 incorrect type (undefined) expecting a String (YYGS)
at gml_GlobalScript_ProcessCommands (line 1) - if (string_pos("!hello",argument1) == 1)
############################################################################################
gml_GlobalScript_ProcessCommands (line 1)

This is the ProcessCommands Script:
if (string_pos("!hello",argument1) == 1)
{
heard = argument0;

}
 

chamaeleon

Member
ok so instead of calling the script with this: ProcessCommands (name,data); what should i use?
That is not what you should change.
This is the ProcessCommands Script:
GML:
if (string_pos("!hello",argument1) == 1)
{
    heard = argument0;
}
That is what you should change
GML:
function ProcessCommands(name, data) {
    if (string_pos("!hello", data) == 1) {
        heard = name;
    }
}
 
I love GM even more since 2.3. These new lightweight objects/classes / whatever you want to call them are awesome. I started using this right away when I found out 2.3 could do this. So much faster and way better for writing 'DRY' code.

Just a note for people who are new to all this. The biggest problem you will have is knowing what scope you are in. Be dutiful with your scoping and everything will be fine. Don't cheat and just use globals to dig you out of a hole. You're probably using it wrong.
 
this is the error i get when i try to run my game

ERROR in
action number 1
of Create Event
for object <undefined>:

Variable <unknown_object>.direction(8, -2147483648) not set before
reading it.
at gml_GlobalScript_PlayerAnimateSprite
*insert a bunch of #s here*
gml_GlobalScript_PlayerAnimateSprite (line -1)

what should i do to fix it?
 

FrostyCat

Redemption Seeker
this is the error i get when i try to run my game

ERROR in
action number 1
of Create Event
for object <undefined>:

Variable <unknown_object>.direction(8, -2147483648) not set before
reading it.
at gml_GlobalScript_PlayerAnimateSprite
*insert a bunch of #s here*
gml_GlobalScript_PlayerAnimateSprite (line -1)

what should i do to fix it?
Follow the instructions for "Script Functions without Arguments". Add function PlayerAnimateSprite() { to the beginning of the script and an additional } at the end of the script.

If you are just starting out, I recommend that you use tutorials from after 2020 that comply with current standards (e.g. official tutorials) instead of random unvetted material from YouTube. Learning to program is difficult enough, don't complicate it further by using obsolete learning materials and having to troubleshoot legacy code issues.
 
Follow the instructions for "Script Functions without Arguments". Add function PlayerAnimateSprite() { to the beginning of the script and an additional } at the end of the script.

If you are just starting out, I recommend that you use tutorials from after 2020 that comply with current standards (e.g. official tutorials) instead of random unvetted material from YouTube. Learning to program is difficult enough, don't complicate it further by using obsolete learning materials and having to troubleshoot legacy code issues.
thank you
 
Top