State.variables doesn't work in <script> passage

Twine Version: 2.7.1
SugarCube 2.36.1

Hello everyone,

this one really frustates me, i just want to use the State.variables.xx API in the <script> area of a Twine passage e.g.

if (State.variables.selectedFoodType === "food1" || State.variables.selectedFoodType === "food2") {

But this gets me the Error: State is not defined.

I already tried various fixes, e.g. a lowercase s etc., but nothing works.


I am already using it (State.variables.xx) in the designated Javascript Area and it works perfectly fine, but when i am using it in the <script> Area in a Twine passage, i get an error…


Hi there!

Could you include the whole code for this error? It is a bit difficult to know where things went wrong with such a small snippet.

Some other things that could be relevant:

  • Is $selectedFoodType set somewhere before this code is loaded?
  • And are you using the HTML <script> or the <<script>> macro in the passage? Because only the second will work… (State is a SugarCube specific code, not a general JavaScript)


$selectedFoodType is set in the init passage, nothing else.

I think your second point is the crucial one. I am using the HTML <script> where i use javascript as well and i thought the State.variable is general usable in Javascript…

The reason why i want to use this, is because i want to use a global variable. Is there a different possibility to use/store data in a global TWINE variable in a HTML <script> passage?

<<script>> will run any JavaScript code in passage, whether you use TwineScript (like State or more global variables. But you will always get an error if you try to use the <script> markup with TwineScript (because SugarCube APIs like State or UI is native to SugarCube only).

Please see the Documentation:

Silently executes its contents as pure JavaScript code—i.e., it performs no story or temporary variable substitution or TwineScript operator processing. For instances where you need to run some pure JavaScript and don’t want to waste time performing extra processing on code that has no story or temporary variables or TwineScript operators in it and/or worry about the parser possibly clobbering the code.

Examples from the Documentation:

→ Basic
	/* pure JavaScript code */

→ Modifying the content buffer
	/* Parse some markup and append the result to the output buffer. */
	$(output).wiki("Cry 'Havoc!', and let slip the //ponies// of ''friendship''.");

okay, i understood that i can’t use the State Statement in the <script> passage and why, but how can i use a global Twine variable in the <script> passage?
I just cant make it work…even with the new infos

Be sure you are using doubles - <<script>> not singles <script>.
I copied your initial code. It just needs the brackets. State.variables.selectedFoodType is equvalent to $selectedFoodType. You have to use $selectedFoodType in the passage.

<<if ($selectedFoodType === "food1" || $selectedFoodType === "food2")>>DO SOMETHING<</if>>
1 Like