GameMaker Difference between undefined and variable not set.

Malcoball

Member
Hi there, first post so don't kill me here.
Can someone explain the difference between a variable that's undefined and not set? It's just I'm used to JavaScript where they're the same thing.
The reason I ask is sometimes I try to handle this by doing say if (is_undefined(fullScreen)) fullScreen = true;
But it just tells me it's not been set, which should be the same as undefined right?
Anyway thankyou.
 

rytan451

Member
In GameMaker, undeclared variables have no value. This is like a null pointer in that attempting to get its value (like in your example, is_undefined(fullScreen)) causes an error. Once the variable is declared, either using var, the deprecated globalvar, or by setting its value, it is no longer undeclared, and now contains a value (which may be the special value undefined).
 

Yal

šŸ§ *penguin noises*
GMC Elder
Variables are created on-demand in Game Maker, so if you never have assigned a value to a variable, it doesn't exist. Undefined, on the other hand, means an illegal value... or perhaps "value that doesn't exist" helps clear up the distinction better.

Older versions had some handling for this with the "just assume a variable is 0 if it doesn't exist" option, but it basically turned out to be a "hide typos" button that caused more issues than it solved - if you ever mistyped a variable name it'd create a new variable with that name, and your code wouldn't work because you read from a completely different source of data than the value you changed.

It's better coding style to initialize all variables you need ahead of time to known values, anyway... these kinds of hacks tend to fall apart like a house of cards if something somewhere breaks.
 

TheouAegis

Member
Some data structures, like lists, will return undefined if the index you try to reference doesn't exist. That's what is_undefined() is primarily for.
 

Malcoball

Member
In GameMaker, undeclared variables have no value. This is like a null pointer in that attempting to get its value (like in your example, is_undefined(fullScreen)) causes an error. Once the variable is declared, either using var, the deprecated globalvar, or by setting its value, it is no longer undeclared, and now contains a value (which may be the special value undefined).
So undefines work more like null would? Thanks for the response. It seems pretty clear now.
 

TsukaYuriko

ā˜„ļø
Forum Staff
Moderator
Almost.

pointer_null is a null pointer.
undefined is returned when a function has to return something but has no appropriate or "correct" value to return, such as attempting to get a list element out of bounds.
(Source: https://manual.yoyogames.com/#t=GameMaker_Language/GML_Overview/Variables/Constants.htm)
A variable that was "not set" according to error messages is a variable that has not been declared (case 1), or declared but not assigned a value (case 2).

Case 1:
GML:
show_message(test);
This variable does not exist at all. It therefore can't have a value.
variable_instance_exists says the variable does not exist.
Error: local variable <unknown built-in variable>(-1610512726, -2147483648) not set before reading it.

Case 2:
GML:
var test;
show_message(test);
This variable has been declared as local, but has not been set. It has no value.
Setting it further down the line will make it come into existence as a local variable.
variable_instance_exists says the variable does not exist.
Error: local variable <unknown built-in variable>(-1610512726, -2147483648) not set before reading it.

Case 3:
GML:
var list = ds_list_create();
var test = list[| 0];
show_message(test);
This variable has been declared and set.
variable_instance_exists says the variable exists.
The variable has the value undefined.
Output: undefined

Case 4:
GML:
var list = ds_list_create();
list[| 0] = "works";
var test = list[| 0];
show_message(test);
This variable has been declared and set.
variable_instance_exists says the variable exists.
The variable has the value "works".
Output: works


I hope that clears it up. :)
 
Top