Writing interactive fiction in Lua

I wanted to write a story in the vein of the text adventures and fighting fantasy books that I grew up with during the 1980’ies. There was something magical in opening up a new book in the Lone Wolf series by Joe Dever and become immersed in a story with simple format; text, rudimentary player decisions and a set of compelling illustrations to set the atmosphere.

My first exploration of ideas were made in Twine, which is a free software that is easy to use for starting up an interactive fiction experience. However, I soon realized that my ideas involved some more complex gameplay mechanics that I would like to implement, and while some of them would be achievable in Twine, I decided I would rather get back into programming; the benefit is having far greater control over the story development and player experience.

Why Lua?

The short answer is that I find it an enjoyable environment to work in, it’s free and it has easy readability. If I am going to spend a few years in the code I want to buckle up with not only coffee, but making sure I am having as much fun as possible during the journey. If Lua isn’t giving you the right spark for your creative ideas then make sure to pick something that does.

While Lua doesn’t have classes like C, it offers tables and meta-tables where you can build complex class-like structures when necessary, with the option to integrate C functions for additional speed, if needed. The main example that comes to my mind is NPC pathfinding, but apart from that, speed is not a central requirement in a traditional style interactive fiction game.

Lua options

There are many easy-to-use free options for writing in Lua, such as Love2d, Corona, Gideros and a ton more. I have had positive experiences with Löve/Love2d, but decided I will write my early game prototype in Gideros since it is a platform that is easy to get started with and I have found it rewarding so far.

My initial focus is building the data structure and basic mechanics that the game will rely on. At a later point I may migrate my code to another Lua platform so I will attempt to keep the code flexible and easy to change.

For this type of game I consider the data structure to be of a more fundamental value than the particular language I use for coding; developing the data structure is what takes most time and consideration. The actual coding is more or less just spending time implementing the desired structure.

When it comes to graphics my needs for the game are simple; illustrations combined with text and a user input field. While Love2d have better graphical capabilities than my chosen platform, Gideros provides the necessary functionalities that I currently require for my project and I find its framework fairly effective and fun to work within.

————

The fun part – starting out!

I will be using both standard and Gideros Lua for my examples. Gideros have a couple of Lua enhancements and I will list a few of them here:

–– Standard Lua code example:

playerHealth = playerHealth + 1
playerHealth = playerHealth + 10

-- Gideros mutation operators with the same result:

playerHealth += 1
playerHealth += 10

Documentation link: Gideros enhancement: mutation operators

–– Syntax for arrays, standard Lua code:

array[1][5] = 2

–– In Gideros, the array dimensions can be written as either:

array[1, 5] = 2
array[1][5] = 2

Documentation link: Gideros enhancement: easy arrays

–– Gideros larger and smaller operators examples:
x = a < > b –– Compares a with b, return the largest number.
      –– This method is faster than x = math.max(a, b)
x = a > < b –– Compares a with b, return the smallest number.
      –– This method is faster than x = math.min(a, b)
x = (x – 1) < > 5 –– Decrement x, but not below 5.

x = (x + 1) > < 15 –– Increment x, but not above 15.


Documentation link: Gideros enhancement: greater-smaller

———

In the next post I will continue with some more direct examples for writing an interactive fiction game in Lua.