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 Request/Help] Automatically subtract health on a successful hit

So I should preface this by saying that I've done some minor code before but nothing as ambitious as what I would like to try here. So any use of laymans terms earns you bonus EXP. I want to write a script that will register when a hit succeeds vs an AC (in the way macros function on the 5e shaped sheet) and, if the hit succeeds (or more ambitiously) if the save fails, then HP is automatically reduced on both the sheet and the token that was targeted with the attack.  Does anyone have any idea how to make this happen? I know its ambitious and I'm starting to wonder if it simply can't be done, but I'd like some input from this community before I give up (before starting).
1483730962
The Aaron
Pro
API Scripter
It can certainly be done, but biggest question is what you want the process like, and how much input you want people to have at which stages.  I think the hardest part of most scripts is the interface, if you can nail that, the script writing becomes much easier. At it's simplest, it sounds like an outline would be: Player executes command, passing the attack roll, target, and damage. API loads the target API compares the attack roll to the target's AC On Success, deduct damage from target's HP On Failure, do nothing. So, the command might look like (Roll Queries for simplicity): !apply-attack ?{Roll} @{target|token_id} ?{Damage} on the API side, it would look something like (pseudocode): /* on chat message */ let token = getObj('graphic', <targetid> ); let character = getObj('character',token.get('represents')); if(character){ let ac = getattrbyname(character.id,'AC'); if(ac < <roll> ){ let hp = findObjs({ type: 'attribute', name: 'HP', characterid: character.id})[0]; if(hp){ hp.set('value', (hp.get('value') - <damage> ) ); } } } Not too complicated.  Complication comes from wanting to provide feedback about what happened, wanting to choose different stats to check an apply to, wanting the DM to be able to override, etc.  That's really all interface stuff though, the actual code is still pretty simple. Hope that helps, and of course feel free to ask more questions!!
This can be done, and for the most part, is not very hard to accomplish. However I would refrain from having one master function for attacks (against AC) and spells (against saving throws) as these are two fundamentally different approaches to damage calculation. While working on this I would work on this in segments. First make a function that will deal damage (or heal or give temporary hit points [if you are in 5e]). You could pass in the token_ids for the target(s) of the damage and update the health for those tokens (this is a very good reason to make sure that all tokens use the same bar for hp). I have a script that allows me to enter !modHealth --sel --damage 5 and reduce the bar1 value of all the selected tokens by 5 points.
Quick replies! YAY! Thank you both so much, I'll have to start delving into this and hope that I can maintain sanity while doing so! :D
1483731550
The Aaron
Pro
API Scripter
Rob B. said: ...and hope that I can maintain sanity while doing so! :D No promisies!!  =D