How do I toggle a value using one command?

0 favourites
  • 6 posts
From the Asset Store
In this game you will command the character by voice command.
  • I feel like there is a simple answer here but I am flustered and can't figure it out right now.

    The 2 basic conditions I used were

    If Q pressed --- Set Value to 1

    Value = 0

    If Q pressed --- Set Value to 0

    Value = 1

    I'm pretty sure I understand why this doesn't work, but can't think of a way around it. Am I missing something?

    Thanks for any input

  • I think your second event is executed after the first one, and it sets the value back to 0.

    You should move your second condition to a sub-event:

    If Q pressed 
           Value = 0   -> Set Value to 1
           Else        -> Set Value to 0
    [/code:wnkayh43]
    
    Also, there is an easier way to toggle boolean variable, in just one line:
    If Q pressed -> Set Value to (Value=0)
  • Ok I see, thanks, both these solutions will work fine.

    I was trying to use the "else" condition earlier but couldn't figure out how to implement it, this makes sense though.

    I don't understand how the second solution works though. Setting a value to (itself=0) ? It works but I don't understand how lol.

    Thanks again for the help

  • In any programming language False=0 and True=1.

    When value=0, the expression (value=0) will return True and value will become 1.

    When value=1, the expression (value=0) will return False and value will become 0.

  • There is another way to toggle boolean using ternary operator:

    value = (value ? 0 : 1)

    (value ? 0 : 1) means "if value is true, then 0, else 1"

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Ok I see. I'm pretty sure I understand that now.

    Programming has never been my strong suit so things like this aren't always easy for me.

    I very much appreciate the help though. The solution is working perfectly

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)