1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

GMS 2 if Toggle

Discussion in 'Programming' started by William Garrison, Nov 7, 2019.

  1. William Garrison

    William Garrison Member

    Joined:
    Apr 16, 2019
    Posts:
    50
    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.
     
  2. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,646
    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().
     
  3. William Garrison

    William Garrison Member

    Joined:
    Apr 16, 2019
    Posts:
    50
    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
            }
        }
     
  4. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,646
    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.
     
  5. William Garrison

    William Garrison Member

    Joined:
    Apr 16, 2019
    Posts:
    50
    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.
     
    rIKmAN likes this.
  6. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,646
    No worries dude, happy to help!
     
  7. TailBit

    TailBit Member

    Joined:
    Oct 16, 2019
    Posts:
    61
    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..
     

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