As a precursor, I have been spending hours researching this, scouring the Twine forums, w3schools for Javascript, the Twine 2 wiki, and SugarCube 2 docs, so this is kinda my last resort.
Firstly, I have found next to no documentation on creating custom Javascript functions and using them. Everything I have learned is through looking at examples, and with my own knowledge of JS.
I set out trying to learn how to take an array of objects, such as:
[ { name: "Gold", value: 100 }, { name: "Silver", value: 10 }, { name: "Copper", value: 1 } ]
And pass it into a custom JS function to get the sum of all of "value" properties.
I created a function and attached it to "window" (a trick that doesn't seem to be explain anywhere) which looked like this:
window.SumValue = function (list) {
var v=0;
for(var i=0; i<=list.length; i++) {
v+=list[i].value;
}
return v;
}
(I put this in my "Edit Story Javascript" area of my Twine project.)
Then, in attempted to use the function as follows:
<<set $list to [ { name: "Gold", value: 100 }, { name: "Silver", value: 10 }, { name: "Copper", value: 1 } ]>>
<<print SumValue($list)>>
The intended result was to print the number: 111 to the screen when the passage loaded (100 + 10 + 1), instead I get an error that reads, "Error: <<print>>: bad evaluation: Cannot read property 'value' of undefined".
If anyone has some answers for me, even if it's just pointing me in the direction of the documentation on how work with JS and Twine/Sugarcube, I would most appreciate it.
I am using
Twine 2 (offline),
SugarCube 2.12.1 (local/offline).
Lastly, I would prefer to not have to use widgets to accomplish this, as they are bulky, do not provide a return method, and generally aren't good for internal workings.