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

@ -8,13 +8,9 @@ script/source = "extends VBoxContainer
var enabled := false:
get: return enabled
set(value):
if enabled == value:
return
enabled = value
if value == true:
var save_file = SaveFiles.read(SaveFiles.selected_file)
if save_file.has(\"played_for\") and save_file.played_for is float:
seconds_spent_total = save_file.played_for
else:
seconds_spent_total = 0.0
if value == false:
$AnimationPlayer.play(\"scale_linear\")
await get_tree().create_timer($AnimationPlayer.current_animation_length).timeout
@ -31,6 +27,14 @@ var seconds_spent_level_attempt := 0.0:
seconds_spent_level_attempt = value
$Level.text = \"Level: \" + seconds_to_readable(seconds_spent_level_attempt)
func _on_visibility_changed() -> void:
if self.visible and len(SaveFiles.selected_file):
var save_file = SaveFiles.read(SaveFiles.selected_file)
if save_file.has(\"played_for\") and save_file.played_for is float:
seconds_spent_total = save_file.played_for
else:
seconds_spent_total = 0.0
func seconds_to_readable(seconds: float) -> String:
var minutes: int = floor(seconds / 60)
return (\"%0*d\" % [2, minutes]) + \":\" + (\"%0*.3f\" % [6, seconds - (minutes * 60)])
@ -115,27 +119,24 @@ _data = {
[node name="Timer" type="VBoxContainer"]
process_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
offset_right = -992.0
offset_bottom = -563.0
grow_horizontal = 2
grow_vertical = 2
offset_right = 315.0
offset_bottom = 85.0
theme_override_constants/separation = -5
script = SubResource("GDScript_q235s")
[node name="Total" type="Label" parent="."]
layout_mode = 2
text = "Total timer"
text = "Total: 0:00.000"
label_settings = SubResource("LabelSettings_m1tld")
[node name="Level" type="Label" parent="."]
layout_mode = 2
text = "Level timer"
text = "Level: 0:00.000"
label_settings = SubResource("LabelSettings_2a86r")
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
libraries = {
&"": SubResource("AnimationLibrary_jmgpn")
}
[connection signal="visibility_changed" from="." to="." method="_on_visibility_changed"]

View file

@ -1,4 +1,4 @@
[gd_scene load_steps=8 format=3 uid="uid://ccgnnif026wb4"]
[gd_scene load_steps=7 format=3 uid="uid://ccgnnif026wb4"]
[ext_resource type="PackedScene" uid="uid://xd3nsiglcdfc" path="res://gui/timer.tscn" id="1_356j3"]
[ext_resource type="PackedScene" uid="uid://dw0xl8644x166" path="res://gui/gravity.tscn" id="1_ir7so"]
@ -31,8 +31,6 @@ func _ready() -> void:
$DevInfos.text += \"build \" + ProjectSettings.get_setting(\"application/config/version\") + \" (\" + OS.get_name() + \")\"
## Hide UI stuff that shouldn't be visible until later in the game
$VictoryScreen.hide()
$GUI.hide()
pause_menu.hide()
## Connect to necessary signals
@ -42,8 +40,11 @@ func _ready() -> void:
set_main_menu()
func set_main_menu():
func set_main_menu() -> void:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
$GUI.hide()
## If no main menu exists yet, create one
if !is_instance_valid(main_menu):
main_menu = res_main_menu.instantiate()
main_menu.connect(\"request_play_level\", start_level)
@ -59,6 +60,7 @@ func start_level(level: Level, scene: PackedScene) -> void:
current_level_scene = scene
add_child(current_level)
current_level.connect(\"started_playing\", func(): gui_timer.enabled = true)
current_level.connect(\"gravity_change\", gui_gravity.react_to_gravity_change)
current_level.connect(\"completed\", finish_current_level)
current_level.connect(\"ring_collected\", func():
@ -73,7 +75,6 @@ func start_level(level: Level, scene: PackedScene) -> void:
playing = true
changing_level = false
gui_timer.seconds_spent_level_attempt = 0.0
gui_timer.enabled = true
func finish_current_level() -> void:
gui_timer.enabled = false
@ -91,15 +92,10 @@ func finish_current_level() -> void:
await get_tree().create_timer(2).timeout
current_level.queue_free()
win_game()
func win_game() -> void:
$VictoryScreen.show()
await get_tree().create_timer(2).timeout
set_main_menu()
$VictoryScreen.hide()
func restart_level() -> void:
gui_timer.enabled = false
if !current_level or changing_level:
return
if current_level_scene is not PackedScene:
@ -143,11 +139,6 @@ func _on_btn_quit_pressed() -> void:
get_tree().quit()
"
[sub_resource type="LabelSettings" id="LabelSettings_1bs00"]
font_size = 160
outline_size = 20
outline_color = Color(0, 0, 0, 1)
[node name="Game" type="Node"]
script = SubResource("GDScript_8n212")
@ -206,27 +197,6 @@ theme_override_constants/margin_bottom = 15
[node name="Rings" parent="GUI/BottomRight" instance=ExtResource("3_mbj17")]
layout_mode = 2
[node name="VictoryScreen" type="Control" parent="."]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
mouse_filter = 2
[node name="Label" type="Label" parent="VictoryScreen"]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
text = "YOU WON!"
label_settings = SubResource("LabelSettings_1bs00")
horizontal_alignment = 1
vertical_alignment = 1
[node name="DevInfos" type="Label" parent="."]
anchors_preset = 12
anchor_top = 1.0

View file

@ -17,6 +17,7 @@ var rings: Array[Ring] = []
var finished_rings_count: int = 0
var velocity: float = 0.0
var has_started_playing: bool = false
func _init() -> void:
assert(len(id) > 0, self.name + " has no id!")
@ -69,6 +70,8 @@ func _input(_event: InputEvent) -> void:
var down := Input.is_action_just_pressed("gravity_down")
if up or left or right or down:
if !has_started_playing:
has_started_playing = true
started_playing.emit()
PhysicsServer3D.area_set_param(
get_viewport().find_world_3d().space,

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"]

View file

@ -13,7 +13,7 @@ config_version=5
config/name="DreamBall"
config/description="Manipulate the gravity to make a ball go through every ring!
https://kitsunes.dev/Taevas/DreamBall"
config/version="20250423.0"
config/version="20250503.0"
run/main_scene="res://index.tscn"
config/features=PackedStringArray("4.4", "GL Compatibility")
boot_splash/show_image=false