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

Calling all Sheet Worker Wizards

1505334976
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
I am trying to have my sheet update the encumbrance based on the selection of a radio button that chooses a loadout. here is my code thus far // Get Loadout Choice: on('change:loadoutchoice', function (e) {updateloadout(); }); //Update Loadout function updateloadout() { console.log('****updateloadout****'); getAttrs(['loadoutchoice']], function(v) { var attrs = {}; if (v.loadoutchoice === "a") { total_weight = "loadouta_total_weight"; } else if (v.loadoutchoice === "b") { total_weight = "loadoutb_total_weight"; } else if (v.loadoutchoice === "c") { total_weight = "loadoutc_total_weight"; } setAttrs({ total_weight: total_weight, }); }); } // Get Encumbrance Level: on('change:total_weight change:basic_lift', function (e) { updateEncLevel(); }); function updateEncLevel(callback) { getAttrs(['basic_lift', 'total_weight'], function(v) { callback = callback || noop; var basicLift = +v.basic_lift; var encumbranceLevel = Math.floor(v.total_weight / basicLift); if (encumbranceLevel > 0 && (v.total_weight % basicLift) === 0) { encumbranceLevel--; } if (encumbranceLevel >= 10) { encumbranceLevel = 5; } else if (encumbranceLevel >= 6) { encumbranceLevel = 4; } else if (encumbranceLevel >= 3) { encumbranceLevel = 3; } setAttrs( { encumbrance_level: encumbranceLevel }, {silent: false}, callback ); }); }
1505336906
Lithl
Pro
Sheet Author
API Scripter
At a glance: You've got an extra closing square bracket on the first parameter to getArgs in updateLoadout. The total_weight variable in updateLoadout is never declared, although outside strict mode that will work anyway. The attrs variable in updateLoadout is unused. The callback parameter in updateEncLevel will always be undefined, and the noop variable is never declared. But more importantly, what is your code doing that it shouldn't be? Or, what is it not doing that it should be
1505337835
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
The idea is I want the player to be able to choose a load out and have that bounced off the encumbrance. Which in another sheet worker affects move, dodge, etc.
1505337969

Edited 1505338698
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
after some updates // Get Loadout Choice: on('change:loadoutchoice', function (e) {updateloadout(); }); //Update Loadout function updateloadout() { console.log('****updateloadout****'); getAttrs(['loadoutchoice'], function(v) { var attrs = {}; if (v.loadoutchoice === "a") { var totalweight = "loadouta_total_weight"; } else if (v.loadoutchoice === "b") { var totalweight = "loadoutb_total_weight"; } else if (v.loadoutchoice === "c") { var totalweight = "loadoutc_total_weight"; } setAttrs({ total_weight: totalweight, }); }); }
1505338748
Jakob
Sheet Author
API Scripter
Why don't you write on('change:loadoutchoice', updateLoadout); instead of on('change:loadoutchoice', function (e) {updateloadout();}) Also, about the updateLoadout function: extra comma after totalweight the attrs variable is still there, and still unused Also, this function doesn't do what you want it to do, I think - the value of total_weight at the end will be a string e.g. "loadouta_total_weight", but you want it to be the value  of the loadout{a,b,c}_total_weight, attribute, don't you? You'd have to get those three attributes as well in the getAttrs, and set total_weight to e.g. v.loadouta_total_weight instead. This also means you have to add these attributes to the event string at the start. Only if you do that does updateEncLevel() have a chance of doing anything sensible.
1505339148

Edited 1505347133
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
after more edits // Get Loadout Choice: on('change:loadoutchoice', updateloadout); //Update Loadout function updateloadout() { console.log('****updateloadout****'); getAttrs(['loadoutchoice, loadouta_total_weight, loadoutb_total_weight, loadoutc_total_weight'], function(v) { if (v.loadoutchoice === "a") { var totalweight = v.loadouta_total_weight; } else if (v.loadoutchoice === "b") { var totalweight = v.loadoutb_total_weight; } else if (v.loadoutchoice === "c") { var totalweight = v.loadoutc_total_weight; } setAttrs({ total_weight: totalweight }); }); }
1505348909
Lithl
Pro
Sheet Author
API Scripter
Jakob said: extra comma after totalweight Actually, that's permissible by JS syntax. In fact, the trailing comma in object and array literals is part of the style guide here at work. It's nice, because if you need to add another property to the end of the object literal, or another element to the end of the array, you don't need to remember to add the comma to the line you're not  editing.
1505349107

Edited 1505349980
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
Still not working after. This is what I have. // Get Loadout Choice: on('sheet:opened change:loadoutchoice', updateloadout); //Update Loadout function updateloadout(callback) { console.log('****updateloadout****'); getAttrs(['loadoutchoice, loadouta_total_weight, loadoutb_total_weight, loadoutc_total_weight'], function(v) { callback = callback || noop; if (v.loadoutchoice === 1) { var totalweight = v.loadouta_total_weight; } else if (v.loadoutchoice === 2) { var totalweight = v.loadoutb_total_weight; } else if (v.loadoutchoice === 3) { var totalweight = v.loadoutc_total_weight; } setAttrs(    { total_weight: totalweight },    {silent: false},    callback ); }); }
1505386582
Jakob
Sheet Author
API Scripter
Brian said: Jakob said: extra comma after totalweight Actually, that's permissible by JS syntax. In fact, the trailing comma in object and array literals is part of the style guide here at work. It's nice, because if you need to add another property to the end of the object literal, or another element to the end of the array, you don't need to remember to add the comma to the line you're not  editing. Hmm, didn't know that! It makes sense from the perspective of only editing the line you're actually adding, though I find it odd from a semantical point of view. @Scott S.: Try (v.loadoutchoice === '1') (et cetera). Attribute values are always strings.  You're missing a whole lot of quotes in your getAttrs argument: ['loadoutchoice, loadouta_total_weight, loadoutb_total_weight, loadoutc_total_weight'] will get a single attribute with the humungous name 'loadoutchoice, loadouta_total_weight, loadoutb_total_weight, loadoutc_total_weight'. Your event string should also include change:loadouta_total_weight et cetera.
1505392035
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
I have it functioning.... I had to add 'on change:loadoutchoice' to the updateEncLevel to get it to work. See below. // Get Encumbrance Level: on('change:total_weight change:basic_lift change:loadoutchoice', function (e) { updateEncLevel(); }); function updateEncLevel(callback) { getAttrs(['basic_lift', 'total_weight'], function(v) { callback = callback || noop; var basicLift = +v.basic_lift; var encumbranceLevel = Math.floor(v.total_weight / basicLift); if (encumbranceLevel > 0 && (v.total_weight % basicLift) === 0) { encumbranceLevel--; } if (encumbranceLevel >= 10) { encumbranceLevel = 5; } else if (encumbranceLevel >= 6) { encumbranceLevel = 4; } else if (encumbranceLevel >= 3) { encumbranceLevel = 3; } setAttrs( { encumbrance_level: encumbranceLevel }, {silent: false}, callback ); }); } // Get Loadout Choice: on('sheet:opened change:loadoutchoice', updateloadout); //Update Loadout function updateloadout(callback) { console.log('****updateloadout****'); getAttrs(['loadoutchoice', 'loadouta_total_weight', 'loadoutb_total_weight', 'loadoutc_total_weight'], function(v) { callback = callback || noop; if (v.loadoutchoice === "a") { var loadout_totalweight = v.loadouta_total_weight; } else if (v.loadoutchoice === "b") { var loadout_totalweight = v.loadoutb_total_weight; } else if (v.loadoutchoice === "c") { var loadout_totalweight = v.loadoutc_total_weight; } setAttrs(    { total_weight: loadout_totalweight },    {silent: false},    callback ); }); }
1505406237
Lithl
Pro
Sheet Author
API Scripter
Jakob said: Brian said: Jakob said: extra comma after totalweight Actually, that's permissible by JS syntax. In fact, the trailing comma in object and array literals is part of the style guide here at work. It's nice, because if you need to add another property to the end of the object literal, or another element to the end of the array, you don't need to remember to add the comma to the line you're not  editing. Hmm, didn't know that! It makes sense from the perspective of only editing the line you're actually adding, though I find it odd from a semantical point of view. Yeah. It isn't valid JSON, though. But JSON.stringify handles the trailing comma just fine: console.log(JSON.stringify([ 'foo', 'bar', 'baz', ])); // "[\"foo\",\"bar\",\"baz\"]"