How to save files to user:// on Android

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Ceilingdoor
:warning: Old Version Published before Godot 3 was released.

My code saves the player’s score into a file using ConfigFile class. It works perfectly on Windows but it doesn’t on Android. I tried adding the permission to read and write from/to the filesystem on export but it had no effect. Any ideas? Here’s the code in case it helps:

func _ready():
 if f.file_exists("user://save.cf"):
    cf.load("user://save.cf")
    get_node("score/best").set_text("Best score: " + str(cf.get_value("scores", "best")))
 else:
    get_node("score/best").set_text("No save file found, creating one")
    cf.save("user://save.cf")
    cf.load("user://save.cf")
 pass

func _notification(what):
 if what == MainLoop.NOTIFICATION_WM_QUIT_REQUEST:
    if f.file_exists("user://save.cf"):
       if cf.has_section("scores"):
          var best_score = cf.get_value("scores", "best")
          if score > best_score:
             cf.set_value("scores", "best", score)
             cf.save("user://save.cf")
       else:
          cf.set_value("scores", "best", score)
          cf.save("user://save.cf")
 pass

Try using File.open(..) == OK instead of file_exists(..)

Bojidar Marinov | 2016-04-15 17:51

Alternatively you can use Directory.file_exists() rather than File.file_exists(). BTW you don’t need those permissions. (Don’t need pass at the end of functions either)

edit: ok, I thought the above was true but looking in my code I do use File.file_exists() not Directory, so not sure what your problem is… I haven’t used ConfigFile.

duke_meister | 2016-04-15 23:18