Author Topic: How do I compare a variable to a range of numbers?  (Read 1068 times)

d3ck3r_x

  • Newbie
  • *
  • Posts: 4
How do I compare a variable to a range of numbers?
« on: July 27, 2021, 07:16:25 AM »
I am trying to make a command for a RTS game where you group units using the key bindings Ctrl+1 to 9. Occasionally the speech recognition software hears something other than 1 to 9. I'd like this script to give me an audible prompt and a log entry when the group command is unsuccessful. I have attempted this verification check below with the compounded if statement after that jump marker "executable" but this has the complete opposite effect. I have tried a few versions of this compounded if statement and can get none of them to work correctly. It also seems a bit of a clunky way to check a variable against a range so if there's a better way please can someone point me in the right direction.

Code: [Select]
Set text [~input] to '{CMD_AFTER}'
Begin Text Compare : [~input] Contains 'won'
    Set text [~input] to '1'
    Jump to Marker: executable
Else If Text Compare : [~input] Contains 'for'
    Set text [~input] to '4'
    Jump to Marker: executable
End Condition
Set text [~input] to '{TXTWORDTONUM:~input}' (Replace 'to' with '2')
Set text [~input] to '{TXTNUM:~input}'
Marker: executable
Begin Condition : [~input] Does Not Contain '1' OR [~input] Does Not Contain '2' OR [~input] Does Not Contain '3' OR [~input] Does Not Contain '4' OR [~input] Does Not Contain '5' OR [~input] Does Not Contain '6' OR [~input] Does Not Contain '7' OR [~input] Does Not Contain '8' OR [~input] Does Not Contain '9'
    Write [Red] 'no group set' to log
    Play sound, 'internal:Doonflap'
    Exit Command
End Condition
Write [Orange] 'group {TXTNUM:~input} set' to log
Execute command, 'control' (and wait until it completes)
Pause 0.01 seconds
Quick Input, '{TXTNUM:~input}'
Pause 0.01 seconds
Execute command, 'control' (and wait until it completes)

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: How do I compare a variable to a range of numbers?
« Reply #1 on: July 27, 2021, 07:26:45 AM »
You could convert ~input to an integer variable and check for a value greater than 0 and less than 10


Your compound condition cannot work correctly, as it checks whether the input variable contains every single one of the numbers you've specified, which it likely won't.

E.G. if your ~input variable value is "1", the statement "[~input] Does Not Contain '2'" will evaluate to True, as ~input does not contain "2".

You'd want to swap it to "AND", rather than "OR", if you want to have the command proceed if ~input contains any one of the numbers (and exit or it contains none of them).


That said, if you only have ten values, you could instead predefine those using a numeric range in a dynamic command section, rather than using wildcards, to make sure the speech recognition engine only recognizes the expected input.

d3ck3r_x

  • Newbie
  • *
  • Posts: 4
Re: How do I compare a variable to a range of numbers?
« Reply #2 on: July 28, 2021, 05:30:16 AM »
Numeric range in a dynamic command achieved the required result. Gone from being an over-complicated script down to a simple 5 line script. As you stated there's no need for wildcards when working with a range with only a few easily defined values.