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

Need a little help with an API that I have (Hopefully) mostly worked out.

1519894368

Edited 1520100559
Update: - This has been resolved, can't adjust the title so I'll update that here. Thank you everyone :) I think, and am hoping, that I've gotten most of this worked out myself. But I only half understand this. In short, I'm trying to set up a api for calculating damage vs the target's armor. However, the target's armor may vary based on where you're hitting. So I set it up like this in a macro at first. /roll 1d20 + @{selected|Damage} - @{target|?{Hit Location|Head|Torso|Arms|Legs|Tail|Wings|Other}Armor} This didn't work. The output was correct, it sent "rolling 1d20 + 10 - @{target|HeadArmor}" into the chat. But it never asked for a target and all it did was roll the d20, it ignored the rest despite saying it rolled it. After poking it a little, I believe this is calculating the roll before getting the input. So my next idea was for it to get the input, then send it over to the API, and have the API just echo the full expression back and roll it. And that's where I'm stuck. I put this in the macro !Rattack /roll 1d20 + @{selected|Damage} - @{target|?{Hit Location|Head|Torso|Arms|Legs|Tail|Wings|Other}Armor} And then I put this in the API (Along with a few lines borrowed from someone else's script to identify the !Rattack, that part seems to work. Realism.Attack = function(msg, who) { sendChat(msg.who, "msg.content"); } When I run this, it just sends "msg.content" in the chat. So I assume that is not the right way to echo the message back. What do I replace "msg.content" with? I can't seem to find that on google. I don't need anything complex, I only half understand what I'm doing so I might not be able to work with anything too complex. Thanks in advance for your time. :)
You're sending the literal text "msg.content" to the chat. Remove the quotes around msg.content and see if it works then.
1519919830

Edited 1519923691
Oh! So the quotes are not necessary, thanks :) Although removing the quotes causes an error. Is msg.content the correct thing to have there? TypeError: Cannot read property 'get' of undefined ------------------- Update: Still tinkering with it. I've made progress. I changed the API to this: Realism.Attack = function(msg, who) { var Rchat = msg.content.split(" ", 2)[1]; sendChat('Example', "/Roll 1d20 + " + Rchat); } And this is properly sending the correct roll command in the chat! But it's only rolling the 1d20, not the rest of the statement. Much closer to what I'm looking for, but I'm still stuck because it's not rolling the full statement like I was hoping it would.
1519930606
GiGs
Pro
Sheet Author
API Scripter
The way I would do this is to have the script build your output text. First, build it to be triggered without the roll built in, just grab the variables: Instead of !Rattack /roll 1d20 + @{selected|Damage} - @{target|?{Hit Location|Head|Torso|Arms|Legs|Tail|Wings|Other}Armor} use !Rattack @{selected|Damage} @{Hit Location|Head|Torso|Arms|Legs|Tail|Wings|Other} Then the script would be something like (untested since i dont have your character sheet):  on("chat:message", function (msg) {         if (msg.type === "api" && msg.content.split(" ")[0] === "!Rattack")         {             let args = msg.content.split(" ");             let damage = args[1]; let location = args[2];             let roll = "/roll 1d20+" + damage + "-@{target|" + location + "}";             sendChat(msg.who, roll);         }     });
1519934974

Edited 1519935269
Awesome, thank you. That works and outputs what I need it to. However I now face another roadblock. It properly sends the roll to the chat and I get "rolling 1d20+10-@{target|Head}" But then it only rolls the 1d20 and ignores the rest. Which has me a little stumped because if I copy/paste the roll into chat again it rolls it all. It would seem it's not reading the @{target|Head} part and that's breaking the expression.
1519938986
GiGs
Pro
Sheet Author
API Scripter
Rolls send from scripts do seem a little finicky to be honest. When you say it ignores the rest, does it add the 10? Here's a couple of things to try: First, try replacing "-@{target|" + location + "}" with  "-[[@{target|" + location + "}]]" If that doesnt work, let's test if it is the location that is causing issues. Try changing this line let roll = "/roll 1d20+" + damage + "-@{target|" + location + "}"; to let roll = "/roll 1d20+" + damage; and see if the damage gets added properly. If all else fails, it is possible to grab the value directly in the script itself, but it'll need a bit of work.
It does not add the 10, it only rolls the 1d20. The first idea did not work, but when I reduced it to the "/roll 1d20+" + damage;" like in your second suggestion, then it rolled the dice and added the damage. Would it be possible to send the value for the target's attribute directly to the API and just subtract that? I'm not sure what I'm doing and am just guessing at solutions.
1520018971

Edited 1520019858
GiGs
Pro
Sheet Author
API Scripter
Yes, you can get the id directly. Try this: Change how you launch it to !Rattack @{selected|Damage} @{target|character_id} ?{Hit Location|Head|Torso|Arms|Legs|Tail|Wings|Other} Change the script to  on("chat:message", function (msg) {         if (msg.type === "api" && msg.content.split(" ")[0] === "!Rattack")         {             let args = msg.content.split(/\s+/);             let damage = parseInt(args[1])||0; let victim = args[2]; // this should now be the character id let location = args[3].trim(); let armor = parseInt(getAttrByName(victim, location, 'current'))||0;             let roll = "/roll 1d20+" + damage + "-" + armor;             sendChat(msg.who, roll);         }     }); Note: i have tested this and it works for me.
1520024280
The Aaron
Pro
API Scripter
For the record, the reason that sending @{target} to the chat doesn't work is because the "player" that the @{target} is directed to is the API, not the person executing the API script.  That's because the sendChat() occurs on the API server, not the player's client browser.  All the data you want to work with needs to be sent in the initial command (as you did above) or collectable via api commands (like findObjs() calls).
1520024413
GiGs
Pro
Sheet Author
API Scripter
Ah, of course. I wondered why that wasn't working.
That works, thank you very much. Sorry for the slow responses, I'm in Massachusetts, in the middle of that massive storm. The whole city's out of power with no hope of it returning for a few days. :( @Aaron. Oh, that makes sense. I never would've thought of that
1520106064
The Aaron
Pro
API Scripter
No worries, I drove out of Cleveland just ahead of that storm Thursday!  Most of the rest of my team (the ones that flew) got stranded there for an extra few days.  One guy got routed through Houston on his way back to Calgary. =D