0 votes

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.

in Engine by (35 points)
edited by

1 Answer

+1 vote

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 ;)

by (364 points)

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

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

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.

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 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 webmaster@godotengine.org with your username.