Godot Version
4.22
Question
I am in the bug fix phase of my game and the last thing i cant figure out is the save, bc on pc it works fine . The thing is that i dont want a simpel save button i want it to auto save when you go to your open apps on your phone and close it there. Any sugestions ?
here the code i use (it is a Global script )
extends Node
var jump_pad_force = 1700.0
var on_jump_pad: bool = false
var tot = false
var jump_pad_force_2 = 1700.0
var on_jump_pad_2: bool = false
# Called when the node enters the scene tree for the first time.
var score = 50000
var score_run = 0
# Tot menue
var after_run_me = false
# Globale Variablen für den Skin-Shop
var selected_skin: String = "default"
var skin1_owned: bool = false
var skin2_owned: bool = false
var skin3_owned: bool = false
var ignore_collisions = false
#Der aktuell ausgewählte Skin ("default", "skin1", "skin2", etc.)
# Gibt an, ob Skin1 gekauft wurde
# Gibt an, ob Skin2 gekauft wurde
# Gibt an, ob Skin3 gekauft wurde
func _ready():
_load_game()
# Verbinde das Signal für das Pausieren der Anwendung
OS.connect("application_pause", Callable(self, "_on_application_pause"))
func _on_application_pause():
_save_game()
func _exit_tree():
_save_game()
func _save_game():
var save_data = {
"score": Global.score,
"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.score = save_data.get("score", 0)
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.")