Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Clock.gd
- #
- # A simple utility that makes use of Godot's Time singleton
- class_name Clock extends Node
- # ---------------------------------- SIGNALS -----------------------------------
- # (To be honest, I didn't really know what to put here as a signal,
- # but this would probably have some use? ¯\_(ツ)_/¯)
- signal minute_changed(new_minute:int)
- # --------------------------------- CONSTANTS ----------------------------------
- const HOURS_IN_DAY:int = 24
- const MINUTES_IN_HOUR:int = 60
- const SECONDS_IN_MINUTE:int = 60
- const MINUTES_IN_DAY:int = HOURS_IN_DAY * MINUTES_IN_HOUR
- const SECONDS_IN_DAY:int = MINUTES_IN_DAY * SECONDS_IN_MINUTE
- # ----------------------------- CACHED REFERENCES ------------------------------
- # % 'scene unique' notation is used here, which I learned is new to Godot 4 :D
- @onready var _time_label:Label = %ClockTimeLabel
- # ------------------------------ MEMBER VARIABLES ------------------------------
- # Inits to -1 so that the first call to _update_time()
- # always results in a call to _update_label()
- var _last_minute:int = -1
- var current_hour:int = 0
- var current_minute:int = 0
- var current_second:int = 0
- # ----------------------------- BUILT-IN OVERRIDES -----------------------------
- # Called upon being added to the scene graph, after any requisite init stuff
- func _ready() -> void:
- _update_time()
- _update_label() # (In the context of this script, this is redundant!)
- # Called every frame; delta is prefixed with an underscore to indicate to Godot
- # that the value is left unused.
- func _process(_delta:float) -> void:
- _update_time() # (Will call _update_label when it's time to do so!)
- # ----------------------------------- PUBLIC -----------------------------------
- ## Returns a formatted String of the current 24-hour timestamp
- func get_24h_timestamp() -> String:
- return "%02d:%02d:%02d" % [current_hour, current_minute, current_second]
- ## Returns the number of seconds since midnight, as an integer
- func get_secs_since_midnight() -> int:
- # Originally used \ for the line separation, but () is more elegant for this
- # (Also, it might get confused for /)
- return ( current_hour * MINUTES_IN_HOUR * SECONDS_IN_MINUTE
- + current_minute * SECONDS_IN_MINUTE
- + current_second )
- ## Returns a Dictionary populated with {hours,minutes,seconds}
- ## given a single integer quotient of seconds
- func cvt_seconds_to_hms(seconds:int) -> Dictionary:
- seconds %= SECONDS_IN_DAY # Just in case?
- # I can't think of a better way to temporarily silence int division warnings
- # here, so have fun with the Godot equivalent of compiler #pragma BS (lol)
- @warning_ignore("integer_division")
- var hours:int = seconds / (MINUTES_IN_HOUR*SECONDS_IN_MINUTE)
- seconds -= hours * MINUTES_IN_HOUR * SECONDS_IN_MINUTE
- @warning_ignore("integer_division")
- var minutes:int = seconds / SECONDS_IN_MINUTE
- var secs:int = seconds % SECONDS_IN_MINUTE # (Seconds *remainder*)
- return { "hours":hours, "minutes":minutes, "seconds":secs }
- # ---------------------------------- PRIVATE -----------------------------------
- func _update_time() -> void:
- var datetime:Dictionary = Time.get_datetime_dict_from_system()
- current_hour = datetime.hour
- current_minute = datetime.minute
- current_second = datetime.second
- if current_minute != _last_minute:
- _last_minute = current_minute
- emit_signal("minute_changed", current_minute)
- _update_label()
- func _update_label() -> void:
- # Should I just replace this with an assert(),
- # given that _time_label is never supposed to be null?
- if _time_label == null: return
- _time_label.text = get_24h_timestamp()
Advertisement
Add Comment
Please, Sign In to add comment