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

Need help using simple javascript in a character sheet

September 27 (8 years ago)
Tomaxx
Sheet Author
Hi everyone!

I'm very new to building character sheets so please bear with me :)

I have two attributes-numbers that I call upon using @acrocarac and @acrorang.

I want to use javascript to test these two values and return a single value using this logic:
If @acrorang = 0 then x=0, return x
Else x = @acrocarac + @acrorang, return x.

So in the very beginning of the character sheet I declare my function:

<script>
  function skilltest(a, b) {
    var x = a;
    var y = b;
    if (y = 0) {
         var z = 0;
    } 
    else {
         var z = x + y; 
    }
    return z;
 }
</script>
And later in the character sheet itself I try calling the function:

<input type="number" name="attr_acrobatie" value="skilltest(@{acrocarac}, @{acrorang})" disabled="true" />
This should display either 0 or an integer in a disabled box.

Evidently, this does not work, but I cannot figure out why.

I'd be very grateful for any help you could give me on this :)

Thanks!






September 27 (8 years ago)
Finderski
Sheet Author
Compendium Curator
What you need to use is a sheet worker (a special type of Javascript accessible by a character sheet).  Here's the info on how to implement Sheet Workers in your sheet: https://wiki.roll20.net/Sheet_Worker_Script
September 28 (8 years ago)
Lithl
Pro
Sheet Author
API Scripter
<input type="number" name="attr_acrocarac">
<input type="number" name="attr_acrorang">
<input type="number" name="attr_acrobatie" readonly>

<script type="text/worker">
on('ready change:acrocarac change:acrorang', function() {
getAttrs(['acrocarac', 'acrorang'], function(values) {
var acrocarac = parseInt(values.acrocarac) || 0,
acrorang = parseInt(values.acrorang) || 0;

setAttrs({ actrobatie: acrorang === 0 ? 0 : acrocarac + acrorang });
});
});
</script>
Script untested, but should be correct.
September 28 (8 years ago)
Tomaxx
Sheet Author
Thank you Finderski and Brian for the quick replies :)

I'll test that script as soon as I get home today.
September 28 (8 years ago)

Edited September 28 (8 years ago)
Tomaxx
Sheet Author
Hi again,

I managed to get the sheetworker script working, but it's returning an incorrect value.
Here is my code (the pertinent parts at least):

 <h1>Caractéristiques</h1>
        
        <span>FOR - Force </span> <INPUT type="number" name="attr_force" value="0"></INPUT><br>
        <span>CON - Constitution </span> <INPUT type="number" name="attr_const" value="0"></INPUT><br>
        <span>DEX - Dextérité </span> <INPUT type="number" name="attr_dext" value="0"></INPUT><br>
        <span>INT - Intelligence </span> <INPUT type="number" name="attr_intel" value="0"></INPUT><br>
        <span>SAG - Sagesse </span> <INPUT type="number" name="attr_sage" value="0"></INPUT><br>
        <span>CHA - Charisme </span> <INPUT type="number" name="attr_chari" value="0"></INPUT><br>


<h1>Compétences</h1>
        
        <span> Caracs - Rang - Total</span> <!-- This is a label above the 3 columns of numbers next to the skill name -->
        
        
        <span>Acrobatie (FOR, DEX) </span>    
            <input type="number" name="attr_acrocarac" value="(@{force}+@{dext})" disabled="true" /> <!-- this displays correctly -->
            <input type="number" name="attr_acrorang" value="0" />
            <input type="number" name="attr_acrobatie" readonly >        
                
                <script type="text/worker">
                    on('ready change:acrocarac change:acrorang', function() {
                        getAttrs(['acrocarac', 'acrorang'], function(values) {
                        var acrocarac = parseInt(values.acrocarac) || 0,
                            acrorang = parseInt(values.acrorang) || 0;
                        
                        setAttrs({ acrobatie: acrorang === 0 ? 0 : acrocarac + acrorang });
                        });
                    });
                </script>
               
        (...)
               
        <span>Athlétisme (FOR, CON) </span>
            <input type="number" name="attr_athletismecarac" value="(@{force}+@{const})" disabled="true" />
            <input type="number" name="attr_athletismerang" value="0" />
            <input type="number" name="attr_athletisme" value="(@{athletismecarac}+@{athletismerang})" disabled="true" />
            <br>
       

The sheetworker should return the sum of acrocarac and acrorang, unless acrorang=0, in which case the total is always 0.

The sheetworker returns only the value of acrorang, without adding acrocarac to it (so I don't even know if the condition (acrorang===0) is being tested. Possibly it somehow sets the value of acrorand to 0 for some reason.

In the code above I included another skill, to show how it should work. This is without the sheetworker and it works correctly, albeit without the condition (acrorang===0...).

I thought that the problem could be the result of one of the values being somehow a string, but that doesn't seem to be the case.

This is what I like about coding, it's a lot of detective work ;)


Also, I don't fully understand this: "parseInt(values.acrocarac) || 0"
parseInt changes even a string into a number, so it's there to make sure the variable is a number, ok.
Can you tell me what "|| 0" is and does please? I can't seem to find a reference for it.

Thanks!


September 28 (8 years ago)
Finderski
Sheet Author
Compendium Curator
The problem may be that attr_acrocarac is an autocalc'd field.  I'd recommend making that input read-only, and use a sheet worker script (similar to what you have here) to set that field when force or dext change.
September 28 (8 years ago)
Tomaxx
Sheet Author
I'll try that and let you know, thanks :)
September 28 (8 years ago)
Jakob
Sheet Author
API Scripter

Tomaxx said:


Also, I don't fully understand this: "parseInt(values.acrocarac) || 0"
parseInt changes even a string into a number, so it's there to make sure the variable is a number, ok.
Can you tell me what "|| 0" is and does please? I can't seem to find a reference for it.

It's a logical 'or'. If parseInt(values.acrocarac) has a falsy value (in javascript, a lot of things are falsy, for example NaN, which is what parseInt returns when it can't change values.acrocarac to an integer in a meaningful way), it will use the second value (0) instead.
September 28 (8 years ago)
Tomaxx
Sheet Author

Jakob said:

Tomaxx said:


Also, I don't fully understand this: "parseInt(values.acrocarac) || 0"
parseInt changes even a string into a number, so it's there to make sure the variable is a number, ok.
Can you tell me what "|| 0" is and does please? I can't seem to find a reference for it.

It's a logical 'or'. If parseInt(values.acrocarac) has a falsy value (in javascript, a lot of things are falsy, for example NaN, which is what parseInt returns when it can't change values.acrocarac to an integer in a meaningful way), it will use the second value (0) instead.

I get it now, thanks Jakob :)
September 28 (8 years ago)
Tomaxx
Sheet Author

Finderski said:

The problem may be that attr_acrocarac is an autocalc'd field.  I'd recommend making that input read-only, and use a sheet worker script (similar to what you have here) to set that field when force or dext change.

This was the problem. Now it works as intended!

Thank you very much for your kind assistance :)

Am I to understand that autocalc and sheetworkers don't mix well?

Thanks again!

September 28 (8 years ago)
Finderski
Sheet Author
Compendium Curator
Yeah, because the autocalc isn't a real number (as I understand it). It works in the dice rolls, because the actual values are resolved before the dice roll, but to use it for other things with sheet workers doesn't work as well.  
September 28 (8 years ago)
Tomaxx
Sheet Author

Finderski said:

Yeah, because the autocalc isn't a real number (as I understand it). It works in the dice rolls, because the actual values are resolved before the dice roll, but to use it for other things with sheet workers doesn't work as well.  

Got it.
Sheet workers all the way then :)
Thanks :)