# 24hr Clock Subroutine

**URL:** <https://forum.choiceofgames.com/t/24hr-clock-subroutine/80220>\
**Category:** Game Development\
**Tags:** tips, coder\
**Created:** [May 30, 2020, 8:51pm UTC](https://forum.choiceofgames.com/t/24hr-clock-subroutine/80220 "2020-05-30T20:51:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![proteus](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/proteus/32/118202_2.png) [@proteus](https://forum.choiceofgames.com/u/proteus)\
**Post date:** [May 30, 2020, 8:51pm UTC](https://forum.choiceofgames.com/t/24hr-clock-subroutine/80220/1 "2020-05-30T20:51:03Z")

</div>

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! 😃

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](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!

---

<div class="post-metadata">

**Author:** ![cup\_half\_empty](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/cup_half_empty/32/273983_2.png) [@cup\_half\_empty](https://forum.choiceofgames.com/u/cup_half_empty)\
**Post date:** [May 30, 2020, 10:44pm UTC](https://forum.choiceofgames.com/t/24hr-clock-subroutine/80220/2 "2020-05-30T22:44:22Z")

</div>

I saw that! 😂

* * *

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)

```

---

<div class="post-metadata">

**Author:** ![no1uno](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/no1uno/32/229374_2.png) [@no1uno](https://forum.choiceofgames.com/u/no1uno)\
**Post date:** [May 30, 2020, 10:48pm UTC](https://forum.choiceofgames.com/t/24hr-clock-subroutine/80220/3 "2020-05-30T22:48:27Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![SpokesWriter](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/spokeswriter/32/115681_2.png) [@SpokesWriter](https://forum.choiceofgames.com/u/SpokesWriter)\
**Post date:** [May 30, 2020, 11:06pm UTC](https://forum.choiceofgames.com/t/24hr-clock-subroutine/80220/4 "2020-05-30T23:06:13Z")

</div>

> [@no1uno](#):
>
> 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

---

<div class="post-metadata">

**Author:** ![proteus](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/proteus/32/118202_2.png) [@proteus](https://forum.choiceofgames.com/u/proteus)\
**Post date:** [May 30, 2020, 11:18pm UTC](https://forum.choiceofgames.com/t/24hr-clock-subroutine/80220/5 "2020-05-30T23:18:09Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![proteus](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/proteus/32/118202_2.png) [@proteus](https://forum.choiceofgames.com/u/proteus)\
**Post date:** [May 30, 2020, 11:36pm UTC](https://forum.choiceofgames.com/t/24hr-clock-subroutine/80220/6 "2020-05-30T23:36:34Z")

</div>

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

```

 ![image](https://global.discourse-cdn.com/choiceofgames/original/3X/f/f/ff9cb2a998d1811755bf2ff5c46dd63d835305eb.jpeg)

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)

---

<div class="post-metadata">

**Author:** ![Szaal](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/szaal/32/112202_2.png) [@Szaal](https://forum.choiceofgames.com/u/Szaal)\
**Post date:** [May 31, 2020, 12:49am UTC](https://forum.choiceofgames.com/t/24hr-clock-subroutine/80220/7 "2020-05-31T00:49:54Z")

</div>

> [@proteus](#):
>
> thats 72 minutes.

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

```choicescript
*set min (min - 60)

```

---

<div class="post-metadata">

**Author:** ![cup\_half\_empty](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/cup_half_empty/32/273983_2.png) [@cup\_half\_empty](https://forum.choiceofgames.com/u/cup_half_empty)\
**Post date:** [May 31, 2020, 1:33am UTC](https://forum.choiceofgames.com/t/24hr-clock-subroutine/80220/8 "2020-05-31T01:33:00Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![proteus](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/proteus/32/118202_2.png) [@proteus](https://forum.choiceofgames.com/u/proteus)\
**Post date:** [May 31, 2020, 11:53pm UTC](https://forum.choiceofgames.com/t/24hr-clock-subroutine/80220/9 "2020-05-31T23:53:53Z")

</div>

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.
