+6 votes

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

in Engine by (49 points)
recategorized by

1 Answer

+12 votes

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

   var savegame = File.new() #file
   var save_path = "user://savegame.save" #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():
   savegame.open(save_path, File.WRITE)
   savegame.store_var(save_data)
   savegame.close()

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):
    create_save()

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
   savegame.open(save_path, 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():
   savegame.open(save_path, 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...

by (427 points)
edited by

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.listdirbegin()/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.

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

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

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

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

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

@Bojidar Marinov whaaaaaat? wow this is kind of unfortunate for me.. :/ 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..(?)

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 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 webmaster@godotengine.org with your username.