<<script>> function suddenly not working

Twine Version: 2.10.0
SugarCube 2.37.3

Have a script function I used to give players some options they could toggle between back and forth to enable or disable features of the game that has suddenly stopped working, it also pops up a little yellow text to tell them exactly what they did and if the feature is now on or off. I made this like four or five years ago, maybe longer, and its functioned fine until just now when I started testing a new version of my game before release.

I have no idea how I made this or how it functions exactly since its been so long so its kinda hard for me to debug this, I’m not really a coder I just fumble my way through cobbling things together as I need them. I tried to check and see if the script function in sugarcube had been deprecated or changed, but it hasn’t, they’ve just added the option to make it twinescript or javascript, which shouldn’t affect this? (Though I did try changing it between the two in case the new default was the other one, but nothing changed.)

Here is the error I see in the browser when clicking the option link:

Error: <<script>>: bad evaluation: state is not defined
<<script TwineScript>>state.display(state.active.title, null, "back")<</script>>

Here is the full twine function in case it helps:

<<if $portraits == false>><<linkreplace "Enable Portraits">>@@.yellow;Portraits have been enabled.@@<<set $portraits to true>><<script>>state.display(state.active.title, null, "back")<</script>><</linkreplace>><<else>><<linkreplace "Disable Portraits">>@@.yellow;Portraits have been disabled.@@<<set $portraits to false>><<script>>state.display(state.active.title, null, "back")<</script>><</linkreplace>><</if>>

Thank you in advance for any help, sorry if I forgot to include something.

If you review the Updating to any version ≥2.5.0 from a lesser version documentation, specifically the State API section, you will learn that:

  • the State.display() method was replaced with the Engine.display() method.
  • the Engine.display() method was replaced with the Engine.play()

And If you review the Updating to any version ≥2.37.0 from a lesser version documentation, specifically the Deprecated legacy APIs section, you will learn that:

  • the state API no longer exists.
  • the State.display() method no longer exists.

So the reason your state.display(state.active.title, null, "back") line of code isn’t working, is because you’re calling a non-existing method on a non-existing API.

Replacing your existing state.display() method calls, with an equivalent Engine.play() call that has the correct arguments, should fix that issue.

notes:
1: the correct syntax for checking the Boolean state of a variable (like your $portraits) is:

/* checking for true */
<<if $portraits>>the variable equals Boolean true<</if>>

/* checking for false */
<<if not $portraits>>the variable equals Boolean false<</if>>
or
<<if ! $portraits>>the variable equals Boolean false<</if>>

2: Using == instead of === as a comparison operator allows JavaScript to convert the data-type of the values being compared, which is know as coercion. Which allows JavaScript to do thing like the following…

/* define a String value */
<<set _thing to "false">>

/* compare that String value to a Boolean, using the coercion allowed operator */
<<if _thing == false>>this will be shown, because false can be Stringified into "false"<</if>>

/* compare that String value to a Boolean, using the exactly equals to operator */
<<if _thing === false>>this will NOT be shown, because because the value data-types are different, as are the values themselves<</if>>
1 Like

Ah, of course, thank you, I should have checked to see if the smaller pieces had been deprecated. For some reason I assumed those wouldn’t be, foolish of me, I’ve got it working now.