File reading/writing isn't working on android

Godot 4.2.1

File reading/writing isn’t working on Android app. Works fine on Windows build.
Using default configuration for building apk with enabled permissions for read and write external storage.

Code

extends Node

const SAVE_PATH = "user://save_game.dat"

func saveGame():
	var file = FileAccess.open(SAVE_PATH, FileAccess.WRITE)
	var data = {
		"Gold" : Game.Gold,
	}
	var jstr = JSON.stringify(data)
	file.store_line(jstr)
	
func loadgame():
	var file = FileAccess.open(SAVE_PATH, FileAccess.READ)
	if FileAccess.file_exists(SAVE_PATH):
		if not file.eof_reached():
			var current_line = JSON.parse_string(file.get_line())
			if current_line:
				Game.Gold = current_line["Gold"]
	elif FileAccess.file_exists(SAVE_PATH) == false:
		saveGame()
		loadgame()


func _on_ready():
	#saveGame()
	loadgame()


i used GitHub - AdamKormos/SaveMadeEasy: An easy to use, versatile Save/Load System for Godot 4 inspired by the simplicity of Unity's PlayerPrefs. Supports nested variables, Resources and encryption. plugin, tested on 3 different android devices, got no issues, might want to take a look

Thanks for sharing, I’ve rewritten the code for this plugin, however it still has the same problem. On my windows it works fine, however on my android device it is the same problem, not working

not working as in, not saving and loading as expected?

When reading the file it returns NULL, even if I manually create and add a file. When writing to file, it doesn’t write and does nothing.

i see, do you put SaveSystem.save() after setting a variable’s value?

yes, stil isn’t working.

it looks like specific android issues, did you try it on other android device?

I tried on an emulator but the app stops when launching, so can’t really test it that way. Haven’t tried it on another physical device, will try to do it as soon as I can.

what’s your rendering mode before exporting as android build?

sorry, I’m not quite sure what you are referring to as the rendering mode.
Rendering method is Mobile and the Rendering device is Vulkan, if you are referring to any of them

Does adb show any errors?

Thank you, I managed to fix it.
The problem was that there were problems with script attaching. Detaching and attaching a new script seems to have fixed the issue. The script wasn’t loading properly on android for some reason.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.