Adding new variables to your game


Did you know you can add, track and change any variables you want in your text-engine adventure games? In this post, I will show you how!

A disk in text-engine is really just a JavaScript object, so you can attach any arbitrary data you want to it.

Let's imagine we want our player to have health, and we want to add a poison that decreases that health when the player drinks it. Let's use the Unlimited Adventure disk that comes with the engine and look at how we can modify it to achieve our goals.

Currently, Unlimited Adventure's disk looks like...

const unlimitedAdventure = {roomId: 'gameOver', inventory: [], rooms: [...]}

We can simply add a health property like so:

const unlimitedAdventure = {roomId: 'gameOver', health: 100, inventory: [], rooms: [...]}

Now, when we are defining the room the player finds the poison in, we can simply add it to the items array like this:

items: [{ name: 'poison', desc: 'It looks bad for you.', use: () => disk.health -= 75 }]

When the player enters this room, if they issue the command use poison, their health will drop 75 points to 25

You could do more with this method if you wanted. For instance, you could check if disk.health is now less than or equal to 0, and if so, move the player to a room you created to serve as the game over screen.

To do this, add the following line to your use method:

if (disk.health <= 0) enterRoom('gameOver')

Taking the poison twice would surely send a player to the game over screen.

Or, you could set up a room with a character who offers to heal the player if they enter with, say, 50 health or less.

onEnter: () => {
  if (disk.health <= 50) {
    println('You look injured. Allow me to heal you.');
    disk.health = 100;
 }
}

Or perhaps the character simply gives the player an antidote which they can take at any time. Just push the item into the player's inventory:

disk.inventory.push({ name: 'antidote', desc: 'Counteracts the effects of poison.', use: () => disk.health = 100 })

I hope this is helpful! If you have any questions about this or any other text-engine topic, feel free to get in touch over on Twitter at @okaybenji.

Get text-engine

Leave a comment

Log in with itch.io to leave a comment.