GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2013-10-05 12:23:01

ReeferMadness
Member
Registered: 2013-10-05
Posts: 2

IF statement always returns true

Hey folks,

I couldn't find anything relevant to my issue with a quick search, so I'll have to go ahead to ask. I'm fairly new to GM so I'm still learning the do's and dont's. I think I may have found a don't.

I'm currently creating a small game for a school project and ran in to an issue with the IF statement (tl;dr action starts here) I created a simple script that moves the player to the next room, when the current room is the start screen; all is well there. This if statement also checks for the last room, in which case it will end the game when the enter key is pressed. I've attempted to modify this statement, for testing purposes, to allow me to skip the other rooms with two enter key presses, using a variable to check if the enter key was pressed.

The problem is that even though the value of the variable is initialized as 0 (or false, I've tried Boolean too) it still fires off as though it was true.

Expandif (room != rm_end) {

    if (room == rm_start || enter_pressed == true) {
        
        room_goto_next();
        enter_pressed = false; // Resetting variable
    
    } else {
    
        enter_pressed = true;
    
    }
    
} else {

    game_end(); 
    
}

The enter_pressed is a global variable defined and initialized in a Game Start event, this code was placed in a Enter key event. I feel like I may be overlooking something simple here, but I could use a fresh pair of eyes with a bit more experience telling me where I may have butchered this. Sorry for the lengthy description, I appreciate any help offered.

Offline

#2 2013-10-05 16:53:04

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: IF statement always returns true

So the problem is that it goes to the next room on the first press of "Enter", even if the current room is not "rm_start"?

Is this code in a "Keyboard" event or is it in a "Key Press" or "Key Release" event? If it is a "Keyboard" event, it won't require two presses, it will only require the key to be down for two steps. The "Keyboard" event fires every step, as long as the key is down. The "Key Press" and "Key Release" events only fire when the key  transitions from up to down or down to up, respectively.


Abusing forum power since 1986.

Offline

#3 2013-10-06 11:00:02

ReeferMadness
Member
Registered: 2013-10-05
Posts: 2

Re: IF statement always returns true

Thanks for the response. It worked flawlessly after that quick change. I knew I was overlooking something simple; I've taken the differences of key event functions into account when coding, but it slipped my mind when I had to go back to using the D&D events. Makes sense now:)

Offline

Board footer

Powered by FluxBB