0 votes
asked by (640 points)
I want to a write a scene which has multiple outcomes based on the value of a pre-defined variable. Let's call it variable X.

I want the player to click on a [[link]] and be directed to a passage based on the outcome of variable X's evaluation.

For example: if Variable X=5 then go to passage 1.

or If Variable X=4 then go to passage 2.


I want the 'same' [[link]] to be used for this purpose.

How do I do this?

user greyelf told me to use the <<if>> macro but I can' figure out how.

Any help is appreciated.

1 Answer

+2 votes
answered by (1.2k points)

Ooh! A question I might be able to help with!

I can think of several ways to do it. Here's how I'd do it using If:

<<nobr>> /* I like to use nobr so I don't need to worry about lots of whitespace. */

<<if $variableX is 0>>

<<elseif $variableX is 1>>



That'll evaluate the variable before the passage is shown and display the appropriate link.

Switch would also work:


<<switch $variableX>>
<<case 0>>

<<case 1>>



That'll also be evaluated before the passage is rendered.

I'm fairly sure one could also make it evaluate after the passage has rendered but if you're not making changes to the variable in that passage it's not necessary.

commented by (131k points)

As stated there are many ways to implement this and each method has their own pros and cons.

Another way is to place the code that determines the Target Passage within the link itself. This can be done by combining a <<link>> macro with a <<goto>> macro.

<<set $x to 4>>

<<link "link">>
	<<switch $x>>
		<<case 5>>
			<<goto "passage 1">>
		<<case 4>>
			<<goto "passage 2">>

note: The <<switch>> macro in the above example could be replaced with an <<if>> macro and an <<elseif>>macro.

commented by (640 points)
I'm sure I'm doing it wrong but I can't get it to work using flamekebab's method. Nothing's showing up when I use your code. This is entered in the passage-edit window, am I right?

greyelf's code work's fine. Thanks again!
commented by (1.2k points)
greyelf's code sets the value of the variable on the first line, mine doesn't.

My code won't do anything if $variableX hasn't been set. I figured you already knew how the variable would be set because that's what you're basing passage selection on.
Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.