how to make a number reset to 0/making a clock

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Alleywaygentleman

So i want to make a day and night cycle that goes from am to pm, or at the very least 0 to 23, but i’m completely new to godot and when i look online, all i see are real time clocks which are neat but not what i needed, any tips?

:bust_in_silhouette: Reply From: codelyok13

I hope you don’t take it personally but you sound pretty young. I recommend if you are as young as I think you are and don’t understand my link that you ask your math teach about remainder/modulo as that is the answer to your problem.

You need to use modulus operator.

So if you want a 24 hour day.

0 % 24 = 0
1 % 24 = 1
2 % 24 = 2
.
.
.
.
23$24 = 23
24 % 24 = 0
25 % 24 = 1
26 % 24 = 2

func update_time():
      time = (time + 1)  % 24

What the above code will do is that anytime you reach 24, time will loop back to 0 without needing any logic.


if you don’t what to use modulo, use an if statement

func update_time():
      if time == 24:
             time = 0
      else:
            time = time + 1

Thanks! and it’s fine, didn’t realized i sounded young, never heard of remainder/modulo, school never taught that. didn’t think math would come in when it came to making a clock XD

Alleywaygentleman | 2022-02-24 13:12