How can I save the progress? (SOLVED)

Hey, I’m new here. I’m in 10th grade and I just started to learn how to use this engine. I made a clicker game for android phones and I’d like to know how can I save the progress. Can anyone explain this to me please ? :blush:

2 Likes

You can use something like

Or use the functions it uses directly.

7 Likes

thank you so much :smile:

1 Like

Oh but wait :confused: I don’t actually know how to implement it to my application. Isn’t there an easier way, since I only need a number to be saved so the app will know where to start from the next time I open it?

Yes, there is. First, you need to get a path to your save file with sys.get_save_file(), then use sys.save() and sys.load() to save and load a table with your number.

1 Like

Ok, I created the path. Now, when I enter the game, the counter starts with 0 and when it raises, I save the value to the save file that I created. So, I check if there is a path to save my game. If there isn’t, it creates the path and the counter starts with 0. Now, if there already is a path to save the game, how can I make the counter take the value from the save file?

The path is just the path to the savefile. When you call sys.get_save_file, the file doesn’t get created automatically. that happens only when you call sys.save.

So, first you get the path:

local savefile_path = sys.get_save_file("MyGameName", "savefile")

Then, when you want to save:

local counter = 42
sys.save(savefile_path, { counter = counter })

And when you want to load, remember the docs for sys.load() say that it returns an empty table if the file is not found. And if you try to index into an empty table, you’ll get nil. And since nil or 0 == 0, the following will fetch the counter from your savefile if the savefile exists or will be 0 if it doesn’t exist:

local counter = sys.load(savefile_path).counter or 0
4 Likes

In case it’s not clear why the above works, from Programming in Lua:

Like control structures, all logical operators consider false and nil as false and anything else as true. The operator or returns its first argument if it is not false; otherwise, it returns its second argument

1 Like

In sfarsit merge :smiley: Multumesc mult, atat pentru sfat cat si pentru site-ul cu limbajul Lua :slight_smile:

1 Like