Hoping somebody can help here...
I'm now trying to use the suggestion made by GiGs above to reduce the number of copies of similar code in my sheet worker script by using the foreach... approach, but I'm having syntax problems...
Here's what I've got:
var cutlass = {name:"Cutlass", tl:"2", damage:"3d6", weight:"4", cost:"200", ap:"0", bulky: "0", veryBulky:"0", singleUse:"0", psi:"0" };
var dagger = {name:"Dagger", tl:"1", damage:"1d6+2", weight:"1", cost:"10", ap:"0", bulky: "0", veryBulky:"0", singleUse:"0", psi:"0" };
var greatAxe = {name:"Great Axe", tl:"2", damage:"4d6+2", weight:"10", cost:"750", ap:"0", bulky: "0", veryBulky:"1", singleUse:"0", psi:"0" };
var bladeWeapons = [ cutlass, dagger, greatAxe ];
var club = {name:"Club", tl:"1", damage:"2d6", weight:"3", cost:"0", ap:"0", bulky: "0", veryBulky: "0", smasher:"0", singleUse:"0", stun:"0" };
var mace = {name:"Mace", tl:"1", damage:"2d6+2", weight:"3", cost:"20", ap:"0", bulky: "1", veryBulky: "0", smasher:"1", singleUse:"0", stun:"0" };
var staff = {name:"Staff", tl:"1", damage:"2d6", weight:"3", cost:"0", ap:"0", bulky: "0", veryBulky: "0", smasher:"0", singleUse:"0", stun:"0" };
var bludgeonWeapons = [ club, mace, staff ];
['blade','bludgeon'].forEach(function(wpntype) {
on (`change:repeating_${wpntype}:${wpntype}-name sheet:opened`, function() {
console.log("=======================================");
console.log(`Entering new ${wpntype} listener`);
getAttrs([`repeating_${wpntype}_${wpntype}-name`], function(values) {
for (i in values) console.log(`...retrieved ${i}: ${values[i]}`); // returns expected value(s)
console.log ("...checking ",`${wpntype}Weapons`.length, " weapons..."); // returns expected value
console.log ("...looking for ",values[`repeating_${wpntype}_${wpntype}-name`]); // returns expected value
for (var i = 0 ; i < `${wpntype}Weapons`.length ; i++) {
console.log("...trying ", `${wpntype}Weapons`[i].name); // returning 'undefined'
if (`${wpntype}Weapons`[i].name == values[`repeating_${wpntype}_${wpntype}-name`]) {
console.log("...found it at position: ",i);
//
// more stuff will go here when this problem is solved.
//
break;
}
}
console.log("...done looking");
});
});
});
and here's the result when I try to define a blade on the character sheet...
=======================================
Entering new blade listener
...retrieved repeating_blade_blade-name: Cutlass
...checking 3 weapons...
...looking for Cutlass
...trying undefined // 3 repeats
...done looking
So... the correct version of the listener fires (the 'blade' version)... and the syntax `${wpntype}Weapons`.length works fine to get the number of items in the bladeWeapons array. And `repeating_${wpntype}_${wpntype}-name` retrieves the proper weapon name from the character sheet.
But when I try to do a name check to select the correct one of the three, all I get is 'undefined'.
So... `${wpntype}Weapons`[i].name apparently isn't the correct syntax to find the entry in the bladeWeapons array that matches the weapon name brought in by the getAttrs function. But what is the correct syntax?!!! I've tried about 30 variants involving (both dot notation and bracket notation for the name property... moving the reverse quotes and/or braces around...but I haven't hit on the syntax that works. Once I have that syntax, the intention is to retrieve selected property values and setAttr them to the character sheet.
(NOTE: data arrays at top have been simplified for showing here. There are more than 3 entries, and there are currently 7 full arrays... but 2 arrays of 3 each is enough to explain my problem.)
Any help is greatly appreciated.