Hi. I'm new to Twine and I've spent the last three hours trying to find something in the documentation or forum that explains my question, but I've completely failed. I'm sure this has been answered somewhere before. I'm sorry for asking again.
I want to run loops manipulate arrays in javascript using functions, and return those values for use in my normal twinecode. I really only need like three java functions for the game I'm planning on setting up, but I can't figure out how to call them from the Edit Story Javascript area. I read that for Harlowe I needed to use a global object, so I just sort of did that here for Sugarcube. IDK what I'm doing at all.
Can anyone explain to me how to make the following function run in Twine? Thank you so much for your time.
if (! window.rollevent) {
window.rollevent = function() {
var sum = 0;
for ( i = 0; i < events_All.length; i++) {
var sum = sum + window['event_'+events_All[i]]
};
var i = 0;
var total = 0;
var random = Math.floor( Math.random() * sum );
while (total < random) {
var total = total + window['event_'+events_All[i]];
var choice = events_All[i];
i++;
};
return choice;
}
}
Comments
You would these call this macro by putting <<myCustomMacro>> somewhere in a passage.
After this macro run, you would have a story variable called $exampleText that could be printed out using
<<= $exampleText>>
I meant to say I'm not sure whether you intend for that value to be stored for later or if it is meant to be printed out into the passage.
That said, the function causes no side-effects and only returns a value, so the the real question here—somewhat clumsily introduced by Rokiyo—is what do you want to do with its return value?