Your browser lacks required capabilities. Please upgrade it or switch to another to continue.
Blue Squirrel's Twine Game Tutorials, Part 4
Hello, and welcome to my Twine tutorial series. I'm Blue Squirrel. I've been writing interactive fiction since the days when the best tool for it Inform 6, and I'm hoping that with these quick tutorials, you can get started writing them too!
First things first, if you are seeing this as a HTML game, that's great. Feel free to poke around and see how the game looks. When you're ready, save the page as a file using CTRL-S on your browser.
Then, download Twine from https://twinery.org/ and open this file in there, to get started editing and changing the game.
Tutorial 4: Randomness
Welcome to part 4. Hopefully now you know how to make both passages, and set variables. That's the basis of most everything you'll be doing in Twine.
In this tutorial, we will add something that almost all games use in some way. Randomness! This is one of the main reasons I like to use Sugarcube 2, as it has a very handy random feature.
We'll also quickly show off setting variables that aren't numbers, but are instead text or on/off values.
[[The Cave]]This sure is a spooky cave we're in right now.
Let's set up all the variables I am going to want for this game.
<<set $haskey to false>>
<<set $species to "human">>
The haskey variable is "false" or "true", and tells us if the player found the key item yet.
The species value is text that we can show whenever we want. It'll be useful in the mirror room.
[[Deeper in the cave]]There's a bunch of paths here! And a big metal door that hopefully is the way out...
Where shall we go?
Haskey is true or false, so we can use it like this:
You can unlock the door!
The door is locked.
<</if>>This room has a random magical effect! That's my favourite.
We're going to do a few things here. First, we're going to set a new variable to use in this room, and call it random. We're going to set it using the random function in Sugarcube. This lets us define a number, and then it will set the value to a number from 0 to that number. So random (3) will set it to either 0, 1, 2, or 3, at random.
Do you remember nobr from the previous tutorials? We're going to use that too! But a little differently here. I'm going to say I only want nobr on this bit of text below:
<<set $random to random(3)>>
<<if $random == 0>>
<<set $species to "fox">>
The twisty room transforms you into a fox!<br>
<<if $random == 1>>
<<set $species to "wolf">>
The twisty room transforms you into a wolf!<br>
<<if $random == 2>>
<<set $species to "cat">>
The twisty room transforms you into a cat!<br>
<<if $random == 3>>
<<set $species to "bull">>
The twisty room transforms you into a bull!<br>
Now the mirror room will tell us what species we currently are.
[[Back to the cave|Deeper in the cave]]There is a big mirror here. When you look in it, you see a $species.
Did you see how we used the text variable with the dollar sign? That will insert whatever the text variable is into what the player sees.
[[Back to the cave|Deeper in the cave]]Oh there's a key on the floor here. Let's take that.
<<set $haskey to true>>
If the player has already been here, we could use an if statement with haskey to say the room is empty, instead. Can you see how we would do that? Maybe try editing this passage to fix it!
[[Back to the cave|Deeper in the cave]]You escape!
That's it for this tutorial. If you didn't try all the rooms, give them a look to see how they use variables in different ways. See you in the next part!