[Twine2][Sugarcube2] Checking Complex Variables

closed

So, the variable itself isn't overly complex, but I need the <<if>> check to be fairly complex.

``````<<set \$gameDay = 5>>

<<if \$gameDay == 1 or 2 or 6 or 7>>
<<set \$workAlert = true>>
<<else>>
<<set \$workAlert = false>>
<</if>>``````

In the above instance, I need the \$workAlert boolean to trip an alert only if \$gameDay == 1, 2, 6, or 7. Currently, \$gameDay is 5, so it shouldn't flip the switch.. But it is. Unhelpfully, it is.

I've tried using ==, I've tried ===, no luck. It auto-flips \$workAlert to true no matter what. Which is super NOT helpful.

Any ideas why Sugarcube 2.14.0 would be doing this?

closed with the note: Answer Found!

by (159k points)
selected

I suggest not putting the version of Twine and the story format within the title of your question as it just makes it longer without any real benefit, just using the question tags to supply that information is more beneficial.

You need to fully state each individual expression of a compound expression.

``````<<if \$gameDay == 1 or \$gameDay == 2 or \$gameDay == 6 or \$gameDay == 7>>
<<set \$workAlert = true>>
<<else>>
<<set \$workAlert = false>>
<</if>>``````

... you could also use a Array literal and the <Array>.includes() function to achieve the same result.

``````<<if [1,2,6,7].includes(\$gameDay)>>
<<set \$workAlert = true>>
<<else>>
<<set \$workAlert = false>>
<</if>>``````

You may want to use the keyword operators ("to", "eq", "is") instead of the mathematical operators ("=", "==", "===") as it is very easy to use the wrong number of equals signs.

by (260 points)
Thanks! I've been using mathematical operators thus far as I enjoy the cleanliness of it, but I've been considering going back through and switching as I have to remember that I need 1 when setting, and 2 when checking. Also, looking up when to use != instead of "neq" was a chore..

Thanks for your help, once again!
by (68.6k points)

Your problem is that each sub-condition needs to be a complete expression.  For example:

``````<<if \$gameDay is 1 or \$gameDay is 2 or \$gameDay is 6 or \$gameDay is 7>>
<<set \$workAlert to true>>
<<else>>
<<set \$workAlert to false>>
<</if>>``````

Alternatively.  Unless you're doing other things not shown in your example, there's no need for the <<if>> here at all.  You may simply set \$workAlert to the result of the aggregate conditional expression.  For example:

``<<set \$workAlert to \$gameDay is 1 or \$gameDay is 2 or \$gameDay is 6 or \$gameDay is 7>>``

by (260 points)
That's.. An interesting way of doing it. I hadn't ever seen an expression structured that way.

I have a separate passage counting the passage of time that's referenced during my "sleep" passage using <<display>> macros, so it'll add and change the day number and day name for it. The big thing is that I have jobs that fire the \$workAlert boolean on different days. If you have X job, you work on X days, if you have Y job, you work on Y days. I wanted to make an easily referenced passage so I could just fill in the days work is required, and have the sensor NOT trip on days not worked. That way it's all automated if the player switches jobs down the line, I don't have to write whole new bits of script for those changes.

I'm learning a lot from creating a "Life Sim" in Twine, over the RAGS engine. Pushing Twine to do something it really wasn't designed for has been interesting.