Variable not declared failsafe

D

digimbyte

Guest
People have mentioned ways about including fail-safes in their objects, in this instance, this game object is listening for a global variable string, if this global variable string does not exist, it crashes obviously.

I've been messing with: if is_undefined(n);

but it doesn't seem to work that way I assumed it would, am I doing something wrong or is there a better way when working with global variables?
 
H

Homunculus

Guest
I'm pretty sure there's nothing like that (unfortunately). You could declare it to something like -1 or "" at game start for example and test against that, or use a ds_map instead and check if the key is present.
 

KhMaIBQ

Member
I recommend declaring ALL of your global variables in a Game Start event in an object that is in the first room of your game. This will make it easier for you to keep track of all the global variables and what they do. Also, if you are needing to check if a global variable is defined to prevent a crash, I would say that is a design flaw in your coding. You shouldn't have to check if a global variable is declared before using it.
 

Mick

Member
I always create an "init" room that is loaded when game starts. In that room there is an object that declares all global variables and does other setups. When that is done, the actual first visible room in the game is loaded with room_goto_next(). With this method it's possible to set any room after the init room (if I'm testing a room or something), I know that all global variables needed will always be declared.
 
A

Aura

Guest
You're misunderstanding how is_undefined() works. It does not tell you if a variable has been declared. The functionality for that is now obsolete and this function is NOT a replacement. It tells you if a value is not defined.

The Manual entry on data types said:
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.
The function checks for that; using it for checking if a variable has been declared is a wrong interpretation of the Manual.
https://docs.yoyogames.com/source/dadiospice/002_reference/001_gml language overview/data types.html

Either way, you shouldn't rely on a function to make sure that your variables have been declared. As long as you have a strong understanding of variable declaration and scope, you can easily avoid pitfalls.

I would also recommend declaring all the global variables at the start of the game. Since global variables persist in memory as long as the game runs, you don't have to worry about them being undeclared elsewhere. Instance variables and local variables should be declared where they're supposed to be. If you still get errors related to uninitialized variables then there's a problem with the game logic and trying to fix it that way is a bad idea.
 
Top