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

Assist with concat

1558459722
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
I want to concat the values that the getAttrs function gets. I put this together based off someone else's code, but somehow mine is broken. on("change:stunned change:shock sheet:opened", function() { console.log("==============================="); console.log("****!!!Update-Conditions!!!*****"); getAttrs(['stun_name', 'shock_name'],function(values) { getConditionNames(function(currentConditions) { const conditionNames = [];//We'll collect all the attribute names in here. console.log(` cond count: ${currentConditions.length}`);//using object literal notation makes your log declarations easier to read for(var i=0; i < currentConditions.length; i++) { console.log(` cond idlen:${currentConditions[i].length}`); console.log(` cond id:|${currentConditions[i]}|`); idCollection.push(currentConditions[i]);//push the attribute name into the collection array } }); var conditions = currentConditions.join(", "); setAttrs({ current_conditions: conditions }); }) });
1558463583
GiGs
Pro
Sheet Author
API Scripter
You have a bunch of problems, some syntax and some basic design issues. The big problem is, it's not clear what it's supposed to be doing. You have getAttrs calling the stats stun_name and shock_name, but those stats aren't used in the worker.  The stats named in the change line and getAttrs line don't match - is this right? You try to declare a function but the syntax is messed up Also that function isnt actually needed You declare the conditionNames variable but later use idCollection It looks like you get the currentConditions mixed up with conditionNames at the end. There is no list of conditions in the script  -should there be? In conclusion, there's no way to be gentle about this, it is a mess.  Can you describe what the script is supposed to be doing, in as much detail as possible? 
1558464040
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
Yes, I see that it's a mess....I've been looking at this stuff all day...not sure I can see straight anymore. Let me describe what I am trying to do. I have a box that will display current conditions at the top of the sheet. Further down in the sheet are a series of check boxes. Shock, Stunned, etc. When they are checked or set by macro I want the condition names to be displayed in the aforementioned box. Preferably comma separated.
1558464429
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
Here's an array of conditions.  var conditions = [ "Stunned", "Shock", "Mentally Stunned", "Bleeding", "Reeling", "Mortally Wounded", "Coughing", "Drowsy", "Drunk", "Euphoria", "Nauseated", "Moderate Pain", "Severe Pain", "Terrible Pain", "Typsy", "Agony", "Choking", "Dazed", "Ecstacy", "Hallucinating", "Paralyzed", "Retching", "Seizure", "Unconscious", "Coma", "Heart Attack" ];
1558466455

Edited 1558466599
GiGs
Pro
Sheet Author
API Scripter
Okay, aside from the syntax issues, the main thing you were missing was a way to test if the checkbox is checked, and enter its value if it is. First, make make sure the checkboxes are named the same as your conditions in that array, but all in small case and with spaces replaced by underscores. If not, rename them so they are.  This is very important . Double check them. Ordinarily you would have a long list on the change and getAttrs line, but with that conditions array we can build them dynamically. It looks confusing, but its easier for me to write. Don't worry too much about that. Look at the part below, the if statement, where the checkbox value is tested: If you already have the conditions array at the top of your sheet worker, you dont need the first line. conditions shouldn't be declared twice. var conditions = [ "Stunned", "Shock", "Mentally Stunned", "Bleeding", "Reeling", "Mortally Wounded", "Coughing", "Drowsy", "Drunk", "Euphoria", "Nauseated", "Moderate Pain", "Severe Pain", "Terrible Pain", "Typsy", "Agony", "Choking", "Dazed", "Ecstacy", "Hallucinating", "Paralyzed", "Retching", "Seizure", "Unconscious", "Coma", "Heart Attack" ]; var condition_attrs = conditions.map( c => c.toLowerCase().replace(' ', '_')); on(`change: ${condition_attrs.join(' change:')} sheet:opened`, function () { console.log("==============================="); console.log("****!!!Update-Conditions!!!*****"); getAttrs(condition_attrs, function (values) { var conditionNames = []; //We'll collect all the attribute names in here. console.log(` cond count: ${conditionNames.length}`); for (var i = 0; i < condition_attrs.length; i++) { console.log(` cond idlen:${i}`); // changed this to show the current row console.log(` cond id:|${condition_attrs[i]}|`);             // need to test the checkbox value if(values[condition_attrs[i]] !== 0) { conditionNames.push(condition_attrs[i]); //push the attribute name into the collection array } } var conditions = conditionNames.join(", "); setAttrs({ current_conditions: conditions }); }); });
1558467572
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
Checkboxes are good to go. Checkboxes will be 0 or 1. There are some radios that range from 0 to n. on('sheet:opened change:stunned', function () { console.log('****!!!Update-Conditions!!!*****'); getAttrs(["stunned"], function (v) { var penalty = +v.stunned * -4; var text = ""; if (penalty < 0 ) { text = "You are -4 to active defenses and cannot retreat, and must Do Nothing on your next turn. At the end of your turn, attempt a HT roll to recover. If you fail, you’re still stunned and must Do Nothing for another turn. And so on.."; name = "Stunned"; } setAttrs({ stun_text: text, stun_penalty: penalty }); }); });
1558469847

Edited 1558470407
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
Was it supposed to push the conditions in upper case, as they are in the array? And for some weird reason Stunned is not triggering the sheetworker.
1558470603

Edited 1558470783
GiGs
Pro
Sheet Author
API Scripter
I made a small mistake in the script I posted. this line conditionNames.push(condition_attrs[i]); should be conditionNames.push(conditions[i]); Which sheet worker is stunned not triggering? the one i posted, or the one you just posted? I'd also recomment changing this log statement console.log('****!!!Update-Conditions!!!*****'); To include the sheet worker, so you know which one is triggering correctly and which aren't. Something like: console.log('****!!!Update-Conditions!!!*****'); console.log('**** stunned *****');
1558470934
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
Yours, I have a debugger that shows the attr changing from 0 to 1 and thus changing the penalty  for 0 to -4
1558471490
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
Here's what I have. The compiler says it's good to hook. Yet not displaying. var conditions = [ "Stunned", "Shock", "Mentally Stunned", "Bleeding", "Reeling", "Mortally Wounded", "Coughing", "Drowsy", "Drunk", "Euphoria", "Nauseated", "Moderate Pain", "Severe Pain", "Terrible Pain", "Typsy", "Agony", "Choking", "Dazed", "Ecstasy", "Hallucinating", "Paralyzed", "Retching", "Seizure", "Unconscious", "Coma", "Heart Attack" ]; var condition_attrs = conditions.map( c => c.toLowerCase().replace(' ', '_')); on(`change: ${condition_attrs.join(' change:')} sheet:opened`, function () { console.log("==============================="); console.log("****!!!Update-Conditions!!!*****"); getAttrs(condition_attrs, function (values) { var conditionNames = []; //We'll collect all the attribute names in here. console.log(` cond count: ${conditionNames.length}`); for (var i = 0; i < condition_attrs.length; i++) { console.log(` cond idlen:${i}`); // changed this to show the current row console.log(` cond id:|${condition_attrs[i]}|`); // need to test the checkbox value if(values[condition_attrs[i]] != 0) { conditionNames.push(conditions[i]); //push the attribute name into the collection array } } var conditions = conditionNames.join(", "); setAttrs({ current_conditions: conditions }); }); });
1558472202
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
Here's a thought....it is possible to capitalize and remove underscores, right?
1558472601

Edited 1558472707
GiGs
Pro
Sheet Author
API Scripter
ahhh I have an extra space that shouldnt be there on the on(change) line. Should be on(`change:${condition_attrs.join(' change:')} sheet:opened`, function () { yes, you can capitalise and remove underscores. why do you ask?
1558472661
GiGs
Pro
Sheet Author
API Scripter
That looks like a very handy debugging script. Is it in the one-click?
1558480361

Edited 1558480963
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
OK, the display is still not working after the edits. The array for the uppercase names is var = conditions. The next variable is var condition_attrs. Which is where we are getting the indexes, right? Would it change anything if I moved the var = conditions inside the function getAttrs? Below works but only displays lowercase. The edit you posted did not work.  // GiGs Copy var conditions = [ "Stunned", "Shock", "Mentally Stunned", "Bleeding", "Reeling", "Mortally Wounded", "Coughing", "Drowsy", "Drunk", "Euphoria", "Nauseated", "Moderate Pain", "Severe Pain", "Terrible Pain", "Typsy", "Agony", "Choking", "Dazed", "Ecstasy", "Hallucinating", "Paralyzed", "Retching", "Seizure", "Unconscious", "Coma", "Heart Attack" ]; var condition_attrs = conditions.map( c => c.toLowerCase().replace(' ', '_')); on(`change:${condition_attrs.join(' change:')} sheet:opened`, function () { console.log("==============================="); console.log("****!!!Update-Conditions!!!*****"); getAttrs(condition_attrs, function (values) { var conditionNames = []; //We'll collect all the attribute names in here. console.log(` cond count: ${conditionNames.length}`); for (var i = 0; i < condition_attrs.length; i++) { console.log(` cond idlen:${i}`); // changed this to show the current row console.log(` cond id:|${condition_attrs[i]}|`); // need to test the checkbox value if(values[condition_attrs[i]] != 0) { conditionNames.push(condition_attrs[i]); //push the attribute name into the collection array } } var conditions = conditionNames.join(", "); setAttrs({ current_conditions: conditions }); }); });
1558482646

Edited 1558482776
GiGs
Pro
Sheet Author
API Scripter
no, the conditions variable has to be before the on(change) line, because the change event and getAttrs lines depends on it. first,check if you have another copy of the conditions variable anywhere? second, try this just to test a theory - notice that the name of one of the arrays have changed, so delete the old worker (including the conditions variable at the start) completely, and copy this in to replace it. // GiGs Copy var conditionsTest = [ "Stunned", "Shock" ]; var condition_attrs = conditionsTest.map( c => c.toLowerCase().replace(' ', '_')); on(`change:${condition_attrs.join(' change:')} sheet:opened`, function () { console.log("==============================="); console.log("****!!!Update-Conditions!!!*****"); getAttrs(condition_attrs, function (values) { var conditionNames = []; //We'll collect all the attribute names in here. console.log(` cond count: ${conditionNames.length}`); for (var i = 0; i < condition_attrs.length; i++) { console.log(` cond idlen:${i}`); // changed this to show the current row console.log(` cond id:|${condition_attrs[i]}|`); // need to test the checkbox value if(values[condition_attrs[i]] != 0) { conditionNames.push(conditionsTest[i]); //push the attribute name into the collection array } } var conditions = conditionNames.join(", "); setAttrs({ current_conditions: conditions }); }); });
1558483013
GiGs
Pro
Sheet Author
API Scripter
oh dear, the problem has been staring us in the face all along. We have been using an array called conditions, and in the sheetworker, create a second array called conditions. You cant have two arrays with the same name in the same script. So try this (changed the second conditions name): var conditions = [ "Stunned", "Shock", "Mentally Stunned", "Bleeding", "Reeling", "Mortally Wounded", "Coughing", "Drowsy", "Drunk", "Euphoria", "Nauseated", "Moderate Pain", "Severe Pain", "Terrible Pain", "Typsy", "Agony", "Choking", "Dazed", "Ecstasy", "Hallucinating", "Paralyzed", "Retching", "Seizure", "Unconscious", "Coma", "Heart Attack" ]; var condition_attrs = conditions.map( c => c.toLowerCase().replace(' ', '_')); on(`change:${condition_attrs.join(' change:')} sheet:opened`, function () { console.log("==============================="); console.log("****!!!Update-Conditions!!!*****"); getAttrs(condition_attrs, function (values) { var conditionNames = []; //We'll collect all the attribute names in here. console.log(` cond count: ${conditionNames.length}`); for (var i = 0; i < condition_attrs.length; i++) { console.log(` cond idlen:${i}`); // changed this to show the current row console.log(` cond id:|${condition_attrs[i]}|`); // need to test the checkbox value if(values[condition_attrs[i]] != 0) { conditionNames.push(condition_attrs[i]); //push the attribute name into the collection array } } var output = conditionNames.join(", "); setAttrs({ current_conditions: output }); }); });
1558483401
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
Testing it now... Should: conditionNames.push(condition_attrs[i]); //push the attribute name into the collection array be conditionNames.push(conditions[i]); //push the attribute name into the collection array
1558483530
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
The final working version!!! var conditions = [ "Stunned", "Shock", "Mentally Stunned", "Bleeding", "Reeling", "Mortally Wounded", "Coughing", "Drowsy", "Drunk", "Euphoria", "Nauseated", "Moderate Pain", "Severe Pain", "Terrible Pain", "Typsy", "Agony", "Choking", "Dazed", "Ecstasy", "Hallucinating", "Paralyzed", "Retching", "Seizure", "Unconscious", "Coma", "Heart Attack" ]; var condition_attrs = conditions.map( c => c.toLowerCase().replace(' ', '_')); on(`change:${condition_attrs.join(' change:')} sheet:opened`, function () { console.log("==============================="); console.log("****!!!Update-Conditions!!!*****"); getAttrs(condition_attrs, function (values) { var conditionNames = []; //We'll collect all the attribute names in here. console.log(` cond count: ${conditionNames.length}`); for (var i = 0; i < condition_attrs.length; i++) { console.log(` cond idlen:${i}`); // changed this to show the current row console.log(` cond id:|${condition_attrs[i]}|`); // need to test the checkbox value if(values[condition_attrs[i]] != 0) { conditionNames.push(conditions[i]); //push the attribute name into the collection array } } var output = conditionNames.join(", "); setAttrs({ current_conditions: output }); }); });
1558491426
GiGs
Pro
Sheet Author
API Scripter
Yay!