0 votes

I have a Singleton called Inventory whose job is to immediately save changes to the player's inventory, such as when the player picks up some coins. Save data for my game uses the Godot SQLite plugin.

Initially I found that immediately saving to the database would cause a small chug in the game's framerate each time, so I put the save code into a thread:

func update_inventory(new_item : Dictionary):
    var thread = Thread.new()

func _update_inventory_threaded(new_item : Dictionary):                         

However, this did not seem to remove the lag, and in fact this occasionally causes the game to hard crash (i.e. the game just closes).

Is there some other way to have instant saving without this lag? I want the player to be able to close the game at any time and still have all their progress saved.

Godot version v3.3.stable.official
in Engine by (73 points)

2 Answers

0 votes

You're writing to disk in the middle of your game, disk access is one of the slowest things you can do on a computer, you're gonna get frame drops if you do it while the game is running. You should probably create distinct points (e.g. auto-saves) at which you actually write the updated data to disk and just cache it in a variable otherwise.

by (3,888 points)
0 votes

Have you tried using a single thread together with some sort of queue. If your Inventory changes often in a short amount of time you might be starting too many threads.

See here for an example: https://docs.godotengine.org/en/stable/tutorials/threads/using_multiple_threads.html#semaphores

by (1,517 points)
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 Frequently asked questions and 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 [email protected] with your username.