[FIXED! Thanks!] Coding a clock that counts up using live:

Twine Version: 2.6.2
Harlowe 3.3.5

Hey! I’m new to these kinds of communities, so if I mess anything up in formatting or using these forums wrong, please feel free to correct me! I’ve looked around other topics and am emulating what I see as best I can :slight_smile:

My problem: I’m trying to code a timer/clock that starts at a specific time when the player opens the passage it’s on, and which continues counting up until the player leaves. (I would like to be able to have whatever it’s on when the player leaves appear in a different passage later on, but that’s much less important tbh). I’ve figured out the live: macro to an extent and have it working somewhat off of that? But I’ve run into a problem where, when the seconds variable reaches 59 and I set it back to zero, I’m having the minutes variable tick up by one, and while it’s doing that, it’s not doing it on zero seconds, like I set it to- it’s doing it on 1 second. What am I doing wrong?

Here’s my code, frankenstein-ed from the Twine For Beginners: Timers and Live Text thing on Wordpress (I would link but it doesn’t want me to? Weird)

{
(set:$hour to 0)
(set:$min to 8)
(set:$sec to 50)
(set:$milli to 0)
$hour : (live:1s)[(if:$sec is 0)[(set:$min to it+1)]$min] : (live:1s)[(set:$sec to it+1)$sec(if:$sec>=59)[(set:$sec to it-60)]] : (live:25)[(set:$milli to it +12)$milli(if:$milli >84)[(set:$milli to 0)]]
}

(note: I have $milli set to just be random numbers instead of anything affecting the rest of the clock, because it flashes by too fast for it to need to make sense, and I was having enough trouble with the sec/min, haha)

It sounds like you’re checking if the minutes should increase before checking if the seconds should start back to 0.

So when $sec is at 59, $min does not increase, then it’s still at 8. Then seconds increase and go back to 0.
The next second $min increases to 9 then $sec to 1.
You probably could work around by modifying $sec before $min.

That makes sense! I see what you’re getting at, only problem now is, I’m trying to figure out how to implement that/change it. If I swap things around to have $sec modify before $min, the way I have it set up will swap their positions in the clock, too? I know this is all noob stuff, haha, but I am very much a coding noob- this is my first major foray into coding anything of substance.

On the other hand, right now it looks easier to modify the value you need $sec to reach to increase $min. Essentially you want $min to increase while $sec is at 59, then both $min then $sec will turn at the same time.

WILD CHEERING!!! IT WORKS!!!

God, that’s been bugging me for literally weeks, thank you so much! Can’t believe it was something as simple as just changing the value it increases at- I literally mentioned that it had a weird lag, I should have been able to connect the dots and just push it back a second so the lag lined up. Shrugs.

Thank you!!

Computer clocks don’t generally track time in course units like hours and minutes, or even second.

They instead track a number that represents the smallest unit of measurement, generally milliseconds, and then calculate the courser units (like hours and minutes) as needed. And any increasing of the represented time is also done using the same smallest unit of measurement.

In the following example time is tracked using seconds as the smallest unit of measurement…

{
<!-- Initialise time to 8 minutes and 50 seconds. -->
(set: $time to (8 * 60) + 50)

(set: _hours to (floor: $time / 3600))
(set: _minutes to (floor: ($time % 3600) / 60))
(set: _seconds to $time % 60)

The current time is: _hours:_minutes:_seconds
}

…and additional time is added like so…

{
<!-- Increase time by 2 hours, 1 minute and 12 seconds. -->
(set: $time to it + (2 * 3600) + (1 * 60) + 12)

(set: _hours to (floor: $time / 3600))
(set: _minutes to (floor: ($time % 3600) / 60))
(set: _seconds to $time % 60)

The new time is: _hours:_minutes:_seconds
}

note: Digital clocks generally display a number that is less than 10 with a leading zero. You can achieve the same result in both of the above examples by adding the following code just before the displaying of the time.

<!-- Convert to Zero padded Strings -->
(set: _minutes to (cond: _minutes < 10, "0", "") + (str: _minutes))
(set: _seconds to (cond: _seconds < 10, "0", "") + (str: _seconds))

The following uses the above information to demonstrates one method for displaying a clock that increments by one second every second.

<!-- Initialise time to 8 minutes and 50 seconds. -->
(set: $time to (8 * 60) + 50)

|clock>[{
(set: _hours to (floor: $time / 3600))
(set: _minutes to (floor: ($time % 3600) / 60))
(set: _seconds to $time % 60)

<!-- Convert to Zero padded Strings -->
(set: _minutes to (cond: _minutes < 10, "0", "") + (str: _minutes))
(set: _seconds to (cond: _seconds < 10, "0", "") + (str: _seconds))

_hours:_minutes:_seconds
}]

(live: 1s)[{
	(set: $time to it + 1)
	(rerun: ?clock)
}]
1 Like