Android How can we check if a variable exists or not?

P

PHL

Guest
Game Maker Studio ditched the variable_local_exists() and variable_global_exists() functions, so my life is harder now in game programming.

I found a function called is_undefined, and I rejoiced, thinking that it would help me.

Zero.

I tried something like this:

is_undefined(variable),

and got an error and a crash.

The irony is that if Game Maker Studio can give an error message when you use an undefined variable, it means she can check for variables. So why is there no function for it?

How do people cope with being unable to check if a variable exists?
 
J

joqlepecheur

Guest
For me, I never check, but then I use simple code so I don't know.
I fear that if I cover loopholes with code I would have a hard time fixing bugs.

Even though I never use it, I am surprised that it is not available anymore.
 

NicoFIDI

Member
make into every object inside the create event a script like this:
Code:
/// Initialize
value_alpha = noone;
value_beta = noone;
value_theta = noone;
then you check the variable it's noone to know if you have aleady a value.

Also, i dont know if it still works but you can use:
Code:
if(object.value != undefined) {
    //Do Work
}
 
The irony is that if Game Maker Studio can give an error message when you use an undefined variable, it means she can check for variables. So why is there no function for it?
No, it can't check for it. That's why the game is crashing.
I would suggest instantiating your variable somewhere and setting it equal to noone. Then you can just check if the variable is not equal to noone.
Edit: The reason for this is that GML is compiled, not dynamic code - meaning that a underlying restriction of it (similar to what its built on, C++), is that you can't do these sort of checks.
 

hippyman

Member
is_undefined is really only useful for things like checking a value in a data structure. Say you have a list of 10 numbers. You try to find number 11 but there is no number 11 so using is_undefined would return true.

Quick code example
Code:
list = ds_list_create();
var i = 0;
repeat(10) {
    ds_list_add(list,i++);
}

show_debug_message(list[|9]);                         //outputs 9
show_debug_message(is_undefined(list[|9]));  //outputs 0
show_debug_message(list[|10]);                       //outputs undefined
show_debug_message(is_undefined(list[|10]));//outputs 1
This is based off of memory but I'm pretty sure this small snippet should work and should give you an idea on how to use the is_undefined function.
 

Tsa05

Member
How do people cope with being unable to check if a variable exists?
In less flexible languages, you just have to keep track of the variables you're going to use. So, you can set a variable to something like "undefined"--that is to say, it's defined as undefined. That way, you can check whether it's been *changed* to something other than undefined.

Fundamentally, you're telling the computer "before continuing, please go into memory and let me know the value that is currently stored at location "
And the computer is waiting for you to finish the sentence before it continues. When you don't define the variable, it just can't continue.

Incidentally, GameMaker can show an error when there's an undefined variable due to a type of coding style that will eventually be added to GM: the try-catch method. Since the computer crashes whenever it doesn't know what to do, you can actually give the computer something to do in cases where it doesn't know what to do! Basically, you tell the computer "try doing this code and if you crash, go to the 'catch' section." So then it knows what to do on a crash. In the 'catch' section, you say "ok, since something unexpected happened, I no longer know what to do after this, so just tell the user that I crashed at this place. After that, I really don't know how to continue, so go ahead and crash."

So, while it *appears* that GameMaker knows what to to when a variable is undefined, it actually doesn't know what to do and is simply telling you so before crashing.

But yea, GMS2 has the function again, and it's super-useful for making extensions and stuff that people will add to their projects. Otherwise, though, I think you can always get around this by pre-defining things or using files to store/check for values.
 
P

PHL

Guest
So Game Maker Studio 2 has functions for variables.
Can Game Maker Studio 2 import projects from version 1? I don't want to lose all my work.
 
Top