GML How to check if global variable is undefined?

M

MarisFrance

Guest
Code:
if !is_undefined(global.server_socket)
{
    if global.server_socket >= 0
        network_destroy(global.server_socket);
    global.server_socket = undefined;
}

Code:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Create Event
for object oMainMenu:

global variable name 'server_socket' index (100010) not set before reading it.
 at gml_Object_oMainMenu_Create_0 (line 4) - if !is_undefined(global.server_socket)
############################################################################################
--------------------------------------------------------------------------------------------
 

Smiechu

Member
You can't...
"Undefined" means the variable has no numerical or string value... but "undefined" is also a kind of value, and must be defined.
 
Last edited:

TheouAegis

Member
Always Define your Global variables at the beginning of the game. checking if a variable even exists at all is horrible programming practice and will not work in many other languages. if you don't know if a variable even exist, then you 💩💩💩💩ed up somewhere along the way.
 
Always Define your Global variables at the beginning of the game.
Good point. Agreed! @MarisFrance Ignore my post, do this.

if you don't know if a variable even exist, then you ****ed up somewhere along the way.
Most of the time, yes, however I've found the function variable_instance_get_names() very useful for printing debug info of the current state of all an instance's variables without having to know the names in advance. But this is an exception rather than the rule.
 

TheouAegis

Member
Debugging is always an exception. lol

Even the help file entry you posted says variable_global_exists shouldn't be used. It's functions like it I despised in GM8, too. It meant every variable was being stored in the exe as name AND pointer, not just pointer.
 
M

MarisFrance

Guest
Always Define your Global variables at the beginning of the game.
But I need to check if variable is initialized.

I use this code in main menu of the game. User can get main menu from start OR from stopping game session. So I need to close network sockets etc. If I just set variable to new value, it will create new socket causing memory leak on each game start/end.
 

TheouAegis

Member
What I'm saying is you need to create the variable before you even get to the main menu. Have a room before the main menu that you go to when the game starts up. Do not go back to that room at any other time. This means you do not ever ever use the game_restart function.
 
D

dannyjenn

Guest
Are you using Studio? I thought they got rid of the variable_global_exists() function years ago. Guess they never did...
 

Smiechu

Member
But I need to check if variable is initialized.

I use this code in main menu of the game. User can get main menu from start OR from stopping game session. So I need to close network sockets etc. If I just set variable to new value, it will create new socket causing memory leak on each game start/end.
Initialize the variable by default with "undefined" value, and the code you posted on the beggining will work.
 
Top