Autosave Mobile Game

Godot Version

4

Question

I am creating a casual mobile game app, and i was wondering how can i autosave in a similar manner to SubwaySurfers? Essentially when the user buys cosmetics, the game will automatically save, and the next time they open the app, their cosmetics will stay in their inventory. If anybody can provide some insight, it would be greatly appreciated>

I’m not sure what exactly you’re looking for, but I’m guessing you’re looking for a way to save data to a file and load it the next time you open the game, which isn’t that complicated in itself.

Since you’re new to the forum, I’m guessing you’re new to Godot, so I won’t go into too much detail and suggest the quickest way:

  • Step 1: Installing SLib
    SLib is one of the libraries that supports saving and loading files, you don’t have to use it or another library for this, but it is faster and reduces the chance of errors.
    You can install it with 4 steps from here

  • Step 2: Add a variable
    Once you have installed and activated SLib, you just need to create a variable to hold the data along with other variables in your script. For example, suppose we have two options in the store for purchase:

var shop_items := {
	item_1: false,
	item_2: false,
}
  • Step 3: How to save
    You can do this simply with one line of code (note that on mobile you can only use paths that start with user://):
SLib.save_file("user://shop_items.save", shop_items)
  • Step 4: How to load
    Now we can set this variable from a file wherever needed , probably loading it once at the start of the scene will be enough for you:
func _ready():
	shop_items = SLib.load_file("user://shop_items.save")

This way you can save and load any variable, if you save a variable the next time you load it it will give you the saved content.

1 Like

You would have to put the code in for each time they buy something. Build a node which can save all data with one function call. Put that node and function call in the source.

1 Like

Thank you. I’ll give it a try

Thank you.