Im working on a 3d horror game I want to be able to save progress how would I do that in Godot 4
How do I create a save system in my game for Godot 4
- Best Answerset by The_Grass_man
The_Grass_man You likely want to look at JSON. You can store everything from strings for scene files (great for loading the last room) or variables for stats or an array for inventory. Then you store all that in a dictionary and load it when needed.
Godot Official Docs - JSON Save System
But you would build a global object, like a data handler then give it some basic functions for saving/loading.
Example:
var _playerHealth = 100
var _playerBullets = 10
var _currentScene = ###insert file name in quotations here
var _saveDatabase = {} ## empty array for holding your data
func _save_data_update(): ## fill the dictionary (your save database)
_saveDatabase = {
"playerHealth" : _playerHealth,
"playerBullets" : _playerBullets,
"currentScene": get_tree().current_scene.filename
}
## then we store that data
func _save_function(): ## json data save function
var _myFile = File.new()
_myFile.open(_GAMESAVE, File.WRITE)
_save_data_update() ## MAKE SURE TO USE THE UPDATE FUNCTION while you save!!!
_myFile.store_line(to_json(_saveDatabase))
_myFile.close()
## and we can load it with another function when needed and we update our stats
func _load_function(): ## json data load function
var _saveFile = File.new()
_saveFile.open(_GAMESAVE,File.READ)
var _text = _saveFile.get_as_text()
_saveData = parse_json(_text)
_currentScene = str(_saveDatabase.currentScene)
_playerHealth = _saveDatabase.playerHealth
_playerBullets = _saveDatabase.playerBullets
Just be certain to build some error handling in if there is no file to load or - even easier - build your logic to not show a load function if there's no save.
- Edited
SnapCracklins for the var _currentScene = do I put the file where I'm storing the save or the scene
- Edited
The_Grass_man yeah don't just copy and paste the code. The indent system never translates right. That error shows the Indents are in the wrong places. You should punch it in and fix the indents manually. Oh the joys of sending data everywhere.