24hr Clock Subroutine

Hi! I’m going to be blatantly ignoring that post I just made and deleted bc I’m an absolute buffoon who realized their mistake literally .2 seconds after posting. Itchy trigger finger, I guess. Also sleep deprivation.

But!! Since I haven’t seen any other clock subroutine (other than that behemoth 650 line code, rip Lordirish) I made my own for keeping track of time when YOU’RE the one adding time. or whatever. If none of this makes sense just let me know and I’ll try again when I’m not tired.

Step 1.

*create hour 00
*create min 00

This code uses military time, since it’ll take a better coder than me to figure out the nasty AM/PM (or maybe me when I havent been up for 28 hours straight coding) So it will display like this: hour:min. Thankfully double zeroes and numbers starting with zero display properly!

Step 2:
Create a scene titled clock, and paste this code (i like to label mine just to be sure or maybe you’re supposed to idk i’ve got tetris syndrome with numbers now, what do i know?)

*label clock
*if min = 60
	*set min 00
	*set hour +1
*if min > 60
	*set min ((min - 60) + 00)
	*set hour + 1
*if hour = 24
	*set hour 00
*return

Then whenever you add (+) to the hour and/or minute value (with a simple *set hour +1 or *set min + 13) and use *gosub_scene clock, it will make sure you’re displaying the right time! :smiley:

I hope somebody finds this useful!

And for those who has seen me flailing and asking question after question and want to know how my game’s going, I got the rough alpha of my first chapter up! Wheeee! https://dashingdon.com/go/5929 (It’s really simple, but historically I suck at coding so I’m p proud of what I have so far)

I would love any feedback or design tips if you’ve gottem!

3 Likes

I saw that! :joy:


The way you create your subroutine means that you need to always call it after changing the time to keep it up-to-date. Wouldn’t it be easier to make so that the routine updates the time, no matter how long it has been since the last routine call?

Also, was this supposed to do something else? Right now it’s adding 00 to the minutes.

*set min ((min - 60) + 00)

I see a flaw. The hour has to land exactly on 24 or it breaks.

Also, why are you checking for the minute to be exactly 60? Wouldn’t it be better just to check if it’s greater than 59?

It needs to handle the 60 case differently than, for example, 65

As a response to everyone

I’m not an expert programmer, so there’s probably flaws, but here’s my reasoning

I was under the impression you needed to have a *gosub_scene everytime you updated something that involves your subroutine to check for flaws ¯_(ツ)_/¯

It can be changed for other people, but this was coded specifically for my game, which moves forward 6 minutes at a time, 30 minutes at most. Should have put a disclaimer this was built specifically for moving minutes at a time. I’m out atm, I’ll write something for moving past 00 hour mark.

I check for 60 specifically at the minute mark because clocks do not show 60, they automatically go from 59 to 00 (of the next hour).

Thanks for the feedback and questions!

1 Like

HUZZAH i added the code

*label clock

*if min = 60
	*set min 00
	*set hour +1
*if min > 60
	*set min ((min - 60) + 00)
	*set hour + 1
*if hour = 24
	*set hour 00
*if hour > 24
	*set hour ((hour - 24) + 00)
*return

Oh missed this! But this is how

*set min ((min - 60) + 00)

works.

So that code responds to if your minutes go over 60, which a clock cannot do.

Lets say, for instance, you’re at 59 minutes, and you add another 13 minutes

thats 72 minutes. Maaan a clock doesnt work like that! So math time! ((72-60)+00) = (12 + 00) = 12. This ensures that when you go above 60 minutes, you go up an hour, and get the correct number of minutes in the minute slot

(I’m so sorry, I could properly explain why everything is the way it is, but I got my BA in theatre and writing, math and explaining math related subjects it not my strong suit and it has been 31 hours since I last slept so I’m gonna pass out, and whatever happens while I’m gone happens)

Haven’t seen the entire thing, but why the 00?
What about the more compact, advanced, humanly and robotically understandable code?

*set min (min - 60)

So, I wrote this routine to update the time. You can pass the minutes as parameter or just use to update the timestamp.

startup.txt
-----------

*create implicit_control_flow false

*create mins 0
*create hours 0
*create days 0
*create timestamp_12 ""
*create timestamp_24 ""

Then, in its own file:

time_utils.txt
--------------

*label clock_update
*set  implicit_control_flow true
*comment UPDATE TIME
*temp add_min 0
*params
*if (param_count = 1)
	*set add_min param[1]
*set mins +add_min
*if mins >= 60
	*set mins -60
	*set hours +1
*if hours >= 24
	*set hours -24
	*set days +1

*comment UPDATE TIMESTAMP
*set timestamp_12 ""
*set timestamp_24 ""
*if (hours < 10)
	*set timestamp_12 "0"&hours
	*set timestamp_24 "0"&hours
*else
	*set timestamp_12 hours
	*set timestamp_24 hours

*if (mins < 10)
	*set timestamp_12 &(":0"&mins)
	*set timestamp_24 &(":0"&mins)
*else
	*set timestamp_12 &(":"&mins)
	*set timestamp_24 &(":"&mins)

*set implicit_control_flow false
*return

You could use the routine to update the time (always in minutes). This would update 1 hour:

*gosub_scene time_utils clock_update 60

Or you could use it only to update the timestamps.

*gosub_scene time_utils clock_update

Then you can use the timestamps variables whenever you need, with an option for 24 hours and another one for 12 hours. So:

${timestamp_12} @{(hours < 12) a.m.|p.m.}
*line_break
${timestamp_24}h

Would print, for example:

01:35 p.m.
13:35h

I’m literally so dumb I hardly understand how that works.

Regardless, this’ll be a good thread for anyone looking for a clock mechanic.

1 Like