Twine Version: Using Twine 2.7.0 and SugarCube 2.36.1
Hi there! I apologize for asking what will most likely be a simple question. For reference, I have searched through this forum, general Google queries, and even asked ChatGPT for help, and I keep hitting the same brick wall. I should also point out that I am generally a programming novice, so much of the code I have been seeing is not extremely intuitive, but I’m figuring it out.
GENERIC PROBLEM - My problem is thus - I cannot get ANY Javascript function to operate correctly in a Twine game.
SPECIFIC PROBLEM - I was looking for an easier way to add elements to an array using a Javascript function. These elements are named statuses that the player can pick up while traversing my game. The statuses the player gains are checked at various points. Twine is saying every attempt to run the Javascript function is undefined, however.
CURRENT SUCCESS - I am able to add these statuses just fine right now, without Javascript. In the StoryInit passage I initialize my array thusly:
<<set $statuses to []>>
Then in a relevant passage, when a status is gained, I have:
<<nobr>>* Gain status A1.
<<if $statuses.includes('A1 - Garik is pleased')>>
<<else>>
<<set $statuses.push('A1 - Garik is pleased')>>
<</if>><</nobr>>
Again, this works just fine. However I realized that I could probably do this slightly easier with a Javascript function. Instead of doing the whole code every time, turn it into a function and just run the function with the new status name. So I added this code to the Story JavaScript section of Twine:
// Story JavaScript section
function gainStatus(statusName) {
if (!$statuses.includes(statusName)) {
$statuses.push(statusName);
}
}
Then, in the relevant passage, I replaced the old code with:
<<run gainStatus('A1 - Garik is pleased')>>
However, when that happens and I reach that passage, I receive the error
“Error: <>: bad evaluation: gainStatus is not defined”
I troubleshot the best that I could, attempting to even change the Javascript code to:
window.gainStatus = function(statusName) {
if (!$statuses.includes(statusName)) {
$statuses.push(statusName);
}
}
Then I tried to call ANY Javascript function and I kept getting similar error messages that the function was not defined. Yes, the Javascript code is being placed in the right location, and yes, the game language is in SugarCube.
Since my old code is working fine I’m not too worried, but I am perplexed as to why I can’t get a Javascript function to operate as it should. I am sure there is some simple thing I am overlooking, to which I deeply apologize. But any light that someone could help shed on this situation would be greatly appreciated.