Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Beginner Question About Switch Case

Hi Guys,

New to Twine and trying out the different builds. Decided to stick to Sugarcube and try to make it work. I got a little problem making Switch Case work.

So to start, I'm trying to implement a haunted mansion, with sanity being one of the ongoing stat of the reader.

I'm testing the switch functionality by putting it in the second passage of the story. At the start of the passage, sanity will drop. I will then go to a different room (passage) then back to this one.

This is the switch code.

<<switch $sanity>>
<<case lte 80>>
You feel slightly faint. Is it something in the air?
<<case lte 60>>
Your head starts to throb. There is a slight numbing pain in your brain.
<<case lte 40>>
The headache seems to be getting worse. You feel liquid oozing from your right nostril. You dab it with your fingertip. Is that blood?
<</switch>>

First of all, I know the code is wrong. It should go in ascending order. But the weird thing I realised was that each case only triggers one and then never again.

So sanity =
80 - slightly faint
70 - nothing happens
60 - numbing pain
50 - nothing happens
etc.

Is the function supposed to work this way?

Comments

  • edited March 2017
    First. You should always state the story format you're using and its version, because advice will tend to vary based on that information—by version I mean the full version, e.g. SugarCube v2.14.0.


    Have you actually read the <<switch>> documentation? I ask because <<case>> accept lists of values to match against, not conditional expressions. You're attempting to treat them as though they were <<if>>/<<elseif>> and that will not work.

    If you need to test a large range of values at each step, or something else when lends itself to using a relational operator, then you should use <<if>>.

    As far as the ordering, you may do it either way, you simply need to use the correct relational operators. That said, you probably should do it in ascending order, as that will simplify the logic you have to use.

    For example, you probably want something like the following:
    <<if $sanity lte 40>>
    \The headache seems to be getting worse. You feel liquid oozing from your right nostril. You dab it with your fingertip. Is that blood?
    \<<elseif $sanity lte 60>>
    \Your head starts to throb. There is a slight numbing pain in your brain.
    \<<elseif $sanity lte 80>>
    \You feel slightly faint. Is it something in the air?
    \<</if>>
    
Sign In or Register to comment.