0 votes

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,
            _PLAYER_LUCK = _PLAYER_LUCK,
            _staminaJar = _staminaJar,
            _isExhausted = _isExhausted,
            _destTarget = _destTarget,
            _playerItems = _playerItems,
            _storyItems = _storyItems,
            _keysRemaining = _keysRemaining

        _saveFile._game_data = _savedata

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
    _PLAYER_LUCK = _gamesave._PLAYER_LUCK
    _staminaJar = _gamesave._staminaJar
    _isExhausted = _gamesave._isExhausted
    _destTarget = _gamesave._destTarget
    _playerItems = _gamesave._playerItems.duplicate()
    _storyItems = _gamesave._storyItems.duplicate()
    _keysRemaining = _gamesave._keysRemaining

Godot version Godot 3.5 stable
in Engine by (548 points)

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?

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.

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.

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

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?

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.

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.

1 Answer

0 votes

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,
        "_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)

func _load_function():  ## json data load function
    var _saveFile = File.new()
    var _text = _saveFile.get_as_text()
    _saveData = parse_json(_text)

    _savedScene = str(_saveData._savedScene)
    _eventLogArray = _saveData._eventLogArray
    _lightsArray = _saveData._lightsArray
    _holdArray = _saveData._holdArray
    _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
by (548 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.