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

Trouble with adding more then 2 variable values?

Is there some special way I need to write out the math when combining more then 2 numbers in a series? I am declaring a variable to be the sum of 8 variables that are already defined. However, when I log the new variable I get a return of the values as a series rather then a sum. When take out all but 2 variables (the first is actually just a integer) I get an actual sum in the log. Here is the example I am using var targAC = 10 + targDexMod + targACdodge + targACnatural + targACdeflect + targACmisc + targACtemp; when I log this I get 1100000. I thought this was because they where default values, but changing them to and number on the sheet still return 1100000.
1400914715

Edited 1400915158
Lithl
Pro
Sheet Author
API Scripter
Assuming targDexMod is 1 in your example, it looks like targACdodge (and possibly the values after it) are strings, not integers. This is what we get for having a weak-typed language to work with, particularly when we have string-input fields for our characters. =) The interpreter sees 10 + 1 + "0" + ... so it says 10 + 1 = 11, 11 + "0" = "110", "110" + "0" = "1100", etc. There are a few ways to force a variable to get treated as an integer. The shortest to write is the identity operator: +targACdodge . While it's the shortest, it's the more error-prone when converting from strings (getting NaN and such instead of what you intended), and it also makes it terribly difficult to read when you've got a series of summed values. (I often use this when throwing together scripts on-the-spot in the forums, specifically because it's fast to write.) The fastest to process is bitwise OR with 0: targACdodge | 0 . It still suffers the problem of being difficult to read in a series of sums, though, and it's difficult to understand what's going on for someone unfamiliar with bitwise arithmetic. The most robust option is the parseInt function: parseInt(targACdodge) . You can also supply a second argument as a radix, but it's unlikely someone has entered the target's AC in binary or hexadecimal, so you probably don't need it. parseInt works as you expect it to on the widest range of strings. In JavaScript, this is called type coersion (particularly the first two; the third option would usually be called string parsing) . So, you could use any of: var targAC = 10 + +targDexMod + +targACdodge + +targACnatural + +targACdeflect + +targACmisc + +targACtemp; var targAC = 10 + targDexMod|0 + targACdodge|0 + targACnatural|0 + targACdeflect|0 + targACmisc|0 + targACtemp|0; var targAC = 10 + parseInt(targDexMod) + parseInt(targACdodge) + parseInt(targACnatural) + parseInt(targACdeflect) + parseInt(targACmisc) + parseInt(targACtemp); Assuming the other variables are strings-as-numbers with nothing special going on, all of those options will work the same. The second one will be a very teensy tiny bit faster than the other two, but the difference is completely negligible. Of course, you could also parse the string as an integer when you first assign the targAC variables, rather than at the point you sum them together. It's up to you.
so by using parseInt it is forcing the strings to be read as an integer? Would expression that expression look like: parseInt(getAttrByName(whoTarg.id,'armor-acbonus')) This would be best in my opinion since I cant set the varible to just be getAttrByName(whoTarg.id,'AC') since @{AC} is an autocalc of the sum of these attributes. I would have to declare them in variables regardless and express the formula for @{ac} in the variable targAC.
Brilliant, but @{size} is having the opposite effect of what it sound be. In this cause -1 actually adds 1 to the overall AC and a 1 should lower the overall AC. Is there a simple way to reverse this? I can already see it being done with an if statement that checks the number and just either removes or add the -, but that seems unnecessarily complex.
1400927333
Sam M.
Pro
Sheet Author
Multiply it by -1 dude.
Danke.