using Godot; using Mr.BrickAdventures.Autoloads; namespace Mr.BrickAdventures.scripts.UI; [GlobalClass] public partial class SpeedRunHud : Control { [Export] private Label _timerLabel; public override void _Ready() { if (SpeedRunManager.Instance != null) SpeedRunManager.Instance.TimeUpdated += OnTimerUpdated; } /// /// Detaches from the autoload's signal. Without this, every level load left another handler /// attached to a freed HUD: SpeedRunManager outlives the scene, so it kept calling them and each /// one touched a disposed Label. It stayed hidden only because the timer was never started on /// Continue, so the signal never fired outside a fresh run. /// public override void _ExitTree() { if (SpeedRunManager.Instance != null) SpeedRunManager.Instance.TimeUpdated -= OnTimerUpdated; } private void OnTimerUpdated(double totalTime, double levelTime) { if (_timerLabel == null || !IsInstanceValid(_timerLabel)) return; _timerLabel.Text = SpeedRunManager.FormatTime(totalTime); } }