Godot: Saving data works on windows but not on android?

Viewed 365

I am testing how to store game data using Godot 3.2.2 (I also tested Godot 3.2.3-rc4). When I exported the project (basicly you press a button and 1 is added to the score, which is displayed by a label) on Windows everything worked fine, the score was saved. But when I exported it to my Android phone the data was not saved. In the export setting I have enabled all permissions and in the settings of my phone I enabled the storage permission to my app. Maybe anybody of you can explain this to me and tell me how to solve this problem, so that data is also stored on android.

Code:

extends Node2D

var score = 0
var path = "user://save_file.json"
var save_game_data

func _ready() -> void:
    load_data()
    set_label_text()

func _on_Button_pressed() -> void:
    score += 1
    set_label_text()

func set_label_text():
    $Label.text = str(score)
    
func _notification(what: int) -> void:
    if what == MainLoop.NOTIFICATION_WM_QUIT_REQUEST:
        save_game_data = {
            "score": score
        }
        save_data()
        
func save_data():
    var save_game = File.new()
    save_game.open(path, File.WRITE)
    save_game.store_line(to_json(save_game_data))
    save_game.close()


func load_data():
var save_game = File.new()
    if not save_game.file_exists(path):
        print("No save file found")
        save_data()
        return
    save_game.open(path, File.READ)
    save_game_data = parse_json(save_game.get_line())
    score = save_game_data["score"]
    print("Saved game loaded", save_game_data)
1 Answers

According to the docs on NOTIFICATION_WM_QUIT_REQUEST:

Implemented on desktop platforms.

NOTIFICATION_WM_QUIT_REQUEST is mainly so you can implement an "Are you sure you want to quit?" dialog, or similar. Mobile platforms generally don't have this option--when you press the home button, the window closes whether it wants to or not.

You could solve this by autosaving whenever the score changes (or at the end of a round, etc).

Related