0 votes
by (1.1k points)
I have read all the documentation about the switch, but I am in doubt about all the possible functionality for this. I'm a bit noob in twine yet and I do not know everything about <<switch>>, I usually use this to create random events. But my question is this, what else is possible to do with it?

I've tried using a custom event system such as the events pertaining to a particular character all in one passage and use the switch to hold the event in another passage based on some variables.

Edit: Yes I use sugarcube.

2 Answers

0 votes
by (44.7k points)
selected by
Best answer

Generally it's good for handling cases where you want certain variable values to do very different things.  For example:

<<switch $dayOfWeek>>
<<case "Tuesday">>
	<<set $todaysTasks = ["Go to work", "go to therapist"]>>
<<case "Saturday" "Sunday">>
	<<set $todaysTasks = []>>
	<<set $todaysTasks = ["Go to work"]>>

So that would set the $todaysTasks array to go to work M-F, and also to go to the therapist on Tuesday, and not assign any tasks on the weekend.  You could then use the $todaysTasks variable to determine if certain tasks are available (shown as links to passages) based on the day of the week:

<<if $todaysTasks.includes("Go to work")>>
	[[Go to work|Work]]

That's just one example though.  Another example might be you have a passage that starts out the same way, but then may go three or more separate directions, depending on previous events.  So if you go the the "Junkyard" passage, for example, it might start describing it the same way, but then if you've met the owner and/or his dog before, that could cause the story to go down three or more paths.  You could use the <<switch>> to provide the different descriptions of what happens next based on previous interactions, all within one passage.  That way you don't have to resort to making three different passages that all start the same way.

As idling said, any time you find yourself doing a long series of "<<if>>...<<ifelse>>...<<ifelse>>..." statements, all based around the value of one variable, that's usually a perfect example of when using a <<switch>> would be better.

Hope that helps!  :-)

P.S. When asking questions here in the future, be sure to put your compiler (e.g. "twine2"), story format (e.g. "sugarcube2"), and other relevant topic names (see the "Most popular tags" list to the right) in the tags here.

by (1.1k points)
Thanks for the answers, the sugarcube documentation does not explain much. It is always a little difficult for new people to learn twinescript, especially those who do not know anything about programming.
0 votes
by (23.6k points)
As far as I know the primary use of switch is to replace long if-else chains in certain cases, to render the end result more readable and give you less work typing everything out. That's pretty much it.