External Scripts

I have a couple of functions that I would like to be available to multiple sheets, so I took them from the sheet I am working on and put them in a .js file, then added the file by going to: Sheet -> Document Setup -> Script Configuration

However, now the sheet throws a runtime error when testing. Is there any other specific steps that must be taken either in the external script file or the sheet itself?

Comments

  • Its hard to say - are you sure its not just an error in the javascript code? What was the error?
  • The code is relatively easy and is just for random rolls. The first error is "Microsoft JScript runtime error." which is followed by "Access violation at address 71633D4A in module 'jscript.dll'. Read of address 0000001B."

    This is the code I put in the external file. All I did was to put the functions in a text file named DiceRoller.js
    function rollDie(sides)
      {
        if(!sides) sides = 6;
        with(Math) return 1 + floor(random() * sides);
      }
    
    function rollDice(number, sides)
      {
        var total = 0;
        while(number-- > 0) total += rollDie(sides);
        return total;
      }
    

    This is an example of the calling code (the function call works when the above is in the script section of the sheet, but not when it is put in the external file.)
    function GetMagicItemNumbers(type, number, sides)
    {
        var condition = ""
        switch(type) {
            case "Low":
                sides /= 2;
                condition = " (Halved)";
                break;
            case "High":
                number *= 2;
                condition = " (Doubled)";
                break;
        }
        return number + "d" + sides + "(" + rollDice(number,sides) + ")" + condition;
    }
    
    

    Thanks for looking at this.
  • I get those errors a lot.

    I've found that they can be caused by fields being set to read only, and once by a field that had been accidentally pasted (that happens if you try to paste into the Script tab) behind something else.

    I figure, check the status of your fields, see if something is read only or not.
  • I am coming across an issue similar to what the OP had mentioned about external javascript files being recognized. It seems odd that when adding an external scripts the only 3 that are listed by default are .vbs, .FMScript and AstroScript. I believe that latter two are custom scripts made by other software applications by NBOS which makes sense but I am not sure I understand why there is a script editor built into the character designer that allows JavaScript but then there is no way to seemingly add .js files to the character designer externally. In the documentation there is mention that JavaScript files can be added externally but in practice I have found this to not be true.

    As a test I took a working character design and added a text edit box that is completely editable (i.e. not read only, not hidden, not a number). I set the fieldname as test123. For the source I included a simple function call called magicBean();

    In an external testFile.js file I create this function and define it as such:

    function magicBean(){
    return "I am a bean";
    }

    I go into the sheet -> Document Setup -> Script Configuration and add my testFile.js into the external scripts area.

    Now I got to test this sheet in the viewer and I get a script error. The scripting error is a result of the field update to the field named test123 and the source call magicBean();

    Obviously this field is not seeing my external javascript file.

    To show that this isn't a result of the external script not being correct, I then copy the function magicBean() from the external javascript file and place it into the built in script editor. I make sure to right click and select paste rather than hitting ctrl-v which will make a copy of the field I had selected on the WYSIWYG editor. With the magic bean function in place I not rerun my character design and voila, it starts up with no error. As expect the field named test123 is being displayed as "I am a bean".

    So I can only conclude that either there is a special way external scripts are called by the character designer or that external .js files aren't supported despite the documentation saying:

    "You can also access a field directly in a javascript function, like:
    function AdjustHits()
    {
    Hits.Text = Strength.Value + Constitution.Value;
    }
    External JavaScript files can be used to store oft used functions. See the document setup
    window for where you set that up."

    I have tested this on both windows 7 and windows 8 and it seems to be the case for both. If anyone wouldn't mind testing this on other OS I would be curious if it's an OS issue or an actual bug with the application.

Leave a Comment