Announcement

Collapse
No announcement yet.

Help with javascript for a character sheet?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help with javascript for a character sheet?

    Hey, I'm wondering if anyone here knows their way around acrobat javascript (or just javascript in general) and would be willing to help me with a character sheet I'm making?

    It is actually a charsheet for Mutants and Masterminds (I already copied the dot-filling function from Skafte's sheets for my various Exalted, WoD, et cetera sheets ^__^ ), but I figure I'd ask around in other places I frequent.

    The calculations that need to be performed are simple addition and multiplication. My problem is that I don't know how to write a function to identify various fields by itself. Ideally, I want a document-level function that I can just call when needed, instead of writing everything for each field.

    For instance, the various Skills are set up in my sheet with 4 fields each, for Total, Rank (how many points you put into it), Ability (the base value of that skill coming from an ability, like Strength or Intellect, though due to some house rules I'm just leaving that value to player input, it doesn't need to get the value from the respective Ability field), and Other (miscellaneous modifiers and ranks bought as part of a power, that won't count when I'm calculating the cost of the skill ranks).

    What I'm thinking is function I can call as a Validation script for the Rank, Ability and Other fields, that would add all 3 and set the Total to that value. As Validate because that event happens when a value is committed. The main reason I want to do a function is that the built-in calculate capacity of form fields makes it so every field with one set-up gets recalculated every time. By making it a custom validation script I would only recalculate the relevant fields and only when one of them is modified.

    However, I don't know how to declare variables able to get their value from different fields, auto-identifying the necessary ones. For instance, when I give a Ranks value to the Acrobatics skill, it should get the sum of Ranks, Ability and Other for the Acrobatics skill, and set the Acrobatics total as that value. And it should do so for the various different skills. I'm pretty sure there is some way to do that without having to write individual functions for each skill, as I have seen similar things done, but I don't know how to accomplish it.

    If possible, I'd like that function to be able to deal with a variable number of things to add, because Abilities would be the sum of two things, not three. Like, I think there is a way that, if I name the fields, say, Acrobatics.1, Acrobatics.2, Acrobatics.3 for a skill, or Strength.1, Strength.2 for an Ability, I can have the function with the base name as a parameter (say, Strength) and it can get all the "children".

    Anyway, that is what I have in mind so far, I would appreciate any hints, ideas or alternatives you can throw my way.


    You need a picture altered to fit your Exalted character, or just looking for some visual inspiration? Check out the twice-reborn Exaltification thread. And here is my Deviantart page

    Søren Kierkegaard + Kim Kardashian = Brilliance

  • #2
    Originally posted by danelsan View Post
    The calculations that need to be performed are simple addition and multiplication. My problem is that I don't know how to write a function to identify various fields by itself. Ideally, I want a document-level function that I can just call when needed, instead of writing everything for each field.
    Hey, sorry to pick out just this one part, but it seems the easiest for me to answer.

    I've had extremely limited experience with javascript in relation to Acrobat and character sheets. In my experience, you have to set it at the individual form field level. There is no global document scripting that I'm aware of...at least not in the versions of Acrobat I have access to.

    I know thats not very helpful...but, I thought I'd least chime in with my limited experience...


    MrGone's Character Sheets
    For all your WoD Character Sheet Needs
    _________________________________
    Buy books & pdfs from DTRPG and support my web site! Click Here
    _________________________________

    Comment


    • #3
      At least in the recent versions there are document level scripts (heck, there is a way to make folder level scripts that affect all the Forms in a given folder). After reading around I have successfully built one that works when called as a custom calculation script for individual text fields.

      This is good to save me a lot of writing, but doesn't solve the issue I'm trying to solve. If I understand correctly, acrobat pdf forms recalculate everything when you change any values, or even if you just enter a field, then leave it. I'm trying to find a way for it to re-calculate only the relevant fields when a value is changed that affect said field, leaving the rest alone.

      In my case, taking Mutants and Masterminds 3e Skills as an example, I have 4 fields per skill: one for the total, one for the ranks (how many points you invested in the skill), one for Ability (the value of the base Ability for that skill, such as Strength or Intellect. I'm leaving it to player input because I want the sheet to work both with RAW and with my house-rules), and one for Other (miscelanous modifiers and skill ranks bought as part of a power). These fields have the same name line after line, but are differentiated by number:

      1st row: [SkTotal.0] [SkRank.0] [SkAbility.0] [SkOther.0]
      2nd row: [SkTotal.1] [SkRank.1] [SkAbility.1] [SkOther.1]
      3rd row: [SkTotal.2] [SkRank.2] [SkAbility.2] [SkOther.2]
      and so on.

      So I made this document-level script:

      function CalcSkill()
      {
      //this takes the name of the name of the field that executes the script and separates what is written after the ".", in this case a number corresponding to that row of skill-related fields

      var SkN = event.target.name.split(".").pop();

      //The these variables get me the values of Rank, Ability and Other fields for that particular row, by concatenating the name of the field up to the dot with the number found out above

      var SkR = +getField("SkRank." + SkN).value;

      var SkA = +getField("SkAbility." + SkN).value;

      var SkO = +getField("SkOther." + SkN).value;

      //then I add the three values

      var SkT = SkR + SkA + SkO;

      // And finally set the total as that added up value

      getField("SkTotal." + N).value = SkT;
      }

      With this, I can go to the individual SkTotal fields of each row, go to Calculate, custom script, and just paste "CalcSkill();" and it will add up correctly...sort of.

      I initially tried to use it as a validation script on the component fields (rank, ability, other), but the calculation is always a step behind. Like, always showing the previous sum, one changed number behind the actual result - and rewriting the same number doesn't count as changing it for validation purposes, apparently (though even that would be annoying).

      I have been told that calculations a step behind is usually a sign that something is out of order, some value is calculated before something on which it depends, then someone else said I can't just put a calculation as a validation and have it work right (though the script that I mostly copied mine from says it was supposed to be a validation - exactly to avoid the "recalculate everything all the time" issue)

      Anyway, used as an actual calculation script, It appears to still be a step behind, but corrects itself because it does recalculate again just by entering and exiting any field.

      I'm kinda stuck there, now. I don't see how the calculations in my script could be calculating something before it has the necessary information.

      In the end I'll probably be satisfied with not having to put a big script in each individual field but still having to take a bunch of calculations every time you do anything to a field. At least I learned some more Javascript by doing this.


      You need a picture altered to fit your Exalted character, or just looking for some visual inspiration? Check out the twice-reborn Exaltification thread. And here is my Deviantart page

      Søren Kierkegaard + Kim Kardashian = Brilliance

      Comment


      • #4
        Oh wow...well, looks you found something that'd mostly work for you, so thats good.

        Thanks for the info!


        MrGone's Character Sheets
        For all your WoD Character Sheet Needs
        _________________________________
        Buy books & pdfs from DTRPG and support my web site! Click Here
        _________________________________

        Comment

        Working...
        X