Author Topic: Writing a command to toggle stuff 'n things  (Read 4343 times)

ecthomason

  • Guest
Writing a command to toggle stuff 'n things
« on: November 11, 2016, 11:59:36 AM »
  I wrote this command to toggle bunny hopping with a voice command, and it works fine, but I was curious if there is a more efficient way to do it.

Code: [Select]
Begin Boolean Compare : [TOG_BUNNYHOP] Has Not Been Set
    Set Boolean [TOG_BUNNYHOP] to False
End Condition
Begin Boolean Compare : [TOG_BUNNYHOP] Equals True
    Set Boolean [TOG_BUNNYHOP] to False
    Say, 'bunny hop, off'
Else
    Set Boolean [TOG_BUNNYHOP] to True
    Say, 'bunny hop, on'
End Condition
Start Loop While : [TOG_BUNNYHOP] Equals True
    Press Space key and hold for 0.25 seconds and release
    Pause 1 second
End Loop

Gary

  • Administrator
  • Hero Member
  • *****
  • Posts: 2832
Re: Writing a command to toggle stuff 'n things
« Reply #1 on: November 11, 2016, 12:22:52 PM »
Since null ('not set') never equals or does not equal true or false (much to Pfeil's peril), you can use it here to shorten up your statement:

Code: [Select]
Begin Boolean Compare : [TOG_BUNNYHOP] Equals True
    Set Boolean [TOG_BUNNYHOP] to False
    Write '[Blue] bunnyhop was true, now it is false' to log
Else
    Set Boolean [TOG_BUNNYHOP] to True
    Write '[Blue] bunnyhop was false or not set, now it is true' to log
End Condition

ecthomason

  • Guest
Re: Writing a command to toggle stuff 'n things
« Reply #2 on: November 11, 2016, 12:35:47 PM »
 Yeah, I was going to use a small int for the variable type, which would require the check for Not Set, but I thought a Boolean type made more sense. Thanks for the help!

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Writing a command to toggle stuff 'n things
« Reply #3 on: November 11, 2016, 12:38:56 PM »
shorten up your statement:
If that's the intent:
Code: [Select]
Begin Boolean Compare : [TOG_BUNNYHOP] Has Not Been Set
    Set Boolean [TOG_BUNNYHOP] to False
End Condition
Set Boolean [TOG_BUNNYHOP] to Toggle
Say, 'bunny hop, {EXP:IIF('{BOOL:TOG_BUNNYHOP}' = 'true','On','Off')}'
Start Loop While : [TOG_BUNNYHOP] Equals True
    Quick Input, '[SPACE][PAUSE:1]'
End Loop
:P

Gary

  • Administrator
  • Hero Member
  • *****
  • Posts: 2832
Re: Writing a command to toggle stuff 'n things
« Reply #4 on: November 11, 2016, 01:08:07 PM »
One of my first things to do in the upcoming betas is to include an option to set default values for condition statements .

Boolean will be false
Text will be empty (blank)
Numbers will be zero
Date will be Date.min or now...

Th option will be on by default when added but will maintain last state. I think that will help out the folks that are new to this and still maintain flexibility.