GML how does "is_undefined" work?

BlueCat6123

Member
trying to use is_undefined to test if I've set a variable, but i get this error message:

Variable o_soli.active(100039, -2147483648) not set before reading it.
at gml_Object_o_soli_Create_0 (line 15) - if (is_undefined(active)) game_end()

I find it kinda funny because that seems to take away the purpose of the function, or am I just using it wrong?
 

Mercerenies

Member
is_undefined doesn't check if a variable is not defined (yes, I know how silly that sentence sounds out of context). It checks whether the value of the variable is the special sentinel value "undefined". So in your create event, you should set active to undefined, which is a special value, until you know what value to fill it in with. There's no way to see if a variable has been defined on an object, as that would be reflection and GML prioritized performance over reflection some time ago and removed all of the reflection capabilities.
 

rIKmAN

Member
trying to use is_undefined to test if I've set a variable, but i get this error message:

Variable o_soli.active(100039, -2147483648) not set before reading it.
at gml_Object_o_soli_Create_0 (line 15) - if (is_undefined(active)) game_end()

I find it kinda funny because that seems to take away the purpose of the function, or am I just using it wrong?
The function isn't used to test if you have declared a variable, it's used to test if a variable that already exists holds the value "undefined".

From the manual:
An undefined value (also known as a "null" value") is one where an expression doesn't have a correct value, although it is syntactically correct, and so must return something. For example, say you have a ds_map and use the function ds_map_find_value(). Now, what happens when the map does not have the value being looked for? Well, since the function is correctly formatted, and the issue is that the no such value exists, then it would return the constant undefined, and you can check for this constant as you would check for true or any other value.
edit: ninja'd
 

Samuel Venable

Time Killer
is_undefined() checks whether an existing variable or expression has been given the special null value undefined, not whether the variable exists. The latter is the job of variable_instance_exists() and variable_global_exists(). Note that these two functions take the variable name in string form to avoid dereferencing it as part of the call.
ya, i remember when they deprecated these claiming it wasn't possible because the runner is no longer an interpreter, but the reality shows later on, they admit, it interprets bytecode to this day.

(I hope I remember wrong or don't have a correct understanding of the situation)
 
Last edited:
Top