0 votes

I tried to do as they say in other answers, but I can't do it. in windows open gives "0", but on android nothing. I also tried to give permission when compiling to apk. in windows this work.

func _notification(what):

func _ready():
    score = load_data()['score']

func save_data(data: Dictionary) -> void:
    var file := File.new()
    file.open('user://save.data',  File.WRITE)

func load_data():
    var file := File.new()
    if file.file_exists('user://save.data'):
        file.open('user://save.data', File.READ)
        var data = file.get_var()
        return data
Godot version 3.2.3.stable.official
in Engine by (159 points)

1 Answer

+2 votes
Best answer

It looks like your score is not being saved when you quit because you are not listening to the correct notification, this is in the official docs:

On desktop platforms, the MainLoop has a special MainLoop.NOTIFICATION_WM_QUIT_REQUEST notification that is sent to all nodes when quitting is requested.

On Android, MainLoop.NOTIFICATION_WM_GO_BACK_REQUEST is sent instead. Pressing the Back button will exit the application if Application > Config > Quit On Go Back is checked in the Project Settings (which is the default).

(Edited to add format into the constants)

by (1,002 points)
selected by

thanks, it works when the back button is pressed, but if I turn off the game by removing it from the list of open applications it does not work.

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 Frequently asked questions and 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 [email protected] with your username.