Science Oxford Logo

Add Stats

Within Twine, you can use coding structures such as variables and if statements, to make your story more like a game.


Step 1: Setting up a variable

In this example, I am going to give my reader some lives - if they make the wrong choice they will lose a life and risk having to start their adventure again!

Firstly, we need to set the variable - give it a starting value.

Open the first paragraph of your story, and add this code.

(set: $lives to 3)

The reader now has 3 lives when they start the story.


Step 2: Using the variable

Now we need to create a situation where your character might lose a life.

At a relevant part of your story, create a branch that leads somewhere perilous, perhaps the character falls down a hole if they choose the wrong path.

Once you have written this new paragraph, add the following code.

(set: $lives to $lives - 1)

This will take one life away from your reader!


Step 3: Creating consequences

What happens when your reader uses up all three of their lives?

After you have taken a life away from your reader, you can check how many lives they have left, and make something happen if they have run out.

In the same paragraph as before, add this new code underneath the previous code.

(if: $lives is > 0)[Phew, that was close... [[Continue]]]
(else:)[Oh no... [[The End]]]

Now, different things will happen depending on whether the reader has used up all their lives or not.

There are lots of brackets here now, so it's easy to miss some out - remember to close any bracket that you open.


Test out this code and see if you can work out what is happening.

Then edit it to make it fit your story!

Do things one step at a time, and test your story every time you make a change.

What other ways could you use variables?