Kitomas

Convention Practice GDScript

Jan 8th, 2026 (edited)
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Clock.gd
  2. #
  3. # A simple utility that makes use of Godot's Time singleton
  4.  
  5. class_name Clock extends Node
  6.  
  7.  
  8.  
  9. # ---------------------------------- SIGNALS -----------------------------------
  10.  
  11. # (To be honest, I didn't really know what to put here as a signal,
  12. #  but this would probably have some use? ¯\_(ツ)_/¯)
  13. signal minute_changed(new_minute:int)
  14.  
  15.  
  16.  
  17.  
  18.  
  19. # --------------------------------- CONSTANTS ----------------------------------
  20.  
  21. const HOURS_IN_DAY:int      = 24
  22. const MINUTES_IN_HOUR:int   = 60
  23. const SECONDS_IN_MINUTE:int = 60
  24. const MINUTES_IN_DAY:int    = HOURS_IN_DAY * MINUTES_IN_HOUR
  25. const SECONDS_IN_DAY:int    = MINUTES_IN_DAY * SECONDS_IN_MINUTE
  26.  
  27.  
  28.  
  29.  
  30.  
  31. # ----------------------------- CACHED REFERENCES ------------------------------
  32.  
  33. # % 'scene unique' notation is used here, which I learned is new to Godot 4 :D
  34. @onready var _time_label:Label = %ClockTimeLabel
  35.  
  36.  
  37.  
  38.  
  39.  
  40. # ------------------------------ MEMBER VARIABLES ------------------------------
  41.  
  42. # Inits to -1 so that the first call to _update_time()
  43. # always results in a call to _update_label()
  44. var _last_minute:int = -1
  45.  
  46. var current_hour:int   = 0
  47. var current_minute:int = 0
  48. var current_second:int = 0
  49.  
  50.  
  51.  
  52.  
  53.  
  54. # ----------------------------- BUILT-IN OVERRIDES -----------------------------
  55.  
  56. # Called upon being added to the scene graph, after any requisite init stuff
  57. func _ready() -> void:
  58.     _update_time()
  59.     _update_label() # (In the context of this script, this is redundant!)
  60.  
  61.  
  62.  
  63. # Called every frame; delta is prefixed with an underscore to indicate to Godot
  64. # that the value is left unused.
  65. func _process(_delta:float) -> void:
  66.     _update_time() # (Will call _update_label when it's time to do so!)
  67.  
  68.  
  69.  
  70.  
  71.  
  72. # ----------------------------------- PUBLIC -----------------------------------
  73.  
  74. ## Returns a formatted String of the current 24-hour timestamp
  75. func get_24h_timestamp() -> String:
  76.     return "%02d:%02d:%02d" % [current_hour, current_minute, current_second]
  77.  
  78.  
  79.  
  80. ## Returns the number of seconds since midnight, as an integer
  81. func get_secs_since_midnight() -> int:
  82.     # Originally used \ for the line separation, but () is more elegant for this
  83.     # (Also, it might get confused for /)
  84.     return ( current_hour * MINUTES_IN_HOUR * SECONDS_IN_MINUTE
  85.            + current_minute * SECONDS_IN_MINUTE
  86.            + current_second )
  87.  
  88.  
  89.  
  90. ## Returns a Dictionary populated with {hours,minutes,seconds}
  91. ## given a single integer quotient of seconds
  92. func cvt_seconds_to_hms(seconds:int) -> Dictionary:
  93.     seconds %= SECONDS_IN_DAY # Just in case?
  94.  
  95.     # I can't think of a better way to temporarily silence int division warnings
  96.     # here, so have fun with the Godot equivalent of compiler #pragma BS (lol)
  97.     @warning_ignore("integer_division")
  98.     var hours:int = seconds / (MINUTES_IN_HOUR*SECONDS_IN_MINUTE)
  99.     seconds -= hours * MINUTES_IN_HOUR * SECONDS_IN_MINUTE
  100.  
  101.     @warning_ignore("integer_division")
  102.     var minutes:int = seconds / SECONDS_IN_MINUTE
  103.     var secs:int    = seconds % SECONDS_IN_MINUTE # (Seconds *remainder*)
  104.  
  105.     return { "hours":hours, "minutes":minutes, "seconds":secs }
  106.  
  107.  
  108.  
  109.  
  110.  
  111. # ---------------------------------- PRIVATE -----------------------------------
  112.  
  113. func _update_time() -> void:
  114.     var datetime:Dictionary = Time.get_datetime_dict_from_system()
  115.  
  116.     current_hour = datetime.hour
  117.     current_minute = datetime.minute
  118.     current_second = datetime.second
  119.  
  120.     if current_minute != _last_minute:
  121.         _last_minute = current_minute
  122.         emit_signal("minute_changed", current_minute)
  123.         _update_label()
  124.  
  125.  
  126.  
  127. func _update_label() -> void:
  128.     # Should I just replace this with an assert(),
  129.     # given that _time_label is never supposed to be null?
  130.     if _time_label == null: return
  131.     _time_label.text = get_24h_timestamp()
  132.  
Advertisement
Add Comment
Please, Sign In to add comment