GameMaker if Toggle

Why doesn't this work?

Code:
        var toggle = false;
      
        if keyboard_check_pressed(ord("1")) && (!toggle) {
            image_index = 1;
            toggle = true;
        }
        if keyboard_check_pressed(ord("1")) && (toggle) {
            image_index = 0;
            toggle = false;
            // more here
        }
What I am trying to have happen is when the user presses 1 the frame changes to 0 and sets toggle to true. Then if they hit 1 again it switches back. Seems so simple. It's little things like this that really tear me down. What am I missing the logic on here? Its like the variable isnt ... variabling.
 

rIKmAN

Member
Why doesn't this work?

Code:
        var toggle = false;
   
        if keyboard_check_pressed(ord("1")) && (!toggle) {
            image_index = 1;
            toggle = true;
        }
        if keyboard_check_pressed(ord("1")) && (toggle) {
            image_index = 0;
            toggle = false;
            // more here
        }
What I am trying to have happen is when the user presses 1 the frame changes to 0 and sets toggle to true. Then if they hit 1 again it switches back. Seems so simple. It's little things like this that really tear me down. What am I missing the logic on here? Its like the variable isnt ... variabling.
Standard "if" checks are ran in order one after the other so the first "if" sets toggle to true, then the next "if" is checked and because toggle is now true it gets set back to false.

If you only want one condition to be evaluated you need to use "else if" for the second keyboard_check_pressed().
 
Standard "if" checks are ran in order one after the other so the first "if" sets toggle to true, then the next "if" is checked and because toggle is now true it gets set back to false.

If you only want one condition to be evaluated you need to use "else if" for the second keyboard_check_pressed().
Ugh yeah i did have that before. It does allow the first if to fire correctly. But does not work on the else if portion. So this is what I have now...Again the first works not the else if part though.

Code:
with (inst_5D781626) {
        var toggle = false;
       
        if keyboard_check_pressed(ord("1")) && (!toggle) {
            image_index = 1;
            toggle = true;
        } else if keyboard_check_pressed(ord("1")) && (toggle) {
            image_index = 0;
            toggle = false;
            // more here
        }
    }
 

rIKmAN

Member
Ugh yeah i did have that before. It does allow the first if to fire correctly. But does not work on the else if portion. So this is what I have now...Again the first works not the else if part though.

Code:
with (inst_5D781626) {
        var toggle = false;
    
        if keyboard_check_pressed(ord("1")) && (!toggle) {
            image_index = 1;
            toggle = true;
        } else if keyboard_check_pressed(ord("1")) && (toggle) {
            image_index = 0;
            toggle = false;
            // more here
        }
    }
You are setting toggle to false before the two "if" checks, so the "else if" will never evaluate to true as it is checking if toggle is true which it will never be at the point of that check.

Make toggle an instance variable and set it to false in the Create Event, remove the "var toggle = false" line from the above code and it should behave as you expect.
If it's meant to be a toggle you don't want it resetting every step back to false the way you are doing above.
 
You are setting toggle to false before the two "if" checks, so the "else if" will never evaluate to true as it is checking if toggle is true which it will never be at the point of that check.

Make toggle an instance variable and set it to false in the Create Event, remove the "var toggle = false" line from the above code and it should behave as you expect.
If it's meant to be a toggle you don't want it resetting every step back to false the way you are doing above.
Okay yeah that makes sense. Thank you rIKmAN. You have helped me a lot of times with my issues. Appreciate the patience and assistance. It does in fact work correctly now.
 

TailBit

Member
Code:
// create event
toggle = false;

// step
if keyboard_check_pressed(ord("1")){
    image_index = toggle;
    toggle = !toggle;
}
You can toggle a variable between true/false with ! .. and also set image index to the toggle value since it will be 1 or 0 after all .. this is just a way to make it shorter, not that handy if you need more code in the ifs tho..
 
Top