Displaying various passage according to current time

Twine Version: 2
Story Format: Harlow3

Hi all,
I would like to display different passage according to the current time of day.
I know how to display (current-time : ) but I don’t know how to use “if” condition to detect if it is the morning, afternoon or evening.
Thank you in advance for your tips
Tharmine


1 Like

Welcome Tharmine,

If you use a variable to track time you can call for a passage in a hook.
For instance, given a variable named $period:

(if:$period is "afternoon")[(display: "afternoon")]

If you track time more precisely, you’d like to use a numeric variable, going from 0 to 23, or even from 0 to 1439 if you happen to like to track to the minute.
For instance, given $hour:

(if:$hour >12 and $hour< 19)[(display: "afternoon")]

Hi, thank you for your help.
I want to use the “real” time of the player (as from his pc).
I can get it by using (current-time) but I don’t know what to do with the result (because it does not return just a number apparently).
It does return something like “1:50 PM”, and I don’t know how to use an IF function with > or < to determine if it is before or after a specific time.
Thank you

I assume you use the (current-time:) macro.

Edit: rewritten because of edit error.

Yes I do

Great. Here’s a tentative code:

{
(set: $time to (current-time:))

(if: (str: $time)'s length < 8)[(set: $time to "0" + it)]
(set: _a to (number:(substring: $time,1,2)))

(if: (substring: $time,7,7) is "P")[(set: _a to it+12)]

(if: _a < 7)[(display: "night")]
(elseif: _a < 13)[(display: "morning")]
(elseif: _a < 18)[(display: "afternoon")]
(elseif: _a < 22)[(display: "evening")]
(else:) [(display: "night")]
}

First, I add a ‘0’ if hours is before 10.
Second I make the two first characters a numeric variable.
Third I add 12 if it’s after noon.
Then it’s a simple (if:) ... (elseif:) ... (else:) statement.

Be aware I’m not sure how hours from midnight to one and from noon to one are displayed by a computer, as I live in a country where we usually write hours under the 00:00 to 23:59 form, so you may have to tweek my code a little because of these hours.

Edit: I probably should have use _time instead of $time. The less story variables, the better.

1 Like

wow, thank you.
We also write time like you, so should not be a problem.
I will test this, thank you very much for your help, reading your script make it more understandable for me.

1 Like

After research I’m convinced there’s no such thing as 0:45 am nor 0:45 pm, so here’s the last tweak:

{
(set: $time to (current-time:))

(if: (str: $time)'s length < 8)[(set: $time to "0" + it)]
(set: _a to (number:(substring: $time,1,2)))

(if: (substring: $time,7,7) is "P")[(set: _a to it+12)]
(if: _a is 12)[(set: _a to 0)]
(if: _a is 24)[(set: _a to 12)]

(if: _a < 7)[(display: "night")]
(elseif: _a < 13)[(display: "morning")]
(elseif: _a < 18)[(display: "afternoon")]
(elseif: _a < 22)[(display: "evening")]
(else:) [(display: "night")]
}

I’ve added two checks to ensure the hours after midnight and noon are considered as 00 and 12 instead of 12 and 24.

1 Like

Thanks, your code yesterday worked perfectly already.
But if this one is better I’ll take it :grinning:
Thank you again for your help

My code of yesterday would not have worked for a player playing between 00:00 and 00:59, nor between 12:00 and 12:59 because the computer doesn’t write time with 00:xx but 12:xx.

You can easily check that by changing your computer’s timezone, something I didn’t think of yesterday.

Thank, I did change my clock but did not test every possibilities. Encore merci !