- 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
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.
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"; 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?" */
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.