These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Calculating ability stats

Duniagdra
Registered: Sep 24 2008
Posts: 140
Answered

Is it possible for acrobat to reference a data table and get a value? Let me explain:

Score=Modifier

1=-5
2-3=-4
4-5=-3
6-7=-2
8-9=-1
10-11=0
12-13=+1
14-15=+2
16-17=+3
18-19=+4

What I'm looking to do is try and automate a character sheet in acrobat. I have six fields named STR, CON, DEX, INT, WIS, and CHA. Each of these fields have a corresponding bonus field STR_Bonus, etc, etc... Unfortunately, I'm not smart enough to come up with a formula capable of providing the correct bonus based on the entered score. The score can go to any height, but realistically could be capped at 50.

I'm hoping that there could be some way that a data field/table field could be created that the STR_bonus field would reference to look up the bonus gained from the user entered Ability Score entered in the STR field.

I'm creating my forms in LiveCycle, but I also have Acrobat Pro that I can use to insert scripts.

Thanks in advance for any help provided,
Jim
The All Time Professional Amateur Hack

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Yes, you can do that. Probably the simplest approach given your situation is to use a JavaScript array:

// Set up JavaScript array to associate score value
// with a modifier value
var modifier_array = [0, -5, -4, -4, -3, -3, -2, -2, -1, -1, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4];

Now, to look up the modifier given the score, you could do something like:

var modifier = modifier_array[score];

Note that I set the vale of the first item of the array to zero. The first item of an array has an index of zero, but since you did not list a score of zero in your table above, I just gave it a dummy value. If a score of zero is a valid score, you'd use whatever modifier value is appropriate.

George
Duniagdra
Registered: Sep 24 2008
Posts: 140
My apologies George,

I'm not all that savvy on javascript. Do I just add

var modifier_array = [0, -5, -4, -4, -3, -3, -2, -2, -1, -1, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4];
var modifier = modifier_array[score];

to calculations section of the field properties? How does the script know that a score of 18 or 19 get a bonus of 4 and a score of 4 or 5 gets a bonus of -3. No there is no valid score of 0 so your dummy score does work. I'm also assuming I add this to each ability mod field too, right? Is there a limit to how many bonus results I can permit?

Also does it matter if I do this in Acrobat Pro or LiveCycle? For some reason, I seem to be having trouble getting access to the field properties in Pro. It keeps sending me to LiveCycle.

Edit: I just tried adding it to LiveCycle and nothing happened. I need a getField variable, don't I?

Sorry for all the questions.

Jim

Thanks in advance for any help provided,
Jim
The All Time Professional Amateur Hack

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
I was suggesting a general approach. The script you use in the calculated field would have to first get the value of its associated Score field, use the array to look up the corresponding modifier value, and set the value of the calculated field equal to the modifier value that was retrieved from the array.

If you were to create the form in Acrobat, the custom calculation script for the STR_bonus field could be something like:

// Set up JavaScript array to associate score value
// with a modifier value
var mod_array = [0, -5, -4, -4, -3, -3, -2, -2, -1, -1, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4];

// Get field value of STR field
var v1 = getField("STR").value;

// Get corresponding modifier value
var mod = mod_array[v1];

// If the corresponding modifier value exists...
if (typeof mod != "undefined") {

// ... set the value of this field equal to the modifier value
event.value = mod;

} else {

// Otherwise, set the field value to nothing
event.value = "";

}

This is not necessarily the best way to code this, but is intended to demonstrate the general approach. If you end up going with Acrobat, feel free to post again for more refined code. An important addition will be code to validate the score entries. That means making sure the entry is a number, that it is within the allowable range, etc.

You can have an unlimited number of bonus result values.

For a script you can use in LiveCycle Designer, perhaps someone else will be able to help. Other than your difficulty with Acrobat, is there a reason you want to do this with Designer?

George
Duniagdra
Registered: Sep 24 2008
Posts: 140
George_Johnson wrote:
For a script you can use in LiveCycle Designer, perhaps someone else will be able to help. Other than your difficulty with Acrobat, is there a reason you want to do this with Designer?George
Honestly,

My only reason for using designer is that the original document was "art (something-or-other)" as acrobat expressed when I wanted to create fields. Anytime I try opening it in acrobat pro and try to edit the field properties, I'm unable to.

I'm finding now that it was how I originally created the the form sheet. I'm now going through the process of RECREATING ALL my form fields (there's almost 200) through Acrobat. This will enable me to edit the field properties, since it's clear to me now that once a form is created in Designer, Acrobat is unable to do anything further, unless I'm just doing something wrong (this would not surprise me).

I'll be looking for your refined code greatfully.

Thanks for your time and help.

Thanks in advance for any help provided,
Jim
The All Time Professional Amateur Hack

Duniagdra
Registered: Sep 24 2008
Posts: 140
Since last posting, I'm sitting here thinking that I need to wait for you to give some other code when in fact the code I need is right there. LOL

Just pasted it into one of the fields and it works great. Thanks George.

Anytime you're able to get a more refined code up, at your own leisure of course, I'll be all the same grateful for your assistance in this.

In fact, I'm finding with a bit of tweaking, I'm able to use this for other fields that show a result based on the value entered in a related field. Cool.

Thanks in advance for any help provided,
Jim
The All Time Professional Amateur Hack

Alysande
Registered: Oct 2 2008
Posts: 7
I am doing the same thing (automating a character sheet) and I found a much simpler way of working out the ability mods from the ability scores. The code I used is:

event.value = Math.floor(( this.getField("").value - 10 ) / 2 )where is the name of the score field.ie for Strength:

event.value = Math.floor(( this.getField("STR").value - 10 ) / 2 )

I found that Math.floor(x) is a great object for use in character sheets as it rounds down to the nearest integer, which is exactly what happens for everything in D&D.Up to you if you want to use it, just thought you might want to make it simple. :D
Duniagdra
Registered: Sep 24 2008
Posts: 140
Alysande wrote:
I am doing the same thing (automating a character sheet) and I found a much simpler way of working out the ability mods from the ability scores. The code I used is:event.value = Math.floor(( this.getField("").value - 10 ) / 2 )where is the name of the score field.ie for Strength:

event.value = Math.floor(( this.getField("STR").value - 10 ) / 2 )

I found that Math.floor(x) is a great object for use in character sheets as it rounds down to the nearest integer, which is exactly what happens for everything in D&D.Up to you if you want to use it, just thought you might want to make it simple. :D
Wow... You know, that is so simple that it's awesome. George's code is great and I thank him for the help, but I have to say that the great thing about this is that it will work with any value, no matter how high.

Thanks Aly

Thanks in advance for any help provided,
Jim
The All Time Professional Amateur Hack