SOLVED How to check if a global variable exists without using a ds_map

Evanski

Raccoon Lord
Forum Staff
Moderator
Hi! As the title says I want to figure out if a global variable (global.cats) exists without having to put every global in the entire game into a ds_map and checking against it.

for more context.

im currently doing it like this
GML:
//If variable name actually exists
          
if (variable_global_exists(set_command_variable_name_argument))
{
    variable_global_set(set_command_variable_name_argument, set_command_value_1_argument);
}
set_command_variable_name_argument is the global variable we are checking to see if it exists
set_command_value_1_argument is just a value for easy understanding lets just say this is 6

now im getting these values from a string, (im getting user input so I can change code as the game is running)

I realize I shouldn't be using compatibility functions for this (im not sure why as this works fine if someone could elaborate why thanks)

anyways I want to be able to continue what im doing but with out having every global need to be in a list or specially set up
I'd like to just check if the game has it existing and be able to change it

Thanks!
 
Last edited:
I realize I shouldn't be using compatibility functions for this (im not sure why as this works fine if someone could elaborate why thanks)
Manual doesn't seem to say not to use them. Just says that the function is "...primarily for use in compatibility scripts".
There are some compatibility functions that should never be used in your own project, but that one is not on the list: compatibility functions not to use
 

Evanski

Raccoon Lord
Forum Staff
Moderator
Manual doesn't seem to say not to use them. Just says that the function is "...primarily for use in compatibility scripts".
There are some compatibility functions that should never be used in your own project, but that one is not on the list: compatibility functions not to use
I was told that there "bad practice" , as long as it wont cause me problems down the road im perfectly fine just using the already made code as it works just fine.
 

8BitWarrior

Member
I have been using the variable_*() functions extensively. I don't see a problem with using them.

If it is vital to have your codebase as secure as possible, then maybe it could be an issue somehow, but I'd consider it to be fine for 99% of use cases out there.
 

Evanski

Raccoon Lord
Forum Staff
Moderator
@8BitWarrior @BaBiA Game Studio
Well seeing as it seems to be fine and I dont really care about my codebase being secure as possible as im not making anything dealing with user data
Im going to write it off as fine,

Thanks for the help!
 

8BitWarrior

Member
Do note, however, that they are slower to use than directly assigning values. Again, if performance isn't a critical issue, it's fast enough.
 

Evanski

Raccoon Lord
Forum Staff
Moderator
Do note, however, that they are slower to use than directly assigning values. Again, if performance isn't a critical issue, it's fast enough.
im not noticing any extensive performance loss, it seems to run the code in about a single second maybe 2 so thats good enough for me
 
Top