Call constants

Dear reader I am trying to make my first twine game with sugarcube settings. When trying to play the game I get the following error: " Error: <<=>>: bad evaluation: ‘constants’ is not defined <<= constants.version>>"

This is the code which calls the version

<div class="title-screen-version-number">
	version <<= constants.version>>
</div>

This is what the passage “constants” looks like:

window.constants =
{	
	version: "1",

	max_hiscores: 10,

	continue_text: "Continue",

	names: 
		[ 	"John",
			"Kevin"] } 
2 Likes

Raw JavaScript like that should normally be written in the JavaScript section (accessed through the bottom menu in Twine), instead of in a normal passage. The JavaScript section gets executed when the story first starts, so that would set the window.constants object for you before your code tries to display it.

If you do want to have JavaScript within a passage, then it must be bracketed by <<script>> macros like this:

<<script>>
	alert("JavaScript test");
<</script>>

Note that any JavaScript in a passage will only get executed at the time when the passage is displayed.

Also, because you’re using a non-story variable to store data, it won’t get saved in the game’s history. If you make any changes to it during the game then those changes will get reset the next time the person plays the game, and won’t be undone by hitting the “back” button or loading a saved game. (That shouldn’t be a problem if your constants really are constant.)

For more information on using JavaScript in Twine/SugarCube, see the “Using JavaScript with SugarCube” section of my sample code collection. (Click “Jump to Start” in the UI bar to see other sample code there.)

Hope that helps! :slight_smile:

1 Like

Will test it! Will also read the article! Thanks :slight_smile:! will report back if it works.

edit:
Worked! Such an easy solution, will read the article and I guess the tutorial again :wink:! Thank you so much!