Having some more fun with JSON dictionary saving. Trying to save a bool and retrieve it and str2var/var2str isn't working? I'm in the latest stable build of Godot 3.6. Having the damndest time pulling it from the docs.

EDIT: Found out it was not the data, it was how I was loading it. D'oh!

    Well, I am making an options menu. Trying to store the bools with var2str and reclaim them with str2var with no luck. It keeps saying it can't pull from the dictionary so I must be doing something wrong. I'm not gonna bother posting the header (it's not the problem) but all the var2str ones are supposed to be bool. Invalid get index "_crt_filter" on dictionary. I should mention my OptionsNode is a global.

    ## saving settings to dictionary
    func _saveOptionsSettings():
    	_optionsData = {
    		"_crtFilter" : var2str(_crtFilter),
    		"_motionFilters" : var2str(_motionFilters),
    		"_noiseFilters" : var2str(_noiseFilters),
    		"_noPrompts" : var2str(_noPrompts),
    		"_itemTextContrast" : var2str(_itemTextContrast),
    		"_staminaMeterHud" : _staminaMeterHud,
    		"_globalBgmLevel" : _globalBgmLevel,
    		"_globalSfxLevel" : _globalSfxLevel,
    		"_globalVoiceLevel" : _globalVoiceLevel,
    		"_deadzoneCurrent" : _deadzoneCurrent,
    		"_hardAnalogAutorun" : var2str(_hardAnalogAutorun),
    		"_analogRunZone" : _analogRunZone
    	}
    	return _optionsData
    
    ## saving option file
    func _saveOptionsData():
    	var _myFile = File.new()
    	_myFile.open(_PREFERENCES, File.WRITE)
    	_saveOptionsSettings()
    	_myFile.store_line(to_json(_optionsData))
    	_myFile.close()
    
    ## loading options file
    func _loadOptionsData():
    	var _directory = Directory.new()
    	if _directory.file_exists(OptionsNode._PREFERENCES):
    		var _load = File.new()
    		_load.open(_PREFERENCES,File.READ)
    		var _loadFile = _load.get_as_text()
    		parse_json(_loadFile)
    		
    	
    		_crtFilter = str2var(_optionsData._crtFilter)
    		_motionFilters = str2var(_optionsData._motionFilters)
    		_noiseFilters = str2var(_optionsData._noiseFilters)
    		_noPrompts = str2var(_optionsData._noPrompts)
    		_itemTextContrast = str2var(_optionsData._itemTextContrast)
    		_staminaMeterHud = _optionsData._staminaMeterHud
    		_globalBgmLevel = _optionsData._globalBgmLevel
    		_globalSfxLevel = _optionsData._globalSfxLevel
    		_globalVoiceLevel = _optionsData._globalVoiceLevel
    		_deadzoneCurrent = _optionsData._deadzoneCurrent
    		_hardAnalogAutorun = str2var(_optionsData._hardAnalogAutorun)
    		_analogRunZone = _optionsData._analogRunZone
    	
    	else:
    		print("No preferences found! Loading defaults.")
    		_defaultOptionsSettings()
    • xyz replied to this.

      SnapCracklins Print the dictionary to se what's inside. And take a look at json file to see what's in there.

        xyz weird. it sure looks right. Also trying straight strings with no luck.

        {"analogRunZone":1,"crtFilter":"False","deadzoneCurrent":0.2,"globalBgmLevel":0,"globalSfxLevel":0,"globalVoiceLevel":0,"hardAnalogAutorun":"False","itemTextContrast":"False","motionFilters":"False","noPrompts":"False","noiseFilters":"False","staminaMeterHud":1}

        So it looks like I may have targeted my load wrong.... but the saving is working fine. Load keeps giving me errors, saying it's loading a string. Also, checked the return data - it all looks good. It's still getting stuck on the booleans when converting them back,

        • xyz replied to this.

          xyz yeah i noticed that, it was a code error. But even then, the string contents are correct. I have heard some people make a parsing function to get around this issue - would that work or am I way overthinking this?

          xyz the function that does it is the _saveOptionsData() function which occurs during the final save function. it seems the loading is the issue, the dictionary readout from Godot is correct.

          • xyz replied to this.

            SnapCracklins Print the dictionary after loading and parsing. Your code calls parse_json() but never takes its return value. Also you do some confusing things like setting a class property in a method and also returning that same property value from that method. A method should do only one of those things. It either operates on class properties or returns a value. Doing both is confusing as hell.

              xyz that's what bothering me - I shouldn't have to do that. I have an actual game save system separate from options so it can keep preferences across playthroughs. It does this flawlessly and loads bools just fine. I'm just confused. And now it doesn't even want to load this file. Back to the coding board...

              xyz yup you were correct. I was forgetting to load into that dictionary then parse it back to the globals. All working now!!! Amazing how two words of code can muck up everything.

              This might need a its own topic, but have you noticed that integer values changed to float types when you save them into the JSON and then load them back?

              The actual value does not seems to change, but if you check the type with typeof() -function, it has been changed. At least I have encountered this kind of behaviour.

                TheAspen some data has that behavior, it has to do with arrays converting to strings. I recently had an issue with storing Vector2 data in a global and it turns out, when that global_position data gets put in a string, you have to do something (str2var()) to get it back. Data is quite selective it seems. But little problems like that - solving them make you a better programmer because then you don't get lost or derailed next time it comes around. Certainly on-topic.

                • xyz replied to this.

                  xyz something I will have to add to my toolbelt. For what its worth, with the problem you helped me solve earlier, I ended up making a separate file for my settings. Works a treat, I was totally loading it to the wrong place (as you said). So, I'm kind of doing something like that? Though if you have tips on efficiency, knock me out!