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 Help] Making calls to the API from a character sheet roll button that includes an Attribute

September 14 (9 years ago)

Edited September 15 (9 years ago)
Chris D.
Pro
Sheet Author
API Scripter
Compendium Curator
I am not certain if I should be posting this here or in the API section, but ...
Something weird is happening whenever I include an Attribute in a roll button.

First some background. 
I am trying to make an Earthdawn Character sheet, and Earthdawn uses a weird step system, where you add and subtract various numbers until you come up with a step number, then you look on a table to see which dice that is, and you roll those dice. For example if your Dex is step 5, your melee skill is step 2, and you have one serious wound, your attack step is 5+2-1 = 6, you look on a table and see that is 1d10!. If next round you have two serious wounds, your attack step is step 5 = 1d8!. This system is a perfect candidate for being computerized to make it easier to use.
So I have an API routine that looks for a certain string in the chat window, and if it sees it, looks at the step number that follows that string, converts it to dice, rolls the dice and displays the results. Simple, no Problem.

My problem is that I can't get it to work on the character sheet. 
If I just hardcode a number onto the button, everything works fine. For example
button name="roll_Dex-test" type="roll" value="!edsdr~ 5"> test
the API sees and correctly processes 
{"content":"!edsdr~ 5","playerid":"-Juqd6bJVF43cGrbxX1d","type":"api","who":"Chris D. (GM)"}
Once again, the API sees it and correctly processes it.

However if I try something such as
button name="roll_Dex-test" type="roll" value="!edsdr~ @{Dex}"> test

*** (edited to remove some misleading stuff. See next post for what I should be seeing) ***

  Thanks.

September 14 (9 years ago)

Edited September 15 (9 years ago)
Chris D.
Pro
Sheet Author
API Scripter
Compendium Curator
OK, I think I finally have it.  I am posting the solution in case it helps the next person with a similar situation. 

I forgot to mention that the @{Dex} in the above code was an auto-calculated field. Specifically, it is
input name="attr_Dex" type="number" disabled value="@{Dex-Step} + @{Dex-Mods} + @{Adjust-All-Tests-Total}" title="@{Dex} Dex step - Modified for Wounds and Conditions."
After fixing a stupid bug, this is what the API is seeing in the "value" for @{Dex}
floor((10 + 3 - 1)/3)+2 + 2 + 0 + 0 - ( 0 + 0 + 0 + 0 + 0 + 0 + 0 )
Which seems to be made up of a chain of every calculated value that makes up this field (ie: the first bit seems to be @{Dex-Step}, the +2 is @{Dex-Mods} and the rest is @{Adjust-All-Tests-Total}

So while it would be nice if the roll button did the calculation for me, I Do have all the numbers right there in numeric form so I can simply have the API call the Eval() function and sum them up for me.

(edit) Note, in order to get it to work, I actually had to run add a string function  
        replace( " floor(", " Math.floor(")
to change the floor to Math.floor
(end edit).

Alternatively, I experimented and found that if I use [[@{Dex}]] it will treat it as an inline roll and I can simply parse it and ask for the result. Of the two, having it send the raw values and my calling the Eval function seems the simplest. 

If anybody knows a way to have it just evaluated without any other extra steps I would appreciate hearing from you.

Thanks!
September 15 (9 years ago)
Lithl
Pro
Sheet Author
API Scripter
  1. Autocalc fields are not calculated until they're part of a roll or displayed on the character sheet. Until then, the attributes than make them up are basically just copied and pasted, which is why the value of @{Dex} that the API sees is an equation.
  2. Judging from "14 + $[[0]] + $[[1]] + $[[2]]", several of the attributes that make up @{Dex} are written as having inline rolls; you really don't need to do that in the attributes. Save the inline roll brackets for actual rolls.
  3. I strongly strongly recommend against using eval(). If you send [[@{Dex}]] in your roll, you can pull out msg.inlinerolls[0].total to get the calculated value for @{Dex}.
September 15 (9 years ago)
Chris D.
Pro
Sheet Author
API Scripter
Compendium Curator
At first I felt it was easier to just call eval() on the passed step number since much of the time (when called by a macro, not by a button on the roll sheet) the number is just a number, not a formula. It seemed like a pain to test to see if there were any inline rolls and then parse the results, etc. Then I found the code in this thread
https://app.roll20.net/forum/post/1416670/help-get...
which is a handy dandy API routine that does exactly that. 

So the final answer if you have a calculated value you want to pass to the API, just wrap it in [[]] and then call the code in the listed thread to replace all instances of $[[x]] with their results. No problem.

Thanks