JavaScript + Ink: Part 4: Calling Ink Functions

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.

Calling Ink Functions

In the second part of this series, the Story API was discussed. In the third, the property variablesState was shown. Between the two, a number of actions can be performed on a story to get its text, choices, and change the values of its global variables. It is also possible to call Ink functions in the story.

The function EvaluateFunction() is used to call Ink functions. It accepts the name of the function as a string, an optional array of arguments, and if both the result of the function and any text it would have displayed should be returned.

As the documentation mentions, if the third parameter is true, the returned object with have both returned, the returned results, and output, the text of the function, as its properties.

Building an Ink Interface

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
=== function changeName(a) ===
~ name = a

Since it is possible to write functions that change the values of variables within Ink, the use of EvaluateFunction() means that they can be called from outside of the story with values passed into the function via the call.

In other words, it is possible to create an interface within an Ink story to changes its variables via different functions. These can then be called via EvaluateFunction() and passed values. Instead of needing to use variablesState directly, values can be changed and other work done from within the Ink story via different usages of EvaluateFunction().


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

var inkStory = new Story(json);

inkStory.EvaluateFunction("changeName", ["Steve"]);


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


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