How To Save To Android

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By CreekWorks

Hello!
I am working on a port of my game to Android, I was wondering if anyone knew of a way to save to Android as I could not find a way to find a directory. My problem is that for example on Windows you can call for a file with file_exists(“user://savegame.save”) If you know of a directory like this for Android please tell me. Thank you!

Here is my code

    func save():
	var save_dict = {
		"highscore":beststreak
	}
	return save_dict

func save_game():
	if PlayerVars.mobile == false:
		var save_game = File.new()
		save_game.open("user://savegame.save", File.WRITE)
		save_game.store_line(to_json(save()))
		save_game.close()

func load_game():
	if PlayerVars.mobile == false:
		var save_game = File.new()
		if not save_game.file_exists("user://savegame.save"):
			print("Error! We Don't Have A Save File To Load")
			return
		save_game.open("user://savegame.save", File.READ)
		
		var current_line = parse_json(save_game.get_line())
		
		beststreak = current_line["highscore"]
		save_game.close()
:bust_in_silhouette: Reply From: jgodfrey

Have you tried what you have on Android? That exact same file name should work fine on Android.

I have tried it, I was looking the place where to save it rather than a file name. Or am I misunderstanding you?

CreekWorks | 2020-12-21 00:37

The user:// path resolves to an appropriate location for each OS. So, I’m just saying that it’s perfectly fine (and expected) that you would use user:// for both Windows and Android (for example): So, your above constructed file path should be perfectly fine for both.

jgodfrey | 2020-12-21 00:46

There’s some general info here:

File paths in Godot projects — Godot Engine (stable) documentation in English

The above page mentions:

On some devices (for example, mobile and consoles), this path is unique to the project.

jgodfrey | 2020-12-21 00:54

Also, I only have one persistence class and I use it for both Android and Windows. It doesn’t contain any OS-specific checks - the exact same code is used for both. My save file name is defined as follows:

var persistFile = "user://savedata.txt"

jgodfrey | 2020-12-21 00:55

Thank you for helping!
I am still having problems with it but I can’t get data on what is happening so until I can get more information I simply have to work on the problem. Again Thank you.

CreekWorks | 2020-12-21 02:01

as it turns out it a single wav file I had added early this morning was the problem… not the saving script -_-

Thank you for helping me with my perceived issue

CreekWorks | 2020-12-21 02:24

Yeah, sorry I don’t have much more to offer. Your code looks reasonable and the file reference you’re using should be fine.

jgodfrey | 2020-12-21 02:29

1 Like