- Part 1: Ink for Web
- Part 2: Story API
- Part 3: Getting and Setting Variables
- Part 4: Calling Ink Functions
- Part 5: Building an Interface
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().
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?" */