Author Topic: Generate a random integer between 1 and a given number  (Read 956 times)

Tooterbooter

  • Newbie
  • *
  • Posts: 3
  • I use VoiceAttack for VR games.
Generate a random integer between 1 and a given number
« on: December 31, 2021, 07:51:21 PM »
I'm fairly new to voice attack, so I'm having trouble writing up commands.
My goal for this command is to be able to roll a die like in DND, except I say how many sides the die has.
For example, if I say "Roll a D20" I will hear a text-to-speech bot say a random number from 1 to 20, or if I for some reason need to say something like "Roll a D34" then I will hear a number between 1 and 34. I won't ever need to use this over 100.
I know I could just look up a random number generator, but my goal for this is to be able to use it quickly on command while in VR, and it's difficult to access the desktop and keyboard with a headset on.
-Tooter

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Generate a random integer between 1 and a given number
« Reply #1 on: December 31, 2021, 07:56:39 PM »
Have you read through the documentation? Press F1 while VoiceAttack has focus to open VoiceAttackHelp.pdf in your default PDF viewer, which contains information on VoiceAttack's features.


Generating a random number can be done using the "{RANDOM:}" token, which you can combine with dynamic command sections and the "{CMDSEGMENT:}" token (or the "{CMD}" and "{TXTNUM:}" tokens), to generate a number within a given range.

If you'd like to be able to recall the rolled value, you'd want to set an integer variable using the aforementioned tokens, and retrieve that value using the "{INT:}" token for use in the "Say Something with Text-To-Speech" action.

Tooterbooter

  • Newbie
  • *
  • Posts: 3
  • I use VoiceAttack for VR games.
Re: Generate a random integer between 1 and a given number
« Reply #2 on: December 31, 2021, 08:38:25 PM »
Thank you so much! This is what I managed to get working. Let me know if I did something cursed and there's a better way I should have done this.
-Tooter

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Generate a random integer between 1 and a given number
« Reply #3 on: December 31, 2021, 08:47:40 PM »
That's pretty much it, exactly.


As you may have already surmised, variables are globally-scoped by default, meaning all commands within the same session can access them, so if you want to have a "what did I last roll"-style command, all you need to do is retrieve that same variable value using the token you're already using in the "Write a Value to the Event Log" action.

If you also want to have access to the value you spoke initially to know which die you rolled, as you're doing in the "Say Something with Text-To-Speech" action, you'd need to store that in a variable as well.
The information retrieved by the "{CMDSEGMENT:}" token pertains to the command it's used in, so without storing it in a variable that value would not be accessible to other commands.