Cannot acces object fields after serialization from ConfigFile

Godot Version

v4.2.1.stable.official [b09f793f5]

Question

Hello! I am writing editor plugin for my project and I had been faced with this problem.
I need to store some settings, for example array of custom Objects:

extends Object

class_name SheetSettings

var attributes: Array[AttributeData]
...

Saved object:

extends Object

class_name AttributeData

var attribute_id: int
var attribute_name: String

I had read this doc about ConfigFile (ConfigFile — Godot Engine (stable) documentation in English) and it seemed perfect for my task. So, I had written something like this:

func _load_my_settings():
    var config = ConfigFile.new()
    config.load("config/path")
    for key in config.get_section_keys("attributes"):
        var attribute = config.get_value("attributes", key)
        print(attribute.attribute_id)


func _save_my_settings():
    var config = ConfigFile.new()
        for item in current_settings_file.attributes:
            config.set_value("attributes", str(item.attribute_id), item)
    config.save("config/path")

Object fields saves corretly. But, when I test load func this thing happens:

Invalid get index 'attribute_id' (on base: 'Object (AttributeData)')

Declaring type explicit doesn’t solve problem. Maybe I use ConfigFile not in proper way. But it should work, I do not understand what’s wrong. Seems like object loaded without customization, but it reffers on my class name… just do not understand!

Will appreciate any help with this)

The error is telling you something is not what you assumed about your variables. This kind of thing is usually about key names - spelling and wrong paths.

As to saving data - this is what Resources are built for. They are quite easy to use and will do most of the hard work for you.

There are several good tuts on YT. I made a very short intro too: https://www.youtube.com/watch?v=8ybDgDvAVNk

hth

1 Like

Thanks for the reply! Great video, I think I will use Resource instead of Object. At least when I changed the classes on using them and loading via ResourceLoader.load() and saving with ResourceSaver.save() worked as intended.

However, why my problem with the ConfigFile occurred is not quite clear. I used the correct field names, also, config.get_section_keys() returned correct key names and get_value returned object with instance id. But still calling attribute.attribute_id returned same error.

Maybe this is not the way to serialize objects from ConfigFile. I am just curious what I am doing wrong)

I suspect it’s about Object lifetime. If you ‘save’ an instance, restart Godot, and try to ‘load’ it - the instance no longer exists. You’d have to .new() the class first before you can access attribute_id in any way.

It really depends on what current_settings_file is. Also would help to see the config file.

Ok, I’ll give it a try if I need to save the Object somehow and it doesn’t work out to use the Resource instead.

Thanks again for your help!

1 Like