Just want to reclarify, that my character sheet works for both repeating_sections and non-repeating_sections and outputs the console commands.
while the API/Token Macro will update the repeating_sections and nothing else.... I have a TL;DR at the bottom that summarizes the situation.
Anyway here is the code. so far
<div class="sheet-row">
<div class="sheet-col-1-12 sheet-classaction-row-number sheet-margin-top">1</div>
<div class="sheet-col-1-4">
<input type="text" name="attr_classactionname1">
</div>
<div class="sheet-col-1-12">
<input type="number" name="attr_classactionresource1" value="1" min="0" />
</div>
<div class="sheet-col-1-12">
<input type="number" name="attr_classactionresource1_max" value="1" />
</div>
<div class="sheet-col-1-6">
<select name="attr_classactionrecharge1">
<option value="None">None</option>
<option value="Short Rest">Short Rest</option>
<option value="Long rest">Long Rest</option>
<option value="Other">Other</option>
</select>
</div>
<div class="sheet-col-1-6">
<select name="attr_classactionsource1">
<option value="Not set" selected="selected">---</option>
<option value="Artificer">Artificer</option>
<option value="Barbarian">Barbarian</option>
<option value="Bard">Bard</option>
<option value="Cleric">Cleric</option>
<option value="Druid">Druid</option>
<option value="Fighter">Fighter</option>
<option value="Monk">Monk</option>
<option value="Paladin">Paladin</option>
<option value="Ranger">Ranger</option>
<option value="Rogue">Rogue</option>
<option value="Sorcerer">Sorcerer</option>
<option value="Warlock">Warlock</option>
<option value="Wizard">Wizard</option>
<option value="Feat">Feat</option>
<option value="Other">Other</option>
</select>
</div>
<div class="sheet-col-1-6 sheet-center">
<button type='roll' class="sheet-roll" name="roll_classaction1" value="&{template:5eDefault} {{title=@{classactionname1}}} {{subheader=@{character_name}}} {{subheaderright=Class/Racial/Other ability}} {{freetextname=@{classactionname1}}} {{freetext=@{classactionoutput1}}}">Use</button>
</div>
</div>
The Above is 1 of 20 segments that existed in the ClassAction section of the character sheet (I did not modify, this is how it was) The important attributes here are `attr_classactionrecharge${#}` and `attr_classactionresource${#}` as those are those the only ones I bother to check and change..
Again my checkbox/button on my character sheet does update these when pressed/checked.
Below is my action buttons for the long rests and short rests
<div class="sheet-row">
<div class="sheet-col-4-5 sheet-center sheet-small-label sheet-sub-header sheet-padl sheet-padr">Short & Long Rests</div>
<div class="sheet-row">
<div class="sheet-col-2-5 sheet-padl sheet-align-left sheet-padr">
<input type="hidden" name ="attr_hidden_short_rest" value="1"/>
<input type="checkbox" name="attr_short_rest" value="1"/>
<button type="action" name="act_actionshortrest" title="Click me to take a Short Rest!"><span style="font-family: fontello"></span>:Take Short Rest?</button>
</div>
<div class="sheet-col-2-5 sheet padl sheet-align-right sheet-padr">
<input type="hidden" name ="attr_hidden_long_rest" value="1"/>
<input type="checkbox" name="attr_long_rest" value="1"/>
<button type="action" name="act_actionlongrest" title="Click me to take a Long Rest!"><span style="font-family: fontello">:</span>Take Long Rest?</button>
</div>
</div>
</div>
Again my sheetworker likes the above code as long as i click the checkbox/button from the character sheet.
Below is the repeating_section that I also check
<fieldset class="repeating_classresources">
<div class="sheet-row">
<div class="sheet-col-5-12">
<input type="text" name="attr_ClassResourceName" />
</div>
<div class="sheet-col-1-4">
<select name="attr_ClassResourceRecharge">
<option value="None">None</option>
<option value="Short Rest">Short Rest</option>
<option value="Long Rest">Long Rest</option>
<option value="Other">Other</option>
</select>
</div>
<div class="sheet-col-1-6">
<input type="number" name="attr_ClassResourceTotal" value="1" min="0" step="1" />
</div>
<div class="sheet-col-1-6">
<input type="number" name="attr_ClassResourceTotal_max" value="1" min="0" step="1" />
</div>
</div>
</fieldset>
Above will be updated from the API/token macro and the sheet buttons works great.
Below is API/Token Macro that I call to test to see if it works...
!setattr --mute --name Test Character --long_rest|1
And...
!setattr --mute --name Test Character --short_rest|1
Below is my Sheetworker, You may recognize it GiGs as you helped trouble shoot it originally :)
I was able to get it to work with the other sections eventually.... (atleast from the original checkbox/action button on my sheet)
Also I try to do .toLowerCase on the parts below that were in the for loop but kept getting type error so decided to abandon that for now (my strings are from a strict dropdown so they should all be the same atleast) <== not important or relevant to my concern however.
on(`change:long_rest clicked:actionlongrest`, function (eventinfo) {
//Section of repeating to be checking
let section = "classresources";
//get proper id for repeating sections (repeating_classresources_)
getSectionIDs(`repeating_${section}`, function (idArray) {
let fields = ["ClassResourceName", "ClassResourceRecharge", "ClassResourceTotal", "ClassResourceTotal_max"];
const attrArray = idArray.reduce( (m,id) => [...m, ...(fields.map(field => `repeating_${section}_${id}_${field}`))],[]);
let attrActionArray = [];
//console.log(`====== attrArray: ${attrArray.join(', ')}`);
for(let i = 1;i < 21; i++){
attrActionArray.push(`classactionname${i}`,`classactionrecharge${i}`,`classactionresource${i}`,`classactionresource${i}_max`);
}
//console.log(`====== attrArray: ${attrArray.join(', ')}`);
//console.log(`====== idArray: ${idArray.join(', ')}`);
let CombinedAttrArray = [...attrArray,...attrActionArray];
//console.log(`====== CombinedAttrArray: ${CombinedAttrArray.join(', ')}`);
getAttrs(CombinedAttrArray, function (values) {
const setattrobj = {};
//console.log(`===== values: ${JSON.stringify(values)}`);
idArray.forEach(id =>{
const recharge = values[`repeating_classresources_${id}_ClassResourceRecharge`];
//console.log(`==== id: ${id}; recharge: ${recharge}`);
//check if ClassResourceRecharge has been set to the string "Short Rest" (this attr is set with a dropdown)
// a common source of errors is having mismatched cases, so here we ensure they are both the same case.
if(recharge.toLowerCase() === "long rest" || recharge.toLowerCase() === "short rest" ){
const max = parseInt(values[`repeating_classresources_${id}_ClassResourceTotal_max`])||0;
//console.log(`==== id: ${id}; max: ${max}`);
setattrobj[`repeating_classresources_${id}_ClassResourceTotal`] = max;
}
});
for(let i = 1; i < 21; i++){
if(values[`classactionrecharge${i}`] === "Long rest" || values[`classactionrecharge${i}`] === "Short Rest"){
setattrobj[`classactionresource${i}`] = 0;
}
}
//check if empty or not
console.log(`=== values: ${JSON.stringify(values)}`);
console.log(`=== setattrobj: ${JSON.stringify(setattrobj)}`);
// add short_rest here so that it gets updated along with the rest.
setattrobj.long_rest = 0;
// since setattrobj will now always contain something, no need for the if statement
setAttrs(setattrobj);
});
});
});
on(`change:short_rest clicked:actionshortrest`, function (eventinfo) {
//Section of repeating to be checking
let section = "classresources";
//get proper id for repeating sections (repeating_classresources_)
getSectionIDs(`repeating_${section}`, function (idArray) {
let fields = ["ClassResourceName", "ClassResourceRecharge", "ClassResourceTotal", "ClassResourceTotal_max"];
const attrArray = idArray.reduce( (m,id) => [...m, ...(fields.map(field => `repeating_${section}_${id}_${field}`))],[]);
let attrActionArray = [];
for(let i = 1;i < 21; i++){
attrActionArray.push(`classactionname${i}`,`classactionrecharge${i}`,`classactionresource${i}`,`classactionresource${i}_max`);
}
const CombinedAttrArray = [...attrArray,...attrActionArray];
//console.log(`====== attrArray: ${attrArray.join(', ')}`);
//console.log(`====== idArray: ${idArray.join(', ')}`);
//console.log(`====== CombinedAttrArray: ${CombinedAttrArray.join(', ')}`);
getAttrs(CombinedAttrArray, function (values) {
const setattrobj = {};
//console.log(`===== values: ${JSON.stringify(values)}`);
idArray.forEach(id =>{
const recharge = values[`repeating_classresources_${id}_ClassResourceRecharge`];
//console.log(`==== id: ${id}; recharge: ${recharge}`);
//check if ClassResourceRecharge has been set to the string "Short Rest" (this attr is set with a dropdown)
// a common source of errors is having mismatched cases, so here we ensure they are both the same case.
if(recharge.toLowerCase() === "short rest" ){
const max = parseInt(values[`repeating_classresources_${id}_ClassResourceTotal_max`])||0;
//console.log(`==== id: ${id}; max: ${max}`);
setattrobj[`repeating_classresources_${id}_ClassResourceTotal`] = max;
}
});
for(let i = 1; i < 21; i++){
if(values[`classactionrecharge${i}`] === "Short Rest"){
setattrobj[`classactionresource${i}`] = 0;
}
}
//che
//check if empty or not
console.log(`=== values: ${JSON.stringify(values)}`);
console.log(`=== setattrobj: ${JSON.stringify(setattrobj)}`);
// add short_rest here so that it gets updated along with the rest.
setattrobj.short_rest = 0;
// since setattrobj will now always contain something, no need for the if statement
setAttrs(setattrobj);
});
});
});
Below is the console.log(); when i click the checkbox/action button (for long_rest, also activates short_rest mechanics too).. The repeating_sections should be set to numbers while the non-repeating_sections should be set to 0 (as repeating is set to "current" while non repeating is set to "Used", might change in the future but that's a minor fix for now)
=== setattrobj: {"repeating_classresources_-maryrmz3mlkngqkppu7_ClassResourceTotal":8,"repeating_classresources_-maryv-qmyllwt0kjtc8_ClassResourceTotal":12,"repeating_classresources_-marywotav2skxc5fcmt_ClassResourceTotal":9,"repeating_classresources_-maryygafgvhwkxmkp1r_ClassResourceTotal":17,"classactionresource1":0,"classactionresource2":0,"classactionresource3":0,"classactionresource4":0}
Again the biggest/weirdest part is that the API/Token will change my character sheet for the repeating_section, but it doesn't output the console.log(); when using the API/token macro and it doesn't update the non-repeating_sections
tl;dr
- Use API/Token macro (API is ChatSetAttr) long_rest or short_rest
- repeating_section resources DO update correctly.
- Console.log() that exists in the sheetworkers DO NOT get called/execute
- non-repeating_section resources DO NOT update
- Use Character sheet checkbox/action button for long_rest or short_rest
- repeating_sections resources DO update correctly.
- Console.log() that exists in the sheetworkers DO get called/execute
- non-repeating_section resources DO update.