Author Topic: Variables and how to use them properly  (Read 2027 times)

Sim UK

  • Newbie
  • *
  • Posts: 12
Variables and how to use them properly
« on: October 06, 2020, 06:56:55 AM »
Hi all, first post.

Been using and promoting VA for a long while, seen a few versions.  Now I am trying g to learn how to do more complex things, which don't sound that complex, but i am struggling to work out how;

Two "simple" procedures I am trying to produce are;

1) Pause for between 3 and 20 seconds but each time it needs to be a new random (right now i get the same random every time).
2) Random keypress...either 'a' or 'by but again each time it needs to be random.

I've read the manual sections relating to this, read numerous Google posts that sounded similar, but i cannot fathom how to complete what I am sure is a simple thing?

Any guidance appreciated.

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Variables and how to use them properly
« Reply #1 on: October 06, 2020, 08:49:07 AM »
How are your commands set up currently?

Sim UK

  • Newbie
  • *
  • Posts: 12
Re: Variables and how to use them properly
« Reply #2 on: October 06, 2020, 10:14:56 AM »
Hey Paul thanks for replying,

I have nothing setup for #2 because I havent the first idea where to start with that one.

As for #1 I have a random integer value generated at the start of the procedure and I name that variable as Random Pause and reference it repeatedly thereafter.

It seems to generate the random value the first time of asking and then just keeps returning the same value.

If I van figure out how to add image here I can share a screenshot of it, if thats helpful?

Many thanks.

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Variables and how to use them properly
« Reply #3 on: October 06, 2020, 10:23:28 AM »
You can right-click the action list and choose "Copy All as Text", then paste that into a code block (click the # button) here.

However, if you're setting an integer variable that's not going to work for the variable pause action as that takes a Decimal variable (as the dialog and the documentation mention).

In addition, if you reference the same variable multiple times without explicitly changing its value, each action will retrieve the exact same value.
You need to use the "Set a Decimal Value" each time you want to generate a different value; Variables just store a static piece of information, so when the "A random value" option sets the variable to a random value, the value it generates is stored in the variable, it doesn't mean the value is random each time you retrieve it.


For random keypresses, the simplest way would probably be to set a text variable to the output of the "{TXTRANDOM:}" token (note that as mentioned in the documentation, tokens output text; They are not variables).

Sim UK

  • Newbie
  • *
  • Posts: 12
Re: Variables and how to use them properly
« Reply #4 on: October 06, 2020, 10:44:59 AM »
On my phone ATM makes things harder, but ill check when on PC later for kmage post.

I do t follow your statement about how the integer value won't work for pauses?  To keep things simple I check the round it up value, so essentially just get a whole # between 3 - 20, what am I missing there?

Is there a way I can pass a random number each time requested?

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Variables and how to use them properly
« Reply #5 on: October 06, 2020, 10:51:26 AM »
An integer variable won't work for variable pauses. An integer number stored in a decimal variable will.

If you want a different random number, you need to set the variable to a different value.

Again, variables just store a piece of information, in this case a number. Unless you explicitly use an action to change that number, any time the value of the variable is requested, it will be whatever it was set to last.

To use an analogy: If you write a number on a piece of paper, and hand that same piece of paper to different people, they'll all see the same number.
If you want different people to see different numbers, you need to write down a different value before handing it to the next person.

Sim UK

  • Newbie
  • *
  • Posts: 12
Re: Variables and how to use them properly
« Reply #6 on: October 06, 2020, 11:06:55 AM »
From a PHP + Java background, I do understand the premise, but are you saying g there is no way to use that feature to auto generate a new random number between the max min and pass that as the variable (refresh/regenerated) each time it is called?

If not, do you know if this functionality exists in VC as standard or will I have to start writing code?

Many thanks.

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Variables and how to use them properly
« Reply #7 on: October 06, 2020, 11:24:07 AM »
Of course, but you need to actually generate the number and set the variable every time, E.G.
Code: [Select]
Set decimal [~pauseTime] value as random from 3. to 20. (round to 0 decimal places)
Pause a variable number of seconds [~pauseTime]
Set decimal [~pauseTime] value as random from 3. to 20. (round to 0 decimal places)
Pause a variable number of seconds [~pauseTime]
Set decimal [~pauseTime] value as random from 3. to 20. (round to 0 decimal places)
Pause a variable number of seconds [~pauseTime]

Something like
Code: [Select]
Set decimal [~pauseTime] value as random from 3. to 20. (round to 0 decimal places)
Pause a variable number of seconds [~pauseTime]
Pause a variable number of seconds [~pauseTime]
Pause a variable number of seconds [~pauseTime]
would mean each pause is the exact same length, as the value is only set once.

Sim UK

  • Newbie
  • *
  • Posts: 12
Re: Variables and how to use them properly
« Reply #8 on: October 06, 2020, 11:49:58 AM »
OK, so then O guess I am struggling to understand what value there is in using that tool?  If it creates a random value once and only once why use it?  Sure if you needed a single random value referenced multiple times this could make sense, but it seems like it was intended to do more??

Maybe I have over anticipated the capability of the random value generator.

I understand it is possible to write and ref your own code, is that a simple case of .txt file being called from within VA?  If it isn't I'm going to have to search through the manual, but thats not easy on a phone lol.

Thanks again.

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Variables and how to use them properly
« Reply #9 on: October 06, 2020, 12:10:12 PM »
As the variable pause action takes tokens, you can use the "{RANDOM:}" token to pass a value to it, without setting a variable at all.


VoiceAttack can natively compile and execute .NET code, written in either C# or VB.NET, using the inline function actions.
However, that would still require executing the inline function to set the variable value, in this case, so it would do nothing but add needless complexity.

Sim UK

  • Newbie
  • *
  • Posts: 12
Re: Variables and how to use them properly
« Reply #10 on: October 06, 2020, 12:30:43 PM »
I see. 

So would something like {RANDOM:20} be a working solution?  But of course that would not allow for the 3 -20 ideal.

Maybe it isn't as simple as I hoped.

Appreciate the help, thanks.


Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Variables and how to use them properly
« Reply #11 on: October 06, 2020, 12:35:14 PM »
Reading the documentation is recommended. The "{RANDOM:}" token accepts a from and a to value, as just about any randomizing function does.

Sim UK

  • Newbie
  • *
  • Posts: 12
Re: Variables and how to use them properly
« Reply #12 on: October 06, 2020, 01:01:43 PM »
Perfect, and to finalise that can be added to any query/value?

Because with respect, the manual isnt all that beginner friendly, once you understand the terms it will prob make more sense.

Tha ks again, you ha e helped tremendously.

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Variables and how to use them properly
« Reply #13 on: October 06, 2020, 01:15:15 PM »
Most fields accept tokens, but not all of them. If they do, the tooltip (and the documentation in VoiceAttackHelp.pdf) will mention that.

As the first paragraph of the "Text (and Text-To-Speech) Tokens" section of VoiceAttackHelp.pdf mentions, tokens are essentially functions that return text, so they can be used in most fields that accept literal text (even if that text is converted to a number internally, for example), where they can substitute either all or part of the input.

Tokens can also be nested (given that the "Use Nested Tokens" option on the "System / Advanced" tab of the VoiceAttack options window is enabled, which it is by default).

Sim UK

  • Newbie
  • *
  • Posts: 12
Re: Variables and how to use them properly
« Reply #14 on: October 06, 2020, 01:22:11 PM »
OK Great, I have a dire tion to proceed in.

Many thanks.