Saving a dictionary and loading WITHOUT Json

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By SnapCracklins

Apologies but all the answers I found for this were JSON which I don’t need.
I am saving a dictionary to a file and then loading it back. I only have a scene and about 15 variables to load. Didn’t find what I needed in the docs.

    func _save_function():
	    var _saveFile = File.new()
	    _saveFile.open(_gamesave, File.WRITE)
	
	    var _savedata = {
		    _savedScene = str(_savedScene),
		    _eventLogArray = _eventLogArray.duplicate(),
		    _lightsArray = _lightsArray.duplicate(),
		    _holdArray = _holdArray.duplicate(),
		    _TOP_STAMINA = _TOP_STAMINA,
		    _TOP_WALKSPEED = _TOP_WALKSPEED,
		    _TOP_RUNSPEED = _TOP_RUNSPEED,
		    _PLAYER_LUCK = _PLAYER_LUCK,
		    _ENEMY_LUCK_LIMIT = _ENEMY_LUCK_LIMIT,
		    _staminaJar = _staminaJar,
		    _isExhausted = _isExhausted,
		    _destTarget = _destTarget,
		    _playerItems = _playerItems,
		    _storyItems = _storyItems,
		    _keysRemaining = _keysRemaining
	}
	
	    _saveFile._game_data = _savedata
	    _saveFile.close()


func _load_function():
	var _loadFile = File.new()
	_loadFile.open(_gamesave, File.READ)
	
	_savedScene = _gamesave._savedScene
	_eventLogArray = _gamesave._eventLogArray.duplicate()
	_lightsArray = _gamesave._lightsArray.duplicate()
	_holdArray = _gamesave._holdArray.duplicate()
	_TOP_STAMINA = _gamesave._TOP_STAMINA
	_TOP_WALKSPEED = _gamesave._TOP_WALKSPEED
	_TOP_RUNSPEED = _gamesave._TOP_RUNSPEED
	_PLAYER_LUCK = _gamesave._PLAYER_LUCK
	_ENEMY_LUCK_LIMIT = _gamesave._ENEMY_LUCK_LIMIT
	_staminaJar = _gamesave._staminaJar
	_isExhausted = _gamesave._isExhausted
	_destTarget = _gamesave._destTarget
	_playerItems = _gamesave._playerItems.duplicate()
	_storyItems = _gamesave._storyItems.duplicate()
	_keysRemaining = _gamesave._keysRemaining

	_loadFile.close()

I’m curious why you’re against JSON? Since a Dictionary maps well to a JSON structure, and simple methods are provided to save/load a dict to/from a JSON structured file, does the underlying format really matter?

jgodfrey | 2022-08-27 00:18

This is where I get confused. I’ve read many articles against using JSON (its the first thing on a Google search when you look up “Saving games in GODOT with JSON”) and many for using it. It seems though JSON may be what I need though.

SnapCracklins | 2022-08-27 00:48

If you don’t want to use JSON, that’s fine. You can save data in other ways. One way could be to use the store_var() method of the File class. This may store the data like you want it. After that, you may be able to retrieve it with the get_var() method. Note that this stores data in binary, i.e. not readable by a text editor.

Ertain | 2022-08-27 01:11

Yeah i tried that and got mixed results. May take another look though.

SnapCracklins | 2022-08-27 01:17

Here’s some info on saving as JSON.

how to save dictionary into a file - Archive - Godot Forum

jgodfrey | 2022-08-27 01:40

I saw several of your answers. I guess my next question is how do I get all these variable references back to the right places?

SnapCracklins | 2022-08-27 01:48

It somewhat depends on the organization of your project. One easy way would be to just create an autoload script to handle all of your persistence. Then, just add the necessary save and load functions, and call it from wherever you need.

jgodfrey | 2022-08-27 02:13

Thank you for your time. I did up going JSON and it worked beautifully. Is there a reason people hate on JSON so much? Seems easy street for saving once you learn it.

SnapCracklins | 2022-08-27 23:55

:bust_in_silhouette: Reply From: SnapCracklins

Did go with JSON after all, here’s the updated code for those wondering. Thanks to jgodfrey for his help! The _savedata is declared in an empty dictionary on load from the global.

    func _save_data_update():  ### save global variables to dictionary
	_saveData = {
		"_savedScene" : get_tree().current_scene.filename,
		"_eventLogArray" : _eventLogArray,
		"_lightsArray" : _lightsArray,
		"_holdArray" : _holdArray,
		"_TOP_STAMINA" : _TOP_STAMINA,
		"_TOP_WALKSPEED" : _TOP_WALKSPEED,
		"_TOP_RUNSPEED" : _TOP_RUNSPEED,
		"_PLAYER_LUCK" : _PLAYER_LUCK,
		"_ENEMY_LUCK_LIMIT" : _ENEMY_LUCK_LIMIT,
		"_staminaJar" : _staminaJar,
		"_isExhausted" : _isExhausted,
		"_destTarget" : _destTarget,
		"_treasurePool" : _treasurePool,
		"_playerItems" : _playerItems,
		"_storyItems" : _storyItems,
		"_keysRemaining" : _keysRemaining
	}
	return _saveData


func _save_function():  ## json data save function
	var _myFile = File.new()
	_myFile.open(_GAMESAVE, File.WRITE)
	_save_data_update()
	_myFile.store_line(to_json(_saveData))
	_myFile.close()


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)

	_savedScene = str(_saveData._savedScene)
	_eventLogArray = _saveData._eventLogArray
	_lightsArray = _saveData._lightsArray
	_holdArray = _saveData._holdArray
	_TOP_STAMINA = _saveData._TOP_STAMINA
	_TOP_WALKSPEED = _saveData._TOP_WALKSPEED
	_TOP_RUNSPEED = _saveData._TOP_RUNSPEED
	_PLAYER_LUCK = _saveData._PLAYER_LUCK
	_ENEMY_LUCK_LIMIT = _saveData._ENEMY_LUCK_LIMIT
	_staminaJar = _saveData._staminaJar
	_isExhausted = _saveData._isExhausted
	_destTarget = _saveData._destTarget
	_treasurePool = _saveData._treasurePool
	_playerItems = _saveData._playerItems
	_storyItems = _saveData._storyItems
	_keysRemaining = _saveData._keysRemaining
	
	var _nowLoaded = get_tree().change_scene(_savedScene)  ### load scene