This function further tackles down code redundancy, where in an N-dimensional Array (given N ∈{2,3,4}) you're assuming that all N-1 dimensions
(e.g. excluding the bottom Array with the non-Array values) have the same form of 1D-Array (equal element positions).
This is the case for many N-dimensional objects within the CustomLists attribute, e.g. MTXinfo, AlchemyDescription, PostOfficePossibleOrders etc.. Hence making this function pretty useful in the long run, despite the ugly nesting and explicit writing. It should be possible to dynamically have this support any dimension through recursion, but for this usecase scenario it should suffice as it is.
Code: Select all
function ChangeND(dim, KeyName, repl, elem){
/* Args:
dim = Amount of dimensions, can take values 2 to 4 (at 1D there's no reason for such complexity)
KeyName = The respecitve key inside GameAttribute Customlist that we want to iterate
repl = The replacement value
elem = List of Array indices, which elements we want replaced
*/
const NDArr = bEngine.getGameAttribute("CustomLists").h[KeyName];
if(dim === 4){
for(const [index1, element1] of Object.entries(NDArr)){
for(const [index2, element2] of Object.entries(element1)){
for(const [index3, element3] of Object.entries(element2)){
for(i in elem) element3[elem[i]] = repl; // Fill every
NDArr[index1][index2][index3] = element3; // Write back to the 4D Array
}
}
}
} else if(dim === 3){
for(const [index1, element1] of Object.entries(NDArr)){
for(const [index2, element2] of Object.entries(element1)){
for(i in elem) element2[elem[i]] = repl;
NDArr[index1][index2] = element2; // Write back to the 3D Array
}
}
} else if(dim === 2){
for(const [index1, element1] of Object.entries(NDArr)){
for(i in elem) element1[elem[i]] = repl;
NDArr[index1] = element1; // Write back to the 2D Array
}
} else return NDArr; // Else return the original without modifications
return NDArr;
}
Edit: How does this function shine?
Code: Select all
// Nullify alchemy bubble upgrade cost
this['com.stencyl.Engine'].engine.getGameAttribute("CustomLists").h.AlchemyDescription = ChangeND(3, "AlchemyDescription", "Blank", [5,6,7,8]);
changedstuff.push("Nullified alchemy shop, bubble upgrade and vial upgrade cost!");
// Nullify MTX shop cost
this['com.stencyl.Engine'].engine.getGameAttribute("CustomLists").h.MTXinfo = ChangeND(4, "MTXinfo", 0, [3,7]);
changedstuff.push("Nullified MTX shop cost and increment!");
AlchemyDescription and MTXinfo are respectively 3D and 4D Arrays, where one needs all upgrade materials/cost replaced with "Blank" and the other requiring the initial Gem cost and increment/purchase set to 0.