You’re using Twine 2 with the SugarCube story format for your interactive fiction story, but JavaScript is throwing curveballs your way. Functions that work like a charm elsewhere mysteriously trigger cryptic error messages. The culprit? JavaScript scope.
The Story JavaScript area, any JavaScript file in the compile path of a Twee notation project, or code written within SugarCube’s <<script>> tags operates in a “Private Scope.”
Imagine a private scope in JavaScript like a private room in a house. It’s a space where everything you put inside (your functions and variables) is only available to those who are inside the room. This privacy sounds great for security, but not so much when you want your code to mingle with other parts of your Twine story.
To make your code less shy, you need to raise its scope from private to global. The global scope in JavaScript is like a community park that everyone in the neighborhood can access. When you define a function or a variable in the global scope, it becomes accessible from anywhere in your script.
There are several methods that you can use to elevate your functions to the global scope.
One simple and common way to elevate a function or variable to the global scope in a Twine 2 or SugarCube project is by attaching it to the window
object. The window
object is a built-in global object in web browsers that represents the browser’s window. Anything attached to this object becomes globally accessible across your project.
Here’s how you can do it:
window.myGlobalFunction = function() {
// Your function code
};
You can execute the function above like this (in SugarCube): <<run window.myGlobalFunction()>>
Creating your own namespace on the window
object is a great way to organize and manage your global functions and variables. A namespace is essentially a container for a set of identifiers (functions, variables), which helps to avoid conflicts with other global identifiers in the JavaScript environment. This method is particularly useful in larger Twine projects where you need a structured approach to managing your global code.
Here’s an example of defining a unique namespace:
window.MyNamespace = window.MyNamespace || {};
window.MyNamespace.myGlobalFunction = function() {
// Your function code
};
This code first checks if MyNamespace
already exists on the window
object. If not, it initializes it as an empty object. Then, it adds myGlobalFunction
to this namespace. You can call this function using <<run window.MyNamespace.myGlobalFunction()>>
in SugarCube.
SugarCube’s setup
object is another effective way to manage global code. This object is intended for storing data and functions that need to be accessible across your entire Twine story. Unlike the global window
object, setup
is specific to SugarCube, making it a more tailored choice for Twine developers.
To use the setup
object:
setup.myGlobalFunction = function() {
// Your function code
};
You can then call this function in SugarCube using <<run setup.myGlobalFunction()>>
.
While global scope is powerful, it’s important to use it wisely by keeping in mind the following best practices: