1. Hey! Guest! The XXX GMC Jam will take place between August 24th, 12:00 UTC (Friday noon) and August 27th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GML How to check if global variable is undefined?

Discussion in 'Programming' started by MarisFrance, Aug 10, 2018.

  1. MarisFrance

    MarisFrance Member

    Joined:
    Jul 26, 2018
    Posts:
    22
    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)
    ############################################################################################
    --------------------------------------------------------------------------------------------
     
  2. Smiechu

    Smiechu Member

    Joined:
    Jul 14, 2017
    Posts:
    475
    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: Aug 10, 2018
    MarisFrance likes this.
  3. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    1,609
    MarisFrance likes this.
  4. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    4,966
    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.
     
    Binsk and IndianaBones like this.
  5. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    1,609
    Good point. Agreed! @MarisFrance Ignore my post, do this.

    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.
     
  6. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    4,966
    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.
     
  7. MarisFrance

    MarisFrance Member

    Joined:
    Jul 26, 2018
    Posts:
    22
    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.
     
  8. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    4,966
    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.
     
    MarisFrance likes this.
  9. dannyjenn

    dannyjenn Member

    Joined:
    Jul 29, 2017
    Posts:
    242
    Are you using Studio? I thought they got rid of the variable_global_exists() function years ago. Guess they never did...
     
  10. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    2,846
    YoYo got rid of it at the dawn of GMS 1.x, then reintroduced it to GMS 1.4 in 2017 and GMS 2.x at inception.
     
    dannyjenn likes this.
  11. dannyjenn

    dannyjenn Member

    Joined:
    Jul 29, 2017
    Posts:
    242
    [post deleted]
     
    Last edited: Aug 11, 2018
  12. Smiechu

    Smiechu Member

    Joined:
    Jul 14, 2017
    Posts:
    475
    Initialize the variable by default with "undefined" value, and the code you posted on the beggining will work.
     
    dannyjenn likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice