Auto saving on adroid

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.")

Hello, @liebezeitruud! From official docs:

On mobile devices

There is no direct equivalent to NOTIFICATION_WM_CLOSE_REQUEST on mobile platforms. Due to the nature of mobile operating systems, the only place that you can run code prior to quitting is when the app is being suspended to the background. On both Android and iOS, the app can be killed while suspended at any time by either the user or the OS. A way to plan ahead for this possibility is to utilize NOTIFICATION_APPLICATION_PAUSED in order to perform any needed actions as the app is being suspended.

On Android, pressing the Back button will exit the application if Application > Config > Quit On Go Back is checked in the Project Settings (which is the default). This will fire NOTIFICATION_WM_GO_BACK_REQUEST.

So, you can try this code:

func _notification(what):
	if what == NOTIFICATION_APPLICATION_PAUSED 
    or what == NOTIFICATION_WM_GO_BACK_REQUEST:
		_save_game()

I’m not sure about this code, because I don’t know if the _notification(what) function handles NOTIFICATION_APPLICATION_PAUSED notification.

1 Like

You where right it works. I did not think that someone would react to this Specific question so thanks :slight_smile: . I just made it save after each major action hapening in the game like Dying , buying or equiping a skin but this is way more recource efficient so im gone chage it thanks a lot :slight_smile:

1 Like