+2 votes

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?

in Engine by (91 points)

This is kind of similar to your other 2 threads?

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

2 Answers

+2 votes
Best answer

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
by (9,674 points)
edited by

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?

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

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]

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

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.

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 ><

in case anyone has the same problem. I think you need to call loadplaydata 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:
loaddata = loadplaydata("user://play.data")
if global.load
data["Music"] == 1:

0 votes

okay. did you save in android

by (22 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.