json save at android

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

I learned how to create a save file using json

It worked well when I ran it on my pc.

But it does not work on Android

If godot does not have a json file, it will generate a json file the first time it runs

But if you change the contents and keep the json file name, it does not seem to be updated

So I thought that every run would not detect, update, or generate changes in the json file

I think we only create a new json file when it does not exist.

But on Android, no save is applied

I’m wondering if the json file is being created, but I do not know where the folder of the app I installed is

Hello sir, I am experiencing the same problem with saved file on android cannot be found. Did you perhaps already have a solution for this?

richvolter15 | 2020-02-29 05:56

:bust_in_silhouette: Reply From: Juxxec

Can you post the code that creates the JSON file?

var save_dictionary = {
"easy_high_score" : 0,
"nomal_high_score" : 0,
"music" : true,
"sfx" : true
}

const save_path = "res://save/save.json"

func load_data():
var save_file = File.new()
if save_file.file_exists(save_path) == true:
	save_file.open(save_path, File.READ)
	var json_string = save_file.get_line()
	save_file.close()
	save_dictionary = parse_json(json_string)
else:
	print("no save")

func save_data():
var json_string = to_json(global.save_dictionary)
var save_file = File.new()
save_file.open(global.save_path, File.WRITE)
save_file.store_line(json_string)
save_file.close()

In this state, the load_data function is executed in the title scene
If the json file does not exist, it is created

MungMoong | 2019-04-03 03:32

res:// is no valid save path IMHO. This directory is read only on some platforms including android. (comparable to “Program Files” in Windows).

Save and load your data to/from user:// instead.

wombatstampede | 2019-04-03 06:46

Oh I see

I tested it on my pc before exporting it to Android, but I do not know where it is stored on my pc

I can not see the folder or file related to save when I go to ‘Drive C - User’

MungMoong | 2019-04-03 09:45

I found out where it was stored.

But I put the folder name in front of the file name, but I did not create the folder

So I deleted the folder name part and the file was created

But I do not know where Android is generated.

Even if you search the internet, all the answers have only links to the wrong posts

MungMoong | 2019-04-03 09:56

Android allows only file writes for apps to user://. And, if you enable the access right for it, they can also read/write in public “SD-Card” space. (Unless you use a rooted device.)

Android expects apps to write their “private” data into user://. This data can not be read or written by other apps.

You can create folders in user://, no problem. The operating system decides where the path is. Normally you don’t have to bother. In Windows 10 for example, you find these files under: C:\Users\<MyUserName>\AppData\Roaming\Godot\app_userdata\<MyProjectName>

wombatstampede | 2019-04-03 10:16

So how do I access the user: // file on Android?

Do I have to connect to my pc and find the file on my pc?

I will use a method to clear the file and recreate it to make sure that the changes in the values are applied correctly

To do so, I need to be able to access the file

MungMoong | 2019-04-03 14:18

user:// is a whole path. So you can save your file under that path like you tried with res://

const save_path = "user://save.json"

You can access that file from your app.
You can delete that file from your app. If you want to know the contents then you can read it into a variable and dump the output with print(variable) while debugging it.

Directory offers some methods dealing with files. You can delete files or create directories as you like.
Directory — Godot Engine (3.1) documentation in English

On Android, all files in the user:// path will be deleted when you uninstall an app. (But not on updates)
So the easiest way to delete android files in user:// which you ie.e. created during testing is to simply uninstall the app.

wombatstampede | 2019-04-03 15:22

Right. I’ll have to test it by deleting the app and reinstalling it. Thank you very much !

MungMoong | 2019-04-03 16:29