Save Record?

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

Hi guys, i’m trying to make a record game for android. However i don’t know how to save the record that is done and when you quit and next time you want to play, the score is saved. Can someone please help me understanding how to do it? for example i have a record saved in a variable, how to make to not loose that value? Thanks

:bust_in_silhouette: Reply From: tiernich

You need a file, a path, and a variable to store data. Put on the global script:

   var savegame = #file
   var save_path = "user://" #place of the file
   var save_data = {"highscore": 0} #variable to store data

the file will be stored in:

Windows: %APPDATA%\Godot\app_userdata\Project Name
GNU/Linux: $HOME/.godot/app_userdata/Project Name

i have a function to create the save

func create_save():, File.WRITE)

i run this function in the_ready() function on the global script
this will create the file if don’t exist. if exist, do nothing.

func _ready():
  if not savegame.file_exists(save_path):

Then i have a function to save the data on the global script also, to call in any part of the game with get_node("/root/Globalscript).save():

func save(high_score):    
   save_data["highscore"] = high_score #data to save, File.WRITE) #open file to write
   savegame.store_var(save_data) #store the data
   savegame.close() # close the file

and another function to read the data in the global script:

func read_savegame():, File.READ) #open the file
   save_data = savegame.get_var() #get the value
   savegame.close() #close the file
   return save_data["highscore"] #return the value

with those function you can save and read from any place in the game. The code is far from perfect, I made in the process of learn how to do it, but it can give you the base to start…

I’m new to Godot as well and I’m writing for Android like the OP. My comment is not on your answer, which should be fine for Android also. I have saving/loading working in my app, but if I try to read the user:// folder (to see which files are there), I get an error (i.e. I do not get 0 returned from dir.list_dir_begin()/dir.get_next(). This same code works on Windows. Do you have any idea why this might be happening? The user:// folder is supposed to accessible any time, and should work without any special Android permissions as I understand it.

duke_meister | 2016-03-25 10:49

Nice use of code blocks! I just had to add a few inline ones :slight_smile:

Bojidar Marinov | 2016-03-25 14:04

@duke_meister ~ directory listing doesn’t work on android.

Bojidar Marinov | 2016-03-25 14:05

well, i never tried nothing on android yet, so have no clue. But you can ask in this Q&A, i bet someone will now why this is happening

tiernich | 2016-03-25 14:06

thanks Bojidar! i will rewrite the code for my current project, this one need some polish but still works :smiley:

tiernich | 2016-03-25 14:11

oh, i get what you mean, just read the “how to use this Q&A” next time will use inline haha :smiley:

tiernich | 2016-03-25 14:18

@Bojidar Marinov whaaaaaat? wow this is kind of unfortunate for me… :confused: Anywhere I can read about this? i.e. why it doesn’t, and if it will ever work? Thanks. I can think of work-arounds, but it’s really bad. I don’t think it should be a technical issue because of Android…(?)

duke_meister | 2016-03-25 21:31