How do I use this widget in a conditional expression?

Twine Version: 2.3
Story Format: SugarCube 2.0

How do I use this widget in a conditional expression? For example, I want something to happen only on the weekend. Actually, it might not be a widget, but I don’t know what else to call it. Here’s what’s in my StoryInit passage:

<<set setup.DAYS to ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]>>

<<set $day to 1>>

I have a widget that’s able to print the day of the week (and days played) using this. It looks like this:

<<widget "now">>
	<<print setup.DAYS[$day % 7]>>, Day $day.
<</widget>>

I’m not really familiar with what the “setup” thing is or what the “%” does. Yeah, I’m pretty clueless as to how all of this works. I tried looking up the answer but it’s difficult to formulate a google query that actually finds a solution. Hopefully someone can help me out here!

note: The title of your thread and the question contained within it seem to be about different topics, so I will answer the question contained within.

The indexes of a SugarCube (and JavaScritpt) Array is offset or zero based, which means that the first element in the Array has an index of 0 (zero) not 1 (one).

So if you want to output the value “Tuesday” from your setup.DAYS Array you would use an index of 2 not 3.

<<print setup.DAYS[2]>>

So if you want your $day variable to be a number between 1 and infinity then you need to do a little more maths in your widget to get the outcome you want…

<<widget "now">>
	<<print setup.DAYS[($day - 1) % 7]>>, Day $day.
<</widget>>

Thanks for your response, and sorry about the title. I used an old draft and changed it and I guess I forgot to change the title. I think you may have misunderstood my question. I’m happy wit what’s being printed. What I want is to create an “if” statement. For example, let’s say I want something to appear in my passage on every Saturday.

<<if $day is 6>>
<>

Obviously, this only works on the first Saturday. I want to make something that’s going to work for every week. I realize that the widget may not actually be involved in this, so I can see how the question may have been misleading.

That’s what the modulo % allows.
It’s not this easy (at least for me) to describe what a modulo is, so I will tell with an example.

Take 7 and 3.
7/3 (a division) is 2.3333.
The integer part of 7/3 is 2.
The modulo 7%3 is equal to 7 minus 2 (the integer part)*3, that is 7-6 = 1.

So what you want is, assuming you start on sunday:

<<if $day % 7 == 6>>

As souppiloulioums explained the % symbol stands for modulus or Modular Arithmetic.

You have already been shown the division related explanation on how modulus works, but there is another way to look at what is happening using reparative subtraction.

eg, Lets take the same 7 % 3 example given by souppiloulioums.but use reparative subtraction instead.

keep subtracting 3 until the remainder is either
less than the subtractor (3) or equal to zero.

7 - 3 => 4

Is 4 less than 3? No.

4 - 3 => 1

Is 1 less than 3? Yes, so 7 % 3 => 1
1 Like

As a minor point of pedantry, in JavaScript the % operator is the remainder operator rather than a true modulo operator.

From remainder operator (%) @MDN:

The remainder operator (%) returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend.

Note that while in most languages, ‘%’ is a remainder operator, in some (e.g. Python, Perl) it is a modulo operator. For two values of the same sign, the two are equivalent, but when the dividend and divisor are of different signs, they give different results. To obtain a modulo in JavaScript, in place of a % n, use ((a % n) + n) % n.

2 Likes