hello, i have a question regarding game structure. I have a HUD that i made that holds lives, a timer and score. Right now, the HUD contains code to calculate and show all these values. The code works well but im starting to think that this probably isnt the best way to structure my code. With how im understanding projects, each element should generally only do one thing, right? So the HUD should only display information not calculate it? Would it be better off for me to move all calculations to an autoload that is then accesed by the HUD or am i overthinking this?
#[Conceptual] Is it ok for the HUD to calculate values too?
1 messages · Page 1 of 1 (latest)
this is an example of one of the elements in my HUD
class_name TimerComponent
extends Control
@export
var timer_label: Label
@export
var timer: Timer
func setup_timer(level_time_seconds: int) -> void:
timer.wait_time = level_time_seconds
func start_game() -> void:
timer.start()
func get_time_left() -> Array[int]:
var timer_seconds_left = timer.time_left
if timer_seconds_left == 0:
Hermes.BreakoutGameplay.game_ended.emit()
return [0,0]
var minutes_left: int = timer_seconds_left / 60
var seconds_left: int = int(timer_seconds_left) % 60
var time_left: Array[int] = [minutes_left, seconds_left]
return time_left
func _process(delta: float) -> void:
if Global.GameplayManager.game_started:
update_timer_label()
func update_timer_label() -> void:
timer_label.text = "%02d:%02d" % get_time_left()
(the timer is setup with values that are taken from a level config file)
It depends on the context of your game project. If the game is contained in a single room like a Dodge the Creeps type of game, then I don't think it's necessary to move this logic to an autoload.
If your game only uses one user interface, then it makes sense to load it once and swap out the nodes for the levels and such. I wouldn't think it necessary for it to need an autoload.
If you have multiple user interfaces however and they need to reference the same information, then at that point I'd think an autoload is the best approach, since you want to maintain it on a global level between interfaces.
It's a breakout style game so I'd assume it fits the first descriptions.
Thank you for responding, I'll take this into account with future projects as well!
Sounds like fun! Feel free to tag me (@gdscript) over in #1235192303459635261 when the project is finished. I'd be happy to play it.