Godot Version
4.22
Question
I am at the end of my game an endlesrunner and did the saving func but it does not work
func _exit_tree():
_save_game()
func _save_game():
var save_data = {
"selected_skin": Global.selected_skin,
"skin1_owned": Global.skin1_owned,
"skin2_owned": Global.skin2_owned,
"skin3_owned": Global.skin3_owned,
}
var save_file = FileAccess.open("user://save_game.save", FileAccess.WRITE)
if save_file:
save_file.store_var(save_data)
save_file.close()
print("Game saved successfully.")
else:
print("Failed to open file for writing.")
func _load_game():
if FileAccess.file_exists("user://save_game.save"):
var save_file = FileAccess.open("user://save_game.save", FileAccess.READ)
if save_file:
var save_data = save_file.get_var()
save_file.close()
Global.selected_skin = save_data.get("selected_skin", "default")
Global.skin1_owned = save_data.get("skin1_owned", false)
Global.skin2_owned = save_data.get("skin2_owned", false)
Global.skin3_owned = save_data.get("skin3_owned", false)
print("Game loaded successfully.")
else:
print("Failed to open file for reading.")
else:
print("No save game found.")
and im tryin to save the var’s of the autoload Global
var selected_skin: String = "default"
var skin1_owned: bool = false
var skin2_owned: bool = false
var skin3_owned: bool = false
it does not show any erors so i dont realy know what i am doing wrong here
try to print out the save_data and see whats inside:
var save_data = save_file.get_var()
print(save_data)
HI! is this the complete script? if it is, you don’t have a proper save_file variable. You are using temporary variables to store data. I will share you how mine is coded:
*extends Node
const PATH = “user://savedata.save”
signal load_finished
var save_data: Dictionary = {
“current_scene” : “res://game_elements/levels/Level_01/level_01_pre_level_story/level_01_pre_level_story.tscn”,
“gravity_strenght” : 0.0,
“jump_strenght” : 0.0,
“health” : 0.0,
“pulse” : false,
“gun” : false,
“jump” : false,
“dash” : false,
“range” : false,
}
func _ready():
load_save_data()
func load_complete():
load_finished.emit()
func load_save_data():
if !FileAccess.file_exists(PATH):
print(“Save file does not exist”)
return
var file = FileAccess.open(PATH,FileAccess.READ)
save_data = file.get_var()
file.close()
func save():
var file = FileAccess.open(PATH,FileAccess.WRITE)
file.store_var(save_data)
file.close()
*
so i have checked it prints all the data but not the Updated things . But i dont get it when i close the game the func _exit_tree(): should start _save_game(): or am i wrong here ?
thanks for sharing your code but i need it to save auto when i exit bc it is a mobile game
1 Like
no _exit_tree doesnt get called when you close the game. You would have to remove the scene from the scene_tree for that to happen
oh then what does ? which func does that ?
Well you can call queue_free(). That deletes a scene.
But maybe you want to use notifications:
Inherited By: AudioServer, CameraServer, ClassDB, DisplayServer, EditorFileSystemDirectory, EditorInterface, EditorPaths, EditorSelection, EditorUndoRedoManager, EditorVCSInterface, Engine, EngineD...
Heres the source:
Attention
Topic was automatically imported from the old Question2Answer platform.
Asked By
XavierH622
how do i execute code/a script/ a function when the user presses the ‘X’ button in the corner of the window so that i can save the game/close any open files/close any open network connections/etc?