Fix memory leak and save file deletion + other stuff

might also be the commit where timer starts only when you move
This commit is contained in:
Taevas 2025-05-03 14:33:49 +02:00
parent 709ce8eea3
commit edf99640cf
Signed by: Taevas
SSH key fingerprint: SHA256:Y5Hv18xwPvUKSlgkx1sPnRO3L2mc03ehC7BzrnZVEyY
6 changed files with 45 additions and 57 deletions

View file

@ -62,6 +62,16 @@ func _on_play_button_pressed() -> void:
func seconds_to_readable(seconds: float) -> String:
var minutes: int = floor(seconds / 60)
return (\"%0*d\" % [2, minutes]) + \":\" + (\"%0*.3f\" % [6, seconds - (minutes * 60)])
## This prevents a memory leak that is possible when this menu is destroyed
## and the loaded_level becomes unavailable to the rest of the game
## For example in the main menu, go to this menu, select a level,
## press \"return\" and go to this menu again, boom, memory leak prevented by this
func _notification(what: int) -> void:
match what:
NOTIFICATION_PREDELETE:
if is_instance_valid(loaded_level) && !loaded_level.is_inside_tree():
loaded_level.queue_free()
"
[node name="LevelSelectionMenu" type="Control"]

View file

@ -6,7 +6,9 @@ script/source = "extends Control
signal request_start
@onready var label_name = $VBoxContainer/MarginContainer/VBoxContainer/Description/Name
@onready var label_time = $VBoxContainer/MarginContainer/VBoxContainer/Description/Time
func _ready() -> void:
$VBoxContainer/MarginContainer.hide()
## Get the newest \"played_for\" when the main menu appears again
func _enter_tree() -> void:
@ -26,6 +28,8 @@ func _on_save_3_pressed() -> void:
display_file_data(SaveFiles.names[2])
func display_file_data(file_name: String) -> void:
$VBoxContainer/MarginContainer.show()
var label_time := $VBoxContainer/MarginContainer/VBoxContainer/Description/Time
var data = SaveFiles.read(file_name)
$VBoxContainer/MarginContainer.show()
label_time.text = \" | \"
@ -34,14 +38,14 @@ func display_file_data(file_name: String) -> void:
var minutes: int = floor(seconds / 60)
label_time.text += (\"%0*d\" % [2, minutes]) + \":\" + (\"%0*.3f\" % [6, seconds - (minutes * 60)])
else:
label_time.text += \"00:00\"
label_time.text += \"00:00.000\"
func _on_start_pressed() -> void:
request_start.emit()
func _on_delete_pressed() -> void:
SaveFiles.empty(SaveFiles.selected_file)
display_file_data(SaveFiles.read(SaveFiles.selected_file))
display_file_data(SaveFiles.selected_file)
"
[node name="SaveFileManager" type="Control"]