SOLVED Problems with global variables

My global variable, global.lastEx isn't being kept across rooms as far as I can tell. The code is below:

GML:
*/
//CLICK ON TO ENTER LEVEL
/*!!!!!!!!!!!!!*/
//Grasslands
if (mouse_check_button_released(mb_left))
{
    
    //Forest of Anguish
    if mouse_x < 390 && mouse_x > 340  && mouse_y > 320  && mouse_y < 370
    {
        if global.lastEx = 1
        {
            global.lastEx = 2
            global.game_map = 7;
            room_goto(SecondLevel)
        }
        
    }
}
        
//go to level 3
//Avalanche
if (mouse_check_button_released(mb_left))
{
    show_debug_message("Passed Ex mouse check button");
    if (global.lastEx == 2)
    {
        if mouse_x < 558 && mouse_x > 540  && mouse_y > 301  && mouse_y < 336
        {
            show_debug_message("Passed border lines Ex")
            //global.game_map = 6;
            global.game_map = 20;
            global.lastEx = 3
            room_goto(ThirdLevel)
        }
    }
}


//////////************LEVEL FOUR*************************/////////////


//The Forge
if (mouse_check_button_released(mb_left))
{
    if global.lastEx == 3
    {
        if mouse_x < 905 && mouse_x > 713 && mouse_y > 267  && mouse_y < 375
        {
            global.lastEx = 4
            global.game_map =30;
            show_debug_message("GLOBALGAMEMAP " + string(global.game_map));
            room_goto(FourthLevel)
        }
    }
}

//Deep Jungle
if (mouse_check_button_released(mb_left))
{
    
    //if mouse_x < 489 && mouse_x > 900  && mouse_y > 501  && mouse_y < 950
    ///{
    
    if mouse_x < 920 && mouse_x >650 && mouse_y > 500 && mouse_y < 628
    {
        if global.lastEx == 4
        {
            global.lastEx = 5
            
            global.game_map = 40;
            room_goto(FifthLevel)
        }
    }
}
    
//It's Desert Monument

if mouse_check_button_released(mb_left)
{
    if (global.lastEx == 5)
    {
        
        if mouse_x < 753 && mouse_x > 460  && mouse_y > 653  && mouse_y < 726
        {
            
            global.lastEx = 6
            global.game_map = 50
            room_goto(SixthLevel)
        }
    }
}
 

kburkhart84

Firehammer Games
Is there any other code where you are setting that global variable, like in some controller object? You can do a search for it using ctrl-shift-F, which is a project wide search thing.
 
No. Its only being used in that block of code I posted. When I comment out lastEx it works, but I need lastEx, its a check on the last level that was completed.

(edit)

Is it bad practice to post more than one topic at a time? I'm working on multiple systems in the same game and I have a code error in another part of the game as well?
 

chamaeleon

Member
No. Its only being used in that block of code I posted. When I comment out lastEx it works, but I need lastEx, its a check on the last level that was completed.

(edit)

Is it bad practice to post more than one topic at a time? I'm working on multiple systems in the same game and I have a code error in another part of the game as well?
If that is truly the only place you use the variable you should get a runtime error on the first if statement when checking its value. You must surely have to have an assignment to it somewhere before this code runs.
 

kburkhart84

Firehammer Games
No. Its only being used in that block of code I posted. When I comment out lastEx it works, but I need lastEx, its a check on the last level that was completed.

(edit)

Is it bad practice to post more than one topic at a time? I'm working on multiple systems in the same game and I have a code error in another part of the game as well?
There HAS to be some initialization of that variable somewhere. Otherwise, you would get an error the first time you run across it in the if() statement.

EDIT*** Ninja'd, same exact point @chamaeleon LOL
 
True I did overlook something. In the create event I have the assignment:
global.lastEx = 1

The other code is in a step event.
So when it creates the room could it be resetting the lastEx = 1? And that's why my check is failing?

How can I go about getting around this?
 

chamaeleon

Member
And you have of course verified that this create event is only runs once and not every time you enter a room.
 

kburkhart84

Firehammer Games
I think since it is a global I am going to move the initialization to a controller that starts with the game.
That's the idea. I typically have a room that is my initialization room. It is persistent and stores variables like this. Then I go to the "real" init room, like the splash screen. I also make sure to NEVER have an instance of that controller object in any other rooms.

If I need to do something like testing without going to that room for whatever reason, I go ahead and put an instance of that thing in the other room, but this isn't typically a thing, as I prefer to just make ways to skip levels, etc...
 
Top