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

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().

index.js


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

var inkStory = new Story(json);

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

inkStory.Continue();

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

inkStory.Continue();

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.