Your initializing code is a bit strange:
var save_data = {"score": score} # data that will be saved
--> this initializes the value of "score" to the unknown value of score (that variable doesn't exist)
perhaps better:
var save_data = {"score": 0} # data that will be saved
here you always overwrite your play data before loading it. But perhaps that is only for test reasons:
func _ready(): #ready
save_play_data("user://play.data", save_data)
loaded_data = load_play_data("user://play.data")
set_data(loaded_data)
Generally, saving should work on android in the user:// path.
Here's the code which I use. (It is a bit more complicated because I use a backup-file for fallback in case the last write got corrupted.). No guarantee its error free:
var status
var status_init = { "version": 1,
"score": 1000
}
const SAVE_GAME = "user://save_game.dat"
const SAVE_BAK = "user://save_game.bak"
func load_status():
var file = File.new()
var data = ""
if file.file_exists(SAVE_GAME):
file.open(SAVE_GAME, file.READ)
data = file.get_as_text()
file.close()
print("Status loaded from "+SAVE_GAME)
if data == "":
if file.file_exists(SAVE_BAK):
file.open(SAVE_BAK, file.READ)
data = file.get_as_text()
file.close()
print("Status backup loaded from "+SAVE_BAK)
if (data == "") or (data == null):
status = status_init
print("Status new? -> Init")
else:
status = {}
status.parse_json(data)
if (status!=null) and (status.has("version")):
print("Status parsed ok")
save_status(SAVE_BAK)
func save_status(file_name = SAVE_GAME):
var file = File.new()
file.open(file_name, file.WRITE)
file.store_string(status.to_json())
file.close()
print("Status saved to: "+file_name)