GM:S 1.4 [SOLVED] Where should global variables be initialized?

Discussion in 'Programming' started by detour, Sep 10, 2016.

  1. detour

    detour Guest

    Hi,

    I'm new to GM Studio (running 1.4), and have followed a few tutorials successfully, making a couple simple games. I'm trying to figure out where the best place is to initialize a global variable such that you won't run into the "global variable .... not set before reading it" error.

    My current problem is I have a global variable that I need to initialize at the very beginning of the game. This variable then get's modified by events that occur in the game. I can't seem to find a place to put it that doesn't produce the error described above. I tried putting it in the room creation code, creating a new object and putting it in that objects Create event, moving that object to the top of Instance Order in the room, making sure none of the accesses of that global variable occur in any other object's Create events, etc. They all produce the error.

    Any ideas on where I should declare my global?

    Am I wrong to think that the Create events are executed sequentially in the room Instance Order list (from top to bottom in the list)?

    Thanks for your help,
     
  2. BLang

    BLang Member

    Joined:
    Jun 21, 2016
    Posts:
    109
    You're not wrong, creation events are executed sequentially.

    Can you post how you initialize your global and how you use it?

    If not, a good idea is to create a room that will serve as an 'initialization room'. There, you can initialize any and all variables that might cause crashes because of instance creation order. You do this by plonking in a persistent control object that initializes the variables in it's create event, and then goes to the next room immediately.

    However, making sure that the instance is first in the instance order in the room should solve the problem just as well, which leads me to believe that there's something else wrong with your code.
     
    BoredMormon likes this.
  3. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    6,869
    In general, I have an "Init" room which is the very first room of my game. I create this room and in the Room Create Event (in the room editor), I initialise ALL my globals, enums, save files, etc... and then call room_goto_next() to actually start the game. From then on simply use room_goto(FIRST_GAME_ROOM), rather than game_restart() so that this room is never called again. This way everything that needs to be called once will be called when the game is first started and never again.
     
    Schwee, maratae, JAG and 4 others like this.
  4. Roderick

    Roderick Member

    Joined:
    Jun 22, 2016
    Posts:
    577
    I do something similar to what @Nocturne suggests: I creat a script called scr_initialize that does all my startup stuff. I might put it in room creation code, or in the creation code for a controller object when I'm doing something that all happens in a single room.

    By putting it in a script, if you have to edit it, you can just open it from the resource tree, rather than opening the room, switching to the creation tab, and opening the creation code.
     
  5. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,788
    Make sure you use global. in all references to the variable. Make sure you don't use the globalvar tag. Make sure you don't use the var tag. Really, all there is to initializing a global variable is

    global.variable = 0

    And that's that. If you put it in the Create Event of an instance that is at the very top of the instance order list and is in the very first room of the game, then the variable should be okay.

    Concerning the people that use the Room Creation Code, bear in mind that the room creation code runs AFTER every instance has run their create event. So typically the initialization room would be a room that's literally all by itself. As Roderick said, the room doesn't need any objects in it. You just run whatever code you want to run and then room_goto_next().
     
  6. Roderick

    Roderick Member

    Joined:
    Jun 22, 2016
    Posts:
    577
    Actually, that was Nocturne, not me. ;D

    I didn't realize that; it's never come up for me. Thanks for that.
     
  7. detour

    detour Guest

    Wow thanks for the replies everyone. I like the idea of an "init room."

    @BLang I think your right, I must just be doing something wrong in my code. I'm going to clean things up a bit and try the initialization room, and post back on my results.

    @TheouAegis I was curious when the room creation code ran in the instance creation order. Thanks for clearing that up.
     
  8. detour

    detour Guest

    Okay so I solved the problem. I believe it was two fold.
    1. The concept of an initialization room was a good design approach take-away from all your comments. It is a very intuitive approach to initialization for me as it will run and "setup" my game parameters before any instances of objects are ever created. Which means those instances can now safely read the game parameters.
    2. I actually had a small typo in my variable, so the error was valid. That's what I get for staying up too late programming. I couldn't see the difference in the variable name from the initialized variable to accessed variable later in the code. Whoops.
    I come from an Eclipse / VIM heavy environment with really good auto-complete. While CTRL+SPACE was doing built in function autocomplete in GM:Studio, it wasn't auto completing my globals, so I just typed it wrong.

    Thanks for the help!
     
    BLang likes this.
  9. MadZenno

    MadZenno Member

    Joined:
    Dec 31, 2017
    Posts:
    25
    Can we have an example of having an "init room" ? I like the idea too.
     
  10. BaBiA Game Studio

    BaBiA Game Studio Member

    Joined:
    Jun 20, 2016
    Posts:
    845
    Both @Roderick and @TheouAegis have already stated how to do this. Just create a room, put it as the first one in your game and then do all of the initialising of the global variables in the Room Creation code of the room. The last thing that should be in the Room Creation code should be the room_goto_next() command - or room_goto(<roomtogoto>) if you want to specify a room that is not the next one in the list. It's that simple, and doesn't really need anything more than that for an example.

    It was probably also not a great idea to necro a post that was more than 1 year old. :)
     
    Roderick likes this.
  11. hogwater

    hogwater Member

    Joined:
    Jun 22, 2017
    Posts:
    635
    I like the init room thing too, but I don't put anything in the room creation code except room_goto_next(). I don't like having things hidden away in there, and like others have said all these "room start" and "game start" events run after a regular old instance's create event.

    The init room is just a blank room where I place controller objects. Each one handles a different thing, like inputs, debug variable drawing, audio, whatever. I declare whatever variables are needed in their create events. I try to organize it so I will easily know where to look for something when I have problems.
     

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