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

Cannot figure out why this is not working

1559076640
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
Just another set of eyes please. Maybe you see an error I don't on('sheet:opened change:strength change:hit_points_points change:hit_points_mod', function (e) { updateHPLocations(); }); function updateHPLocations(v) { getAttrs(['strength', 'strength_base', 'hit_points_points', 'hit_points_mod'], function (v) { var base = (modCascadeAll ? +v.strength : +v.strength_base) + ((+v.hit_points_points || 0) / 2 | 0); var mod = +v.hit_points_mod; var maxhp = base + mod; var eyes = Math.floor(maxhp / 10); var limbs = Math.floor(maxhp / 2); var extr = Math.floor(maxhp / 3); setAttrs({ ehp: eyes, limbhp: limb, extremityhp: extr }); }); } <div class="sheet-cell sheet-col1"> <input type="number" name="attr_ehp" value="0" readonly="readonly" /> </div> <div class="sheet-cell sheet-col1"> <input type="number" name="attr_limbhp" value="10" readonly="readonly" /> </div> <div class="sheet-cell sheet-col1"> <input type="number" name="attr_extremityhp" value="10" readonly="readonly" /> </div>
1559077873
Diana P
Pro
Sheet Author
Note:  not really a programmer, but the first thing I notice is you have limbhp: limb   but are  actually calculating limb s instead.  But that's really all I notice (since I don't understand the modCascadeAll  and why there is a + in your var mod = +v.hit_points_mod; bit).  Sorry to not be of more help than that.)
1559085776
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
Thanks, I appreciate the help.
1559098117
GiGs
Pro
Sheet Author
API Scripter
Diana, this +v.hit_points_points || 0 is another way to coerce an attribute into a number, it's equivalent (not identical, but works) to parseInt(v.hit_points_points) || 0 The modCascadeAll part seems to have a major problem I'll come back to, but this section (modCascadeAll ? +v.strength : +v.strength_base) is called a ternary operator, and is a very handy shorthand for some if statements. read it like this If something ? what happens when true : what happens when false It's very handy because of how ypu can use it within expressions.  Imagine a game with CON and SIZ (size) stats, and Hit points are equal to whichecer of the two is highest. You could calculate that like so: let hp = 0; if (CON > SIZ) {      HP = CON; } else {     HP = SIZ; } But the ternary operator lets you use much shorter code: let hp = (CON > SIZ) ? CON : SIZ; You start with an expression to evaluate, then a question mark, then the true result, then a colon, then the false result. It looks weird and confusing to begin with, but its well worth learning, because it's really useful.     
1559102686

Edited 1559102797
GiGs
Pro
Sheet Author
API Scripter
Did Diana's suggestion fix the script, Savage? I see a couple of problems function updateHPLocations(v) { // <-- v getAttrs(['strength', 'strength_base', 'hit_points_points', 'hit_points_mod'], function (v) { // <-- and another v Here you declare v twice. As far as I can tell, the first one should just be () not (v). A couple of issues here: var base = (modCascadeAll ? +v.strength : +v.strength_base) + what is modCascadeall? It's not defined in the script, so this expression will always be false. (Unless its a global variable of some sort.) Also you don't have the || 0 after each variable there, which may or may not be needed. ((+v.hit_points_points || 0) / 2 | 0); It looks like this is missing a | ((+v.hit_points_points || 0) / 2 || 0);
1559108846

Edited 1559147613
Diana P
Pro
Sheet Author
Thank you for the explanation, Gigs.  I shall attempt to wrap my mind around it. :)
1559308564
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
GiGs said: Diana, this +v.hit_points_points || 0 is another way to coerce an attribute into a number, it's equivalent (not identical, but works) to parseInt(v.hit_points_points) || 0 The modCascadeAll part seems to have a major problem I'll come back to, but this section (modCascadeAll ? +v.strength : +v.strength_base) is called a ternary operator, and is a very handy shorthand for some if statements. read it like this If something ? what happens when true : what happens when false It's very handy because of how ypu can use it within expressions.  Imagine a game with CON and SIZ (size) stats, and Hit points are equal to whichecer of the two is highest. You could calculate that like so: let hp = 0; if (CON > SIZ) {      HP = CON; } else {     HP = SIZ; } But the ternary operator lets you use much shorter code: let hp = (CON > SIZ) ? CON : SIZ; You start with an expression to evaluate, then a question mark, then the true result, then a colon, then the false result. It looks weird and confusing to begin with, but its well worth learning, because it's really useful.      @GiGs: Thanks for the nice explanation of the expressions.  And, yes Diana did help...thank you very much.
1559308642
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
The modCascade ensures all of the sheetworkers on the sheet trip and do calcualtions.
1559312646

Edited 1559312684
GiGs
Pro
Sheet Author
API Scripter
Oh, so it is a global variable? It's a bit weird its only used for strength.  Without seeing the full code, I wonder if it's necessary. If it's a protection against invalid values of strength, you could use var base = (+v.strength || +v.strength_base) + or even var base = (+v.strength || +v.strength_base || 0) + If it's supposed to stop the entire sheet worker, you could put it earlier, like on('sheet:opened change:strength change:hit_points_points change:hit_points_mod', function (e) { if(modCascadeAll) updateHPLocations(); }); All this comes from not knowing the full sheet and how it's used, feel free to ignore these ruminations if I'm off-base.
1559313935
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
The original sheet code...which has long since been transmuted into my current is still partly resident within my current code. As I get better at Javascript I plan to replace functions such as that with more streamlined and efficient code. GiGs, you are a GM in my Working Copy game. Don't hesitate to take a look, make suggestions, recommendations, etc.
1559315741
GiGs
Pro
Sheet Author
API Scripter
I do that too. I have a sheet i started working on maybe 3 years ago, that i am gradually refining with the stuff I've learned since then. On your sheet - Being a GM unfortunately doesnt let me look at the character sheet, so i cant see the sheetworkers. 
1559316978
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
Obviously... [personal eyeroll and facepalm because I wasn't thinking] I'll share with you in private message later.