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

Want to learn coding: programming an ApplyDamage button

Hi! I am new to Roll20 (playing DnD5e) and new to javascript as well. But I can learn best if I have a specific task in mind .... I have added actions for my NPCs/creatures for attacking (I think like those added with TokenAction), plus a reference to a target. Now, if my creatures attack  player it is written into the journal (attack rolls, damage, and target of attack). What I want to have is a script that captures such a message and adds a button below that message, 'apply damage'. I am well aware of dozens of cases where such damage shouldn't be applied automatically, but most combats are standard combats. So I am satisfied to 'manually' decide if the situation is a standard situation and save a lot of time when applying the damage to the targeted player. It would be great if players could hit such a button also, so that I don't have to change hps of my monsters by myself. I really think this could save a lot of time. I am looking through the codes of applydamage.js for example, and I think I can figure out how to do this from here, _if_ the functionality of such a button is possible at all. So my question is: Is this possible or are there any severe problems to solve? In the forum this idea has been mentioned several times, but I didn't find a finished script :-) Best  regards, Norman
I am not sure you would need to write a new script. There are already scripts that can do some of what you wish, such as chatsetattr and TokenMod. Since you are creating macros you could create a global macro and tie it to an ability button in your macro to put into chat. 
True, and I use those scripts already and had planed to use them exactly as you suggest. ;-) But I want to have the button, or something similar, enabling me to choose between easy hitpoints reduction or not. I don't have a clue, how to 'feed' the button function with the correct information (target and damage). Maybe this is much easier than I thought ..?  I need to register an event, looking for some specific words in the chat, extract the needed information, then set up the button calling TokenMod with those information and I am done. That is the way to accomplish my little project, right?
I&nbsp; am not sure about how to do it in an API directly, but if you simply calling another api command you can put that into button via [label](ability/macro)&nbsp;&nbsp; <a href="https://roll20.zendesk.com/hc/en-us/articles/360037256754-API-Chat#API:Chat-sendChat(speakingAs,input[,callback[,options]])" rel="nofollow">https://roll20.zendesk.com/hc/en-us/articles/360037256754-API-Chat#API:Chat-sendChat(speakingAs,input[,callback[,options]])</a> &nbsp;and&nbsp;<a href="https://roll20.zendesk.com/hc/en-us/articles/360037256794#Macros-MoreExamples" rel="nofollow">https://roll20.zendesk.com/hc/en-us/articles/360037256794#Macros-MoreExamples</a>
1588627321
The Aaron
Roll20 Production Team
API Scripter
So, there are a couple pieces you need to handle in the above, none of them too difficult. First off, the capturing of the message.&nbsp; You just need a handler for the "chat:message" event which then outputs your message.&nbsp; Here's a very simple example that responds to someone mentioning the word "taco": on('ready',()=&gt;{ on('chat:message', msg =&gt; { if(msg.playerid !== 'api' &amp;&amp; /taco/i.test(msg.content)){ sendChat('API',`(Did someone mention tacos?)`); } }); }); One thing to be aware of: you can very easily end up in an infinite loop if you aren't careful.&nbsp; If you take out the check against the playerid being the API, this will result in an infinite number of chat messages about mentioning tacos.&nbsp; If you get in that situation, disable your script then go clear your chat log outside the game. Secondly, outputting a button.&nbsp; This is really straight forward and well documented here:&nbsp; <a href="https://wiki.roll20.net/API:Chat#API_Command_Buttons" rel="nofollow">https://wiki.roll20.net/API:Chat#API_Command_Buttons</a> Basically, you're just outputting either a Markdown format button: sendChat('Do a thing',`[Do it](!do-something with args)`); Or an HTML anchor tag with the href set to the command: sendChat('Do a thing',`&lt;a href="!do-something with args"&gt;Do it&lt;/a&gt;`); The benefit of a HTML anchor tag is the ability to style it as something other than a big pink button.
Uh, looks much easier than I thought. Must try it immediately .... after taking a good nights sleep first :-) Thank you!
The scriptomancer to the rescue...lol
@Norman V.&nbsp; You can also check this script that @Jacob did some time ago:&nbsp;&nbsp; <a href="https://app.roll20.net/forum/post/3631602/script-groupcheck-roll-checks-saves-et-cetera-for-many-tokens-at-once/?pageforid=4359153#post-4359153" rel="nofollow">https://app.roll20.net/forum/post/3631602/script-groupcheck-roll-checks-saves-et-cetera-for-many-tokens-at-once/?pageforid=4359153#post-4359153</a> .
Thank you, Mark W., that was the post I had seen a while ago. Before I thought about scripting at all :-) Didn't find it again, though ... I wasn't sure it was almost exactly what I am looking for now :-) So, now I will try coding my button :-)
1588677051

Edited 1588689362
There's one thing (at least for now :-) ) I don't understand: I extract the different kinds of damage from the rolltemplate-message, adding dmg1 and dmg2, and also add the crits damage, but is there any flag to decide if that was a crit at all? Is this information not included in the message.content? Or am I just blind to see it? I began to circumvent this by evaluating the original rolls, but can't finish it right now.
Hi Norman. Did you happen to make any progress on this at all? It seems a rather useful feature.
Hi Ryan G.! Yes, I did make some progress. Actually up to that it is useful for me :-) The next step would require much more work .... and maybe it's not realistic to achieve at all: There are so many special conditions to consider. But still, I can use the "apply damage" button in about 75% of all times, although sometimes the damage is calculated wrong. I think it has to do with 2nd kind of damage and the way I set it up (e.g. sneak damage). But I can live with that; it still saves time! When I am returning to my computer I hope to remember to send the code. But it is mainly based on code from other people. I also changed ApplyDamage.js in some minor ways. I can't create a list of changes out of my head now, but I think it was only for displaying reasons. By the way: Thanks for all the input, players!
You might also get some use out of this script I wrote awhile back:&nbsp; <a href="https://app.roll20.net/forum/post/8469386/script-5e-automatic-attack-script-compares-roll-to-target-ac-and-deducts-damage/?pageforid=8469386#post-8469386" rel="nofollow">https://app.roll20.net/forum/post/8469386/script-5e-automatic-attack-script-compares-roll-to-target-ac-and-deducts-damage/?pageforid=8469386#post-8469386</a> .&nbsp; No "apply damage" button, it just automatically subtracts. If nothing else it might give you some ideas,&nbsp;
Thank you for this useful info.