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

Help to implement custom dice roll in API

Hi, I'm new to roll20 and I really don't know what I'm doing, but I'm trying to implement a fairly odd dice roll that should be triggered on the character sheet. The dice roll is D6 against a set och success levels, one die per success level and each die can be matched against any of the success levels. Example: Succes levels: 4, 6, 6 equals 3D6 Results: 6, 5, 1 = 2 successes since 5&gt;4 and 6=6 Success levels: 4, 5, 6, 6 equals 4D6 Results: 6, 3, 2, 4 = 2 successes A friend crafted a javascript that solves the problem, but I don't know how to implement it in the character sheet. Here's the code:&nbsp; <a href="https://jsfiddle.net/uotq8ydj/7/" rel="nofollow">https://jsfiddle.net/uotq8ydj/7/</a> Is there a way to have a button on the character sheet that calls the function and returns a result to the chat? The success levels are located in an input field on the character sheet...
1600864283

Edited 1600864492
The Aaron
Roll20 Production Team
API Scripter
If I understand the mechanics you've outlined, you wouldn't be able to do something this dynamic from a character sheet, it would require an API script. Some questions: How are the success levels determined?&nbsp; Are they fixed before the roll or are they rolled?&nbsp; Do you want the dice to be rolled automatically?&nbsp; For example, an api command like !roll-against 4 6 6 which then results in 3d6 being rolled and compared? Are the success levels and dice rolls position dependent?&nbsp; For example, is this 1 success or 0 successes?&nbsp; &nbsp;6,6,1 vs 3,1,1
The success levels are entered in an input field in the character sheet, it's a trauma level for a weapon (just for context). Let's say the player entered 4,5,6 in the input field called trauma1 - then we need to roll 3D6 against those levels and match up the results. Not position dependent. I'd like there to be a button next to the input field where the players can "roll for trauma" if possible.
1600875520
The Aaron
Roll20 Production Team
API Scripter
I'm 99% sure this won't be possible with a Character Sheet.&nbsp; You'd need an API script to do the work.&nbsp; Character sheets can roll values, or they can do logic, but they can't roll values and do logic at the same time, except for a very narrow set of comparisons in the chat.
Yeah, I guessed that. But I don't know how to make a button on the character sheet send information to an API script, is there a way? All the examples are huge and the API scripts I've been looking at are really complex... I'd just want a button on the sheet that takes the values from the input field that holds the trauma levels and sends that off to an API script that does the logic and then posts the results in the chat. (sorry for late reply, kids and life happened). And thanks for all the work Aaron, I've seen you helping a lot of people in the forum and it really means a lot to us! You've already solved many of my problems in other answers here. *two enthusiastic thumbs up*
1600927450
The Aaron
Roll20 Production Team
API Scripter
No worries, kids and life abound! =D To call and api script from a character sheet, you just put the command in as the roll. &nbsp;Where you might have: [[1d8+@{str_mod}]] you instead have something like: !check-trauma @{trauma} Then the script takes over from there.&nbsp;
So, something like this? &lt;button name="roll_test" type="roll" value="!api_script_name @{values_to_send}"&gt;&lt;/button&gt;
To anyone else wondering how to pass data from character sheet to API through a button, this is what I found out with Aarons help: In the character sheet I have the following button, a roll that activates the !testbutton API and passes the @{willpower} attribute: &lt;button type="roll" name="roll_testbutton" value="!testbutton @{willpower}"&gt;Activate!&lt;/button&gt; In the API I have this script that passes the @{willpower} to the chat, you can ofcourse add powerful logic in the API: on('chat:message', function(msg) { &nbsp; &nbsp; if(msg.type == 'api' &amp;&amp; msg.content.indexOf('!testbutton') !== -1) { &nbsp; &nbsp; &nbsp; &nbsp; var willpower = msg.content.replace("!testbutton ", ""); &nbsp; &nbsp; &nbsp; &nbsp; sendChat("System", "/direct &lt;div&gt;Willpower is&lt;/div&gt; " + willpower); &nbsp; &nbsp; } });&nbsp; There is surely more clever and correct ways to do this, but this works for me and is a bit of an eye opener into how things work. I don't know jack about javascript but that's where google and roll20 forums come in.
1600950482
The Aaron
Roll20 Production Team
API Scripter
Nice!&nbsp; Looks like you're well on the way to getting a working solution. I threw together a quick script for you system.&nbsp; Hopefully it will be a good starting point for you.&nbsp; You call it like this: !trauma-check 4,3,1 or !trauma-check 1 5 2 It will roll as many d6 as there are target numbers, then count the successes: Code: on('ready',()=&gt;{ const traumaSuccesses = (t,r) =&gt; { let s = 0; let ts = [...t].sort((a,b)=&gt;b-a); let rs = [...r].sort(); ts.forEach(n=&gt;{ let i = rs.findIndex((v)=&gt;v&gt;n); if(-1 !== i){ ++s; rs = [...rs.slice(0,i),...rs.slice(i+1,rs.length)]; } }); return s; }; const roll = (number,sides) =&gt; [...Array(number)].map(()=&gt;randomInteger(sides)); on('chat:message',msg=&gt;{ if('api'===msg.type &amp;&amp; /^!check-trauma(?:\b\s|$)/i.test(msg.content) &amp;&amp; playerIsGM(msg.playerid)){ let who = (getObj('player',msg.playerid)||{get:()=&gt;'API'}).get('_displayname'); let trauma = msg.content .replace(/^!check-trauma(?:\s+|$)/i,'') .split(/[^\d]+/) .filter(t=&gt;t.length) .map((n)=&gt;parseInt(n)) ; let rolls = roll(trauma.length,6); let successes = traumaSuccesses(trauma,rolls); sendChat(who, `&lt;div&gt;&lt;div&gt;&lt;b&gt;Trauma:&lt;/b&gt; ${trauma.join(', ')}&lt;/div&gt;&lt;div&gt;&lt;b&gt;Rolls:&lt;/b&gt; ${rolls.join(', ')}&lt;/div&gt;&lt;div&gt;&lt;b&gt;Successes:&lt;/b&gt; ${successes}&lt;/div&gt;`); } }); });