What's wrong with this logic? I'm using sugarcube with twine 2

/* Day night cycle */
<<if $time >= 7 and $time <= 20>><<set $dayNightCycle to “day”>><>
<<if $time >= 21.00 and $time <= 6.99>><<set $dayNightCycle to “night”>><>
It is currently $dayNightCycle.

Hi there!

Instead of using 2 if conditions it will work more reliably if you use if/else like this:

/* Day night cycle */
<<if $time >= 7 and $time <= 20>><<set $dayNightCycle to "day">>
<<else>>
<<set $dayNightCycle to "night">>
<</if>>
It is currently $dayNightCycle.

Hope that helps!

2 Likes

Hacd is correct. Note that with your system, if the $time was 20.5, then $dayNightCycle wouldn’t get set, but with his version of the code, that variable will always get set to either "day" or "night", because the <<else>> covers all other cases.

P.S. @Pepsidog: In this forum you should put any code within “preformatted text” blocks (using the </> button) so that code, like your <</if>> parts, don’t get turned into just <>. For multiple lines of code, just select two blank lines in your post (using SHIFT+DOWNARROW when the cursor is on the first blank line) and then click the </> button, and it will create a “codeblock” for you to enter your code into. You can see how that looks in Hacd’s post.

@Hacd’s approach is definitely better! Else statements are great.

If you do want to know what was wrong with your original code though, it probably has to do with the second conditional:
<<if $time >= 21.00 and $time <= 6.99>>

Because of the AND there in between, BOTH conditions (greater than/equal to 21 and less than or equal to 6.99) have to hold for it to become night. But there’s no number that meets that, right? $time can’t be both >= 21 and <= 6.99 at the same time. You’d want to replace the AND there with an OR, so the night conditions are satisfied if $time is either >= 21 OR <= 6.99.

1 Like

That explains everything.