saving in android

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Themaker482
:warning: Old Version Published before Godot 3 was released.

Anyone has succeded on saving and loading data in android? What Iam trying to do is to save variables from a global script and the docs are confusing, and i tried to see of the File was created in the save function but i couldnt find anything, that function I copied from the docs, I suposed the File should be created… Someone achived to save global variables on android?

This is kind of similar to your other 2 threads?

duke_meister | 2016-06-10 03:55

yes, iam sorry for that, I was needed an answer but almost everyone sent me to the docs

Themaker482 | 2016-06-10 14:01

:bust_in_silhouette: Reply From: volzhs

This is simple version to save and load variables.

var save_data = { "key1":0, "key2": [1,2,3] }
save_play_data("user://play.data", save_data)
var load_data = load_play_data("user://play.data")

func save_play_data(path, data):
	var f = File.new()
	f.open(path, File.WRITE)
	f.store_var(data)
	f.close()

func load_play_data(path):
	var f = File.new()
	if f.file_exists(path):
		f.open(path, File.READ)
		var data = f.get_var()
		f.close()
		return data
	return null

that is a great example, just let me know two things:
in the var save_data “key” is the name of the variable that I want to save isnt it? the other thing is why in the "key1 there is a 0 and in the “key 2” there is [1, 2, 3], is related to a changing variable, or that is the way to build a dictionary?

Themaker482 | 2016-06-10 13:57

“key1”, “key2” are variable name and this way is just building dictionary.

volzhs | 2016-06-11 13:13

Great, and I know Iam very annoying but that thing in the key1 and key2 …
Why “key1” has :0 and key2 has :[1,2,3]

Themaker482 | 2016-06-11 20:22

And one thingo more? Really do you have tried this on android… I checked the app an it seems to have created a file but i couldnt find anywhere, in the data about the app in says that has 16 kb stored so i think that really has a saved file

Themaker482 | 2016-06-12 00:06

that shows just any variable is possible.
and yes. I made my game using that for android.
the saved file is located private place.
if you really want to see it, I think you need rooting your device.

volzhs | 2016-06-12 01:31

Hi, I read your code and tried several times but still fail.
This is my autoloaded global.gd, there are only two variable I want to store in save
Music and BgColor

var save_data = { "Music":1, "BgColor":0 }

var load_data = load_play_data("user://play.data")

func save_play_data(path, data):
    var f = File.new()
    f.open(path, File.WRITE)
    f.store_var(data)
    f.close()

func load_play_data(path):
    var f = File.new()
    if f.file_exists(path):
        f.open(path, File.READ)
        var data = f.get_var()
        f.close()
        return data
    if not f.file_exists(path):
        save_play_data("user://play.data", save_data)
    return null

And when I need to change variable, I do this

#change the value between 0 and 1
global.save_data = {"Music": 0, "BgColor": 0} 
save_play_data("res://play.data", global.save_data)

when I want to make if statement based on the key value in dictionary “load_data” i use

if global.load_data["Music"] == 1:

Whne I did all of this, debugger said Invalid get index 'load_data' (on base: 'Nil').
and the errors said

0:00:00:0264 - Script does not inherit a Node: res://ScenesAndScripts/global.gd
----------
Type:Error
Description: Script does not inherit a Node: res://ScenesAndScripts/global.gd
Time: 0:00:00:0264
C Error: Condition ' !valid_type ' is true. Continuing..:
C Source: main/main.cpp:1633
C Function: start

Pls tell me during which steps I made mistake and how to fix this. I just want to store some integer numbers ><

zhangyao | 2018-07-18 13:22

in case anyone has the same problem. I think you need to call load_play_data again. its returned Nil because var load_data only executed once in the beginning when there is no data has been saved in “user://play.data”.

so your code should be:
load_data = load_play_data(“user://play.data”)
if global.load_data[“Music”] == 1:

daniteer | 2020-04-29 06:25

:bust_in_silhouette: Reply From: Lucrak

okay. did you save in android