How do I Save Settings in a Config File?

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

I’ve been trying to save settings to a config file, but keep failing. I have some code and I don’t know whats wrong with it. Here it is:

extends Node2D

const SAVE_PATH = "res://settings.cfg"

var mouseSensitivty = 3/10

var _config_file = ConfigFile.new()
var _settings = {
	"Video": {
		"FullScreenMode": ProjectSettings.get(size/fullscreen),
		"SRX": ProjectSettings.get(size/width),
		"SRY": ProjectSettings.get(size/height),
		"VSYNC": ProjectSettings.get(vsync/use_vsync)
		 },
	"Game": {
		"FOV": get_fov(),
		"Sensitivty": mouseSensitivty
		}
	}
func _ready():
	pass

func save_settings():
	for section in _settings.keys():
		for key in _settings[section].keys():
			_config_file.set_value(section, key, _settings[section][key])
	_config_file.save(SAVE_PATH)

I need some things explained as I don’t understand what went wrong and why. The whole computer froze when I tried to run this script. I didn’t use autoload. I used a Node2D with a script attached. The script is also incomplete but gave no errors in the editor.

:bust_in_silhouette: Reply From: guppy42

The problem is here:

for section in _settings.keys():
    for section in _settings[section]:
        //code

the first for loop create a variable named section, then the 2nd one also does this and then uses it to reference an index in the settings array index.

try something like this instead

for section in _settings.keys():
    for key in _settings[section].keys():
        //code

ps. please use the " { } " icon to insert code it’s quite hard to read otherwise :wink:

Thank you. I will fix my code in Godot and put my code in braces here.

Noddy | 2018-11-28 12:16

I tried it in Godot and the program still crashed. Do you know where any more problems could be?

Noddy | 2018-11-28 17:20

I know this is a fairly old question, but I think the problem was from trying to save to the res:// path, I don’t think that is writable, since it will most likely be in a .pck file when deployed. You most likely want the user:// path.

drako0812 | 2020-01-10 07:47