Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

API Button to roll a dice

July 07 (5 years ago)
Richard @ Damery
Pro
API Scripter

I'm trying to add an API button to Roll Templates for spells so that a player can read the spell description, understand what dice need to be rolled and, if they desire, roll those dice by pressing the button (some people prefer to roll the physical dice).  Thus, I want a command something like this, which both provided the button if people want it, but also shows those rolling physical dice which dice to roll:

[Roll 1d4+4](~[[1d4+4]])

Clearly, that won't work as it stands as ~[[1d4+4]] cannot call an Attribute macro, but is there some way of getting the same functionality without having to create a new API (I'm not a programmer) or a macro for every possible dice roll?  There may well be something obvious that I'm missing, but I've searched for it and can't find it.

Or is there an API Script someone has created that I could use if I upgraded from Plus to Pro use?

July 07 (5 years ago)

Edited July 07 (5 years ago)
Oosh
Sheet Author
API Scripter

Without the API, you would have to create one or more macros. What system/character sheet are you using? You could go with one macro:

/r [[?{Number of dice?|1}]]?{Die type|d4|d6} + ?{Modifier?|4}

Just a brief example with limited options, but you get the idea. They can just copy the details from the button label.

Or you could go with one per die type, or one per modifier type, or one per spell. Depending on the system you're using it may also have templates available that have all those fields included for you to fill in to roll damage.

But yes, you do need to link the button to an actual macro.


I'm not sure what the API options are though, I'm sure there's something nifty available.

July 07 (5 years ago)
Finderski
Sheet Author
Compendium Curator

You can use an API Command Button like this:

[1d4+4](!
/r 1d4+4)

The html notation for a carriage return is necessary for this to work and if you open the macro after you save it, you'd have to replace it again, but...this will do what you want. I tried using an inline roll but the results were always weird.

July 07 (5 years ago)
Richard @ Damery
Pro
API Scripter

Finderski, that's brilliant!  Works just fine - thanks.  It's great to learn new stuff!

Finderski said:

You can use an API Command Button like this:

[1d4+4](!
/r 1d4+4)

The html notation for a carriage return is necessary for this to work and if you open the macro after you save it, you'd have to replace it again, but...this will do what you want. I tried using an inline roll but the results were always weird.




July 07 (5 years ago)
Oosh
Sheet Author
API Scripter

Huh, didn't know that worked! I was sure I tried that at some point. You're right about inline rolls though, they definitely break things.

July 07 (5 years ago)
Finderski
Sheet Author
Compendium Curator


Oosh said:

Huh, didn't know that worked! I was sure I tried that at some point. You're right about inline rolls though, they definitely break things.

I wasn't sure if it was going to work, I just know people had used it for doing some tricky stuff...that trick is what actually lead me to learning about Ability Command Buttons which I use a lot for character sheets. :)


July 07 (5 years ago)
Finderski
Sheet Author
Compendium Curator

By the way, you can get fancier, but you have to use html markup.  Here's the wiki page that explains a lot of this stuff: https://wiki.roll20.net/API:Chat#API_Command_Buttons

July 07 (5 years ago)
GiGs
Pro
Sheet Author
API Scripter

If you want to send dice of different types, you can combine Finderski's trick with a query:

[Roll ?{Roll String|1d6}](!
/r ?{Roll String})

This will prompt you to enter a dice string (1d6, 1d4+4, 3d6>4, 5d10kh2, or whatever), and it'll send that roll to the chat for the players.

July 08 (5 years ago)
Richard @ Damery
Pro
API Scripter

OK, all was going swimmingly and worked really well, until I tried to whisper a button using the 
 code in the button string.  Using such a code in normal chat seems absolutely fine, and works well.  However, as soon as you put "/w gm" in front of it, for example as

 /w gm [Example](!
/r 1d6)

You get an error.  Does not matter whether this is in the Chat Window, or in a macro, both error.  Of course, I'm not trying to use it as simply as that - I'm trying to /whisper a Roll Template containing API Buttons to a character via an Ability macro, so that the Roll Template with the clickable buttons only appears for that character and does not clog up everybody's chat windows.

Any other ideas, that don't involve creating an API Script?

July 08 (5 years ago)
keithcurtis
Forum Champion
Marketplace Creator
API Scripter

I don't think you can easily double up on slash commands like that. You need to escape the second slash so it doesn't try to interpret it right away. Try:

/w gm [Example](!
/gmroll 1d6)


July 08 (5 years ago)

Edited July 09 (5 years ago)
Richard @ Damery
Pro
API Scripter

Edit: Yep, all works fine now.  Thanks for your help, guys!

keithcurtis said:

I don't think you can easily double up on slash commands like that. You need to escape the second slash so it doesn't try to interpret it right away. Try:

/w gm [Example](!
/gmroll 1d6)



Hmm.  Promising!  Certainly that test worked.  Now to try it inside a whispered Roll Template API Button.  More updates later...