How to save game progress on android?

:bust_in_silhouette: Reply From: kidscancode

First, make sure you’re saving in the right place. User data needs to be saved in user://. On Android, this will be the only writeable path.

Here’s some example code to load and save a high score. It should get you started:

var score_file = "user://highscore.save"
var highscore

func load_score():
    var f = File.new()
    if f.file_exists(score_file):
        f.open(score_file, File.READ)
        highscore = f.get_var()
        f.close()
    else:
        highscore = 0

func save_score():
    var f = File.new()
    f.open(score_file, File.WRITE)
    f.store_var(highscore)
    f.close()

Oh my god it’s you! I’ve been following you on Youtube in your tutorials and you were the one who got me started on Godot! I will try your sample code and will check back if I get it right. Thank you!

bodicpen | 2019-06-28 05:46

It worked! I knew there was a simpler method than those youtube videos. Thank you so much!

bodicpen | 2019-06-28 06:18

Just a question though, in your load_score() function, why would you create a new file then check if that file exists? I assume that we are checking if there are saved data but even if there is not, there would be because we just created a new file before we check?

bodicpen | 2019-06-28 07:59

The line var f = File.new() creates a new File object, not a new file on the disk. This File object is how you interact with files on the disk.

jandrewlong | 2019-06-28 13:48

not working apk export.

Mrtoxa42 | 2021-12-20 19:25