JavaScript + Ink: Part 3: Getting and Setting Variables

JavaScript + Ink:

ink is a scripting language for creating interactive narratives. It can be written using the editor Inky. The inkjs NPM module is a JavaScript port of the ink engine.

Getting and Setting Variables

In the previous part, the Story API was discussed as a way to access and make choices about the story. While the Story API provides access to the Story object and its properties and functions, global variables can also be accessed through the variablesState object.

VAR name = "Dan"
It was our first date and we hadn't made dinner plans.
-> Pizza_Choices
=== Pizza_Choices ===
He turned to me. "What should we eat, {name}?"
+ [Pizza?]
-> Pizza
+ [Sushi?]
-> Sushi
+ [Salad?]
-> Salad
+ [Nothing?]
-> Nothing
=== Pizza ===
He shook his head. "I don't like pizza."
-> Pizza_Choices
=== Sushi ===
"Sushi sounds good!"
=== Salad ===
"Not a salad"
-> Pizza_Choices
=== Nothing ===
"We have to eat something!"
-> Pizza_Choices

Once a story has been created, its variables can be accessed through a Proxy via the variablesState property on the Story object. That is, the variables are not technically a part of the object, but act as a way to gain access and change their values.

For example, to change the variable name in the above code, it could be accessed via its name, name: variablesState[“name”]. Changing this value would change it in the story itself.


var Story = require('inkjs').Story;
var json = require('./ExampleStory.json');

var inkStory = new Story(json);

inkStory.variablesState["name"] = "Steve";


  It was our first date and we hadn't made dinner plans.


  He turned to me. "What should we eat, Steve?"

Through accessing or changing the values of variables, the variablesState property acts as an interface between the story and other, outside code. It would be possible, then, to wrap the Ink code in a larger interface that showed the values of certain variables or allowed, for example, for a player to enter their name upon starting the game.