Author Topic: Determining if any other command is running  (Read 1985 times)

jcdick1

  • Jr. Member
  • **
  • Posts: 59
Determining if any other command is running
« on: March 11, 2021, 11:44:59 PM »
I am trying to write a command that will behave in certain ways depending on if any command other than itself is running.  If I use something like

If {CMD} does not equal {Querying command} then do X
If {CMD} equals {Querying command} then do Y

do I need to use a compound containing a bunch of ORs if the {Querying command} is a dynamic command, such that each possible iteration of {Querying command} is represented?

Also, will {CMD} be populated by {Querying command} as soon as I invoke its trigger, even if another command is currently running?  If so, that would basically force it to perform action Y every time.  In which case, I'll need to come up with some other method to get to my result.

Thanks for any insight!

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Determining if any other command is running
« Reply #1 on: March 12, 2021, 01:12:38 AM »
The value retrieved by "{CMD}" is relative to the command it's used in, E.G. if you have "command a" execute "command b", within "command a" the token will return "command a", and within "command b", the token will return "command b".
Executing "command c" will not change either of those (but "{CMD}" will of course return "command c" when used within "command c").

The "{CMDISSUBCOMMAND}" token can be used to check whether the command it's used in was executed by another command, and there are a few tokens than can check whether a specific command is running, however there is no native method for checking whether any command is running except for the current one, while still executing the command immediately.

jcdick1

  • Jr. Member
  • **
  • Posts: 59
Re: Determining if any other command is running
« Reply #2 on: March 12, 2021, 01:46:14 AM »
however there is no native method for checking whether any command is running except for the current one, while still executing the command immediately.

While I would have to give it serious consideration whether the effort is worth it or not, if I were to add a set Boolean "{running}=true" at the beginning of all other commands and "{running}=false" at the very end, then I would guess that if I execute this {Querying command} and have it do a compare, that should theoretically work?

But then, if two other commands are executing, and the first command finishes before the second, it could set that Boolean incorrectly if I were to trigger the {Querying command} before that second command completes.

Or, if there's no "native" way to do it, could you maybe give me some hints of a "non-native" way?

Thanks!

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Determining if any other command is running
« Reply #3 on: March 12, 2021, 02:05:29 AM »
Setting up all your commands for that functionality could be possible, if tedious.

Rather than using a Boolean value, you can use an integer that is incremented when a command starts, and decremented when it stops, so that if the value is 0, theoretically no other commands are running.

This would require a plugin or inline function that resets the value to 0 if all commands are stopped, too.


Obviously a major limitation of that technique is that any command you don't (and/or can't) modify won't be included in the count.



Another option would be to have all your other commands set up with the "Allow other commands to execute while this one is running" option disabled, while your "Querying command" has both it and the "Always execute this command" options enabled.

You can then use a separate command that does not have the "Always execute this command" option enabled, and have it set a variable when executed, E.G.
check if command is allowed to execute
Code: [Select]
Set Boolean [~~noOtherCommandsAreRunning] to True

Where your "Querying command" then uses an inline function to execute that command not as a subcommand (as otherwise the executed command takes on certain properties of the calling command, including the "Always execute this command" option), then check whether the variable was set, E.G.
Code: [Select]
Inline C# Function: Execute command not as subcommand, wait until execution finishes
Begin Boolean Compare : [~~noOtherCommandsAreRunning] Equals True
    Write [Blue] 'This should be the only running command' to log
Else
    Write [Blue] 'Other commands are running' to log
End Condition

Where "Execute command not as subcommand" contains
Code: [Select]
public class VAInline
{
public void main()
{
VA.Command.Execute("check if command is allowed to execute", WaitForReturn: true, AsSubcommand: false);
}
}


Note that this assumes that the "Cancel Blocked Commands" option on the "System / Advanced" tab of the VoiceAttack options window is enabled, as it is by default.