|
|
|
|
Reply From: |
tiernich |
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…
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
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
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
tiernich | 2016-03-25 14:18
@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…(?)
duke_meister | 2016-03-25 21:31