Switch statements. can i goto a specific case?

Ill try to explain this as best I can. I am making a game that is based on a good or bad personality. at the beginning I ask a series of what would you do questions. Something like if you saw a wallet on the ground full of money what would you do. A. take the money or B turn it in

I have 3 different passages set up for this. the questions on one bad answers on one and good answers on another all 3 are in switch statements. The problem I realized is that you can only use all good or all bad answers you cannot mix them or it will not align properly with the answers. So I was wondering if there was a way to use goto (passage) and to a specific case like the question was in case 3 the first 2 cases I answered were bad answers so case 1 and 2 on the bad passage would have been hit but nothin on the good so when you hit case 3 question with a good the good passage is still on case 1 so is there a way to force it to case 3?

I hope this made sense the way i described it.

1 Like

About the switch statement, assuming you meant the macro:
<<switch>> is like <<if>> but you only use it for one variable/function, and it should match the is operator.
So:

<<switch $var>>
   <<case [value 1]>>
         what should happen for this case
   <<case [value 2] [value 3]>>
          what should happen for these cases
    <<default>>
          what should happen to all other cases not listed above
<</switch>>
<<case [value 1]>> = <<if $var is [value 1]>>

It would be easier to both see your code, and explain a bit clearer what you’re trying to do with it? (maybe a graph or a list of cases instead of a paragraph?)

OK heres the code This first one is the one for the questions.

<<if ndef $teen>><<set $teen = 1>><</if>><<switch $teen>>
<<case 1>>

You have made it relatively unscathed, you are now starting your teen years. At 13 your route is starting to set. You have a chosen path in mind and are closing in. Now is the time to start molding your mind and deciding what type of person you truly wish to be. The choice is yours. your fate is in your hands.

<center><h1>Day 8</h1></center>

Your first day of Middle school you meet a cute nerdy girl her name is <<textbox "$riley" "Riley">>

<center><<addPic "teen" "cutenerdgirl.jpg">></center>

(note: she will become your best friend)
How do you want your friendship to be?

<center>
<button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;bad&quot;>>')" tabindex="0">Encourage opening up</button>&nbsp; &nbsp;<button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;good&quot;>>')" tabindex="0">Encourage her inner Nerd</button>
</center>


<<case 2>>
You have made it relatively unscathed, you are now starting your teen years. At 13 your route is starting to set. You have a chosen path in mind and are closing in. Now is the time to start molding your mind and deciding what type of person you truly wish to be. The choice is yours. your fate is in your hands.

<center><h1>Day 9</h1></center>
You spot a couple of girls making fun of your friend $riley. What will you do?

<center><<addPic "teen" "teasingfriend.jpg">></center>

<center>
<button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;bad&quot;>>')" tabindex="0">Fight them</button>&nbsp; &nbsp;<button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;good&quot;>>')" tabindex="0">Comfort her</button>
</center>
<<case 3>>
You have made it relatively unscathed, you are now starting your teen years. At 13 your route is starting to set. You have a chosen path in mind and are closing in. Now is the time to start molding your mind and deciding what type of person you truly wish to be. The choice is yours. your fate is in your hands.

<center><h1>Day 10</h1></center>

A Group of girls were hanging out at school and asked you to skip and go to the mall. What do you do?

<center><<addPic "teen" "girltalk.jpg">></center>

<center>
<button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;bad&quot;>>')" tabindex="0">Go to the Mall</button>&nbsp; &nbsp;<button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;good&quot;>>')" tabindex="0">Stay and learn</button></center>
<<case 4>>
You have made it relatively unscathed, you are now starting your teen years. At 13 your route is starting to set. You have a chosen path in mind and are closing in. Now is the time to start molding your mind and deciding what type of person you truly wish to be. The choice is yours. your fate is in your hands.

<center><h1>Day 11</h1></center>

Some girls were spreading rumors around the school. What do you do?

<center><<addPic "teen" "rumors.jpg">></center>

<center>
<button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;bad&quot;>>')" tabindex="0">Spread the rumors</button>&nbsp; &nbsp;<button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;good&quot;>>')" tabindex="0">Tell a teacher</button></center>
<<case 5>>
You have made it relatively unscathed, you are now starting your teen years. At 13 your route is starting to set. You have a chosen path in mind and are closing in. Now is the time to start molding your mind and deciding what type of person you truly wish to be. The choice is yours. your fate is in your hands.

<center><h1>Day 12</h1></center>

You were playing with your $Mrm in the Living room and you broke your $ll's favorite vase. What do you do?

<center><<addPic "teen" "Brokenvase.jpg">></center>

<center>
<button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;bad&quot;>>')" tabindex="0">Blame your brother</button>&nbsp; &nbsp;<button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;good&quot;>>')" tabindex="0">Tell her it was you</button></center>
<<case 6>>
You have made it relatively unscathed, you are now starting your teen years. At 13 your route is starting to set. You have a chosen path in mind and are closing in. Now is the time to start molding your mind and deciding what type of person you truly wish to be. The choice is yours. your fate is in your hands.

<center><h1>Day 13</h1></center>

Your $LL ran over some toys in the driveway and is mad. What do you do?

<center><<addPic "teen" "toy.jpg">></center>

<center><button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;bad&quot;>>')" tabindex="0">Yell at your Brother</button>&nbsp; &nbsp;<button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;good&quot;>>')" tabindex="0">Apoligize to dad</button></center>

<<case 7>>
You have made it relatively unscathed, you are now starting your teen years. At 13 your route is starting to set. You have a chosen path in mind and are closing in. Now is the time to start molding your mind and deciding what type of person you truly wish to be. The choice is yours. your fate is in your hands.

<center><h1>Day 14</h1></center>

Your $LL's autographed Babe Ruth BaseBall has gone missing. What do you do?

<center><<addPic "teen" "baseball.jpg">></center>

<center><button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;bad&quot;>>')" tabindex="0">Blame the dog</button>&nbsp; &nbsp;<button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;good&quot;>>')" tabindex="0">Tell him it was you</button></center>
<<case 8>>



<<unset $teen>>
<</switch>><<if def $teen>><<set $teen += 1>><</if>>

This is the passage for bad answers

<<if ndef $bad>><<set $bad = 1>><</if>><<switch $bad>>
<<case 1>>
You encourage her to open up to new experiences and to let down her guard. 


<center><<addPic "teen" "sluttynerd.jpg">></center>

<center><button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;teen&quot;>>')" tabindex="0">Next</button></center>

<<set $naughty += 1>>

<<case 2>>
You decided to defend your friend and fight the bullys.

<center><<addPic "teen" "girlfight.jpg">></center>

<center><button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;teen&quot;>>')" tabindex="0">Next</button></center>

<<set $naughty += 1>>
<<case 3>>
You decided to skip and go to the mall.

<center><<addPic "teen" "skipschool.jpg">></center>
<<set $naughty += 1>>
<center><button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;teen&quot;>>')" tabindex="0">Next</button></center>
<<case 4>>
You decided to help spread the rumors around the school.

<center><<addPic "teen" "spreadrumors.jpg">></center>
<<set $naughty += 1>>
<center><button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;teen&quot;>>')" tabindex="0">Next</button></center>
<<case 5>>
You decided to lie to your $ll and say your $Mrm was the one that broke it.

<center><<addPic "teen" "vaselie.jpg">></center>
<<set $naughty += 1>>
<center><button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;teen&quot;>>')" tabindex="0">Next</button></center>

<<case 6>>
You decided to lash out and yell at your $Mrm for leaving toys in the driveway.

<center><<addPic "teen" "yellatbro.jpg">></center>
<<set $naughty += 1>>
<center><button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;teen&quot;>>')" tabindex="0">Next</button></center>
<<case 7>>
You decided to blame it on the dog.

<center><<addPic "teen" "baddog.jpg">> </center>
<<set $naughty += 1>>

<center><button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;highschool&quot;>>')" tabindex="0">Next</button></center>
<<case 8>>


<<unset $bad>>
<</switch>><<if def $bad>><<set $bad += 1>><</if>>

and the one for good answers

<<if ndef $good>><<set $good = 1>><</if>><<switch $good>>
<<case 1>>
You encouraged her to embrace her inner nerd. 


<center><<addPic "teen" "lovablenerd.jpg">></center>

<center><button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;teen&quot;>>')" tabindex="0">Next</button></center>

<<set $nice += 1>>


<<case 2>>

You decided to comfort your friend and make her feel better.

<center><<addPic "teen" "comfortfriend.jpg">></center>>

<center><button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;teen&quot;>>')" tabindex="0">Next</button></center>

<<set $nice += 1>>
<<case 3>>
You decided to stay and learn something new.

<center><<addPic  "teen" "learning.jpg">></center>
<<set $nice += 1>>
<center><button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;teen&quot;>>')" tabindex="0">Next</button></center>
<<case 4>>
You decided to tell a teacher about the rumors the kids are spreading.

<center><<addPic "teen" "tellteacher.jpg">></center>
<<set $nice += 1>>
<center><button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;teen&quot;>>')" tabindex="0">Next</button></center>
<<case 5>>
You told your mom the truth and it was all your fault.

<center><<addPic "teen" "vasetruth.jpg">></center>
<<set $nice += 1>>
<center><button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;teen&quot;>>')" tabindex="0">Next</button></center>
<<case 6>>
You apoligized to your $LL for leaving the toys laying around.

<center><<addPic "teen" "sorrydad.jpg">></center>

<<set $nice += 1>>
<center><button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;teen&quot;>>')" tabindex="0">Next</button></center>
<<case 7>>
You told your $LL that it was you. You wanted to play with it and lost it over the fence.

<center><<addPic "teen" "badgirl.jpg">></center>
<<set $nice += 1>>

<center><button style="top: 2%; left: 4%;" onclick="$.wiki('<<goto &quot;highschool&quot;>>')" tabindex="0">Next</button></center>
<<case 8>>

<<unset $good>>
<</switch>><<if def $good>><<set $good += 1>><</if>>

I thoiught at first it worked perfect and didnt realize until later that you couldnt switch up your answers between the two without it messing up and getting the wrong case number

I’m assuming there is another passage where $good and $bad are defined right?

How does that passage looks like?

Also, what’s the need for <<if def $bad>><<set $bad += 1>><</if>> ?

No other passages like that just those 3 passages. as for

<<if def $bad>><<set $bad += 1>><</if>> IDK thats how I learned switch code. To be honest i didnt know if it was a part of it or not. So i just kept using it.

This was the original code I found for Switch. Dont remember where exactly I found it.

<<if ndef $textGroup>><<set $textGroup = 1>><</if>><<switch $textGroup>>
	<<case 1>>Initial story text.
		<<link "Link Text 1" `passage()`>><</link>>
	<<case 2>>Next story text.
		<<link "Link Text 2" `passage()`>><</link>>
	<<default>>Final story text.
		<<unset $textGroup>>[[Next Passage]]
<</switch>><<if def $textGroup>><<set $textGroup += 1>><</if>>

Ok, so that’s the code to essentially “reload” the passage and show new bits of text instead.

But what you’re trying it just show different variation before moving to another passage, correct?
If so:
<<if def $textGroup>><<set $textGroup += 1>><</if>>
is unnecessary. And so would be:

<<if ndef $textGroup>><<set $textGroup = 1>><</if>>

if player must make a choice before.

But still… how is $good, $bad, and $teen set in the first place?

they arent set they are just the names of the passages themselves that are being called to go to. It was probably from my misunderstanding the code and what I needed to do.

I basically want it so that on Day 8 and Day 9 I go to the bad passage. Then Day 10 go to the good passage. The problem is doing that would take me back to day 8 of the good passage so essentially im getting day 8 answers to day 10 question so I need to skip day 8 and day 9 of the good side and go directly to day 10

Assuming that Day 8 is the first case of the $good passage, then the issue would come from the conditional statements (<<if>>) I mentioned in my post above.

You may want to do 1 passage per day before trying to merge it all together. It will make things much easier :wink:

That’s what I was thinking. I didn’t do that at first because I didn’t want to clutter up Twine with more passages than needed. I have 3 sets of these type of questions one growing from toddlers to teen teen to highschooler age and highschooler to adult before it turns into a sandbox style game. So separate passages may be best.

Yeah, I agree with Manon here—I think the underlying issue is that you’re using passages for a purpose that switch trees would be better suited for and vice versa. Rather than having a “good” passage and a “bad” passage with day progression tracked through switch trees, it would be easier to have one passage per day with the outcomes of the player’s choices tracked through switch trees.

In addition, if the $good and $bad variables are meant to be tracking how many good or bad choices the player has made overall (I can’t quite tell if this is what you’re intending to do, but it seems like it?), you shouldn’t be using them to also determine what text the player sees in response to a specific choice, because that will create issues if the player is mixing and matching good/bad choices. You want to have a separate variable that determines the correct switch tree case for the next passage and then to increment the $good or $bad variable either within the link markup or in the correct section of the next passage.

Structurally, that would look something like this:

:: Day 8 Passage

Description of Day 8 scenario.

<<link [[Make good choice|Day 9 Passage]]>><<set $choice = 1>><</link>>
<<link [[Make bad choice|Day 9 Passage]]>><<set $choice = 2>><</link>>

:: Day 9 Passage

<<switch $choice>>
   <<case 1>>
      Outcome of good Day 8 choice. <<set $good += 1>>
   <<case 2>>
      Outcome of bad Day 8 choice. <<set $bad += 1>>
<</switch>>

Description of Day 9 scenario.

<<link [[Make good choice|Day 10 Passage]]>><<set $choice = 1>><</link>>
<<link [[Make bad choice|Day 10 Passage]]>><<set $choice = 2>><</link>>

(Alternately, <<link [[Make good choice|Day 9 Passage]]>><<set $choice = 1>><<set $good += 1>><</link>> and so on, but if you’re just tallying up good/bad points at the end of each life stage this doesn’t really make a difference.)

Does that all make sense?

In general, I don’t think you should worry about “cluttering up” your Twine project with passages. Passages are sort of the basic unit of a Twine game and the workings of Twine as an authoring system are predicated on the idea that you will be moving from one passage to another anytime something significant happens. Trying to do a Twine game in as few passages as possible doesn’t have any particular benefits and you’ll be fighting Twine the whole way.

1 Like

Yeah I see what your getting at with that. And yeah Im using the variables $naughty and $nice to tally the amount of each so at the end whatever has the most will be the players personality for the game either Angel or Devil and will affect the choices they can make through out such as the bedroom they have the job they have how they interact with people and so on.

1 Like