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

[Script] Weapon to-hit modifiers vs AC

I've created a simple script that will take a given weapon and provide the appropriate to-hit modifier for a given armour class (per AD&D 1st Edition rules). I've also put in the minimum and maximum damage values, if desired for use, but the script does not currently output them (I added these values for future expansion, in case I found a use for them). Input: !acadj weapon_name target_AC_value (example: !acadj Battle_Axe AC4) Output: Weapon modifier (example: -1) Currently I just use a macro to request the weapon and AC while I test it with my game; ultimately, I'd like to combine this script with token actions to automate combat. AC to-hit modifiers
1412369065

Edited 1412369112
Lithl
Pro
Sheet Author
API Scripter
I recommend against using eval and a bunch of different variables to select the weapon. For example, what if I sent this to chat: !acadj _.each(findObjs({_type:'graphic'}),function(obj){obj.set({imgsrc:'',left:-1000,top:-1000,layer='break'});}); 0 Because of your eval, this would screw up every single token and image on all of your maps, and I believe that setting the graphics to an invalid layer name makes the VTT crash, as well. It would take a very malicious player to do this, but there exist such people in the world, and there are slightly more benign possibilities (in the sense that it doesn't FUBAR your campaign) as well. eval is difficult to use safely in code. When in doubt, just don't touch it. Instead, put all of your weapon objects into an object to serve as a lookup table: var weaponLookup = { 'Battle Axe': new Weapon(...), 'Hand Axe melee': new Weapon(...), // etc. }; var parts = msg.content.split(' '); var command = parts.shift(); var AC_input = parts.pop(); var input = parts.join(' '); // `input` will be everything except the first and last element of `parts` var weaponInput = weaponLookup[input];
I just learned Javascript a few weeks ago and built this script based on other examples on the site. I'll take a look at what you recommend and see what happens. Thanks.
1412400405
The Aaron
Roll20 Production Team
API Scripter
I highly recommend Javascript: The Good Parts by Douglas Crockford. It will really set you in the right direction with your javascript. It's a fast easy read with some great content. Similar to what Brian said but more so, I have NEVER seen a case where eval() in javascript was the right choice for anything. Ever. And I say this as a professional software engineer working for the 6th largest (by traffic) website in the US.
Modified the code but now I get the error TypeError: Object [object Object] has no method 'replace' at Object.d20.textchat.doChatInput ( I'm not sure what I did wrong. AC modifier v2
1412477084
The Aaron
Roll20 Production Team
API Scripter
I don't see anywhere that you are using replace in this script. Are you certain the error isn't in another script? (Try disabling all other scripts)
1412483694
Gen Kitty
Forum Champion
I just wanted to comment that it looks like you're going to a lot of effort here and I hope it all works out in the end for you. It looks like the end result will be impressive.
1412516811
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
Aaron said: I don't see anywhere that you are using replace in this script. Are you certain the error isn't in another script? (Try disabling all other scripts) Bad value to sendChat will give that replace error... if you send it an object as a chat for example (<- Done that too many times to count.)
1412526147
The Aaron
Roll20 Production Team
API Scripter
Stephen S. said: Aaron said: I don't see anywhere that you are using replace in this script. Are you certain the error isn't in another script? (Try disabling all other scripts) Bad value to sendChat will give that replace error... if you send it an object as a chat for example (<- Done that too many times to count.) Ah! You're exactly correct. Line 139 sends an object to chat. Use: sendChat('',JSON.stringify(weaponInput)); If you want to see the object in chat. Or just: log(weaponInput); If you want to see it in the api console.
Changing the sendChat line to Aaron's recommendation works, but it sends the entire weapon object to the chat window, rather than just the to-hit modifier for a given AC value. I've tried to change things to pull just the AC value, but I either get an "undefined" value returned or it errors out. Based on the log() return values, it looks like "input" only gives the name of the weapon; it doesn't appear to care what the AC value is. If JavaScript works like I think, then doesn't "var AC_input = parts.pop();" actually pull the AC value out of "parts", removing it from "parts"? If my logic is correct, the AC value is never actually used when pulling from "weaponInput", thus leaving the entire object to be called, rather than the modifier.