Attention | Topic was automatically imported from the old Question2Answer platform. | |
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