Skip to content

abadonna/defold-ink

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

74 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

defold-ink

The Ink language runtime implementation in Lua, an alternative to Narrator, based on parsing ink JSON files.

Version

Works with Inky 0.14.1/ink v.1.1.1

Please note: json ink format sometimes changes, so check your version.

Example

local ink = require "ink.story"

-- Parse a story from the JSON file. Make sure it's UTF8!
local res = sys.load_resource("/assets/test.json")
local story = ink.create(res)

-- Begin the story
local paragraphs, answers = story.continue()

-- Output text to the player
for _, p in ipairs (paragraphs) do
	pprint(p.text)
end

if #answers > 0 then
	for i, a in ipairs (answers) do
		pprint("[" .. i .. "] " .. a.text)
	end
end

 -- Send answer #1 to the story to generate new paragraphs
paragraphs, answers = story.continue(1)

Defold

You can use defold-ink in your own project by adding it as a Defold library dependency. Open your game.project file and in the dependencies field under project add:

https://github.com/abadonna/defold-ink/archive/master.zip

Then you can require the ink.story module.

Alternatively - try this defold project template that provides a good start for creating game with Ink and Defold.

Documentation

create(json)

Parses the Ink json string and returns a story instance. Make sure it's UTF8!

local ink = require "ink.story"
local res = sys.load_resource("/assets/test.json")
local story = ink.create(res)

story.continue(answer_index)

Returns paragraphs and choices. Parameter "answer_index" is ignored if the story just begins.

local paragraphs, choices = story.continue(1)

story.add_observer(var_name, f)

Assigns an observer function to the global variable. Each global variable can have multiple observers.

story.remove_observer(var_name, f)

Removes an observer function.

story.assign_value(name, value)

Assigns value to global variable and calls all it's observers.

story.variables

Just a table of all global variables you can read and set. Use story.assign_value(name, value) to notify observer functions about the change.

story.eval(expression)

Returns a result of evaluation of string expression, all names of global variables will be replaced by values.

Saving and loading

Story actually replays from the start with saved user choices, similar to what you see in Inky editor while modifiyng ink script. But it happens more correct way - as we keep random values as well.

story.get_state()

Returns the current state of the story. Can be saved and used to restore story later.

local filename = sys.get_save_file("inktest", "story")
sys.save(filename, story.get_state())

story.restore(state, with_externals)

Restores story from the saved state. Story should be created with the same - or at least similar :) - json. Returns it's last paragraphs and choices. Set parameter "with_externals" to true if you want external functions to be called during restoring, e.g. you have some calculations in lua code.

local story = ink.create(json_string)
local paragraphs, choices = story.restore(state)

story.serialize(), story.deserialize(data, path, reset_observers)

An alternative way to save and restore the state. More reliably to script changes, but you have to pass "path" to jump, as the story just restores variables. Good option if your game can save only in the predefined moments\places. reset_observers can be omitted in most cases so ui will update on new values. !Not compatible with previous method!

Multiple parallel flows

It is possible to have multiple parallel "flows" - please read this for more details.

story.switch_flow(name)

Creates a new flow or switches to an existing one. If name is nil - it will goes back to the default flow.

story.jump(path)

You can jump to a particular named knot or stitch.


About

ink implemetation for defold

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published