Code seems right, but won't write to project.godot

Godot Version

4.7.1

Question

Hi all! I’ve got this question up on Reddit already, but the code is changed enough and the problem become perplexing enough that I thought I’d bring it over here.

I’m making a plugin and want to add settings to the Project Settings. plugin.cfg points to testing.gd, which looks like this:

@tool
extends EditorPlugin

func _enter_tree() -> void:
    _check_settings()

func _check_settings() -> void:
    var the_name = "plugins/testing/test_value"
    if !ProjectSettings.has_setting(the_name):
        ProjectSettings.set_setting(the_name, 0)
        ProjectSettings.set_initial_value(the_name, 0)
        ProjectSettings.set_as_basic(the_name, true)
        ProjectSettings.add_property_info(
            {
                "name" : the_name,
                "type" : TYPE_INT,
                "hint" : PropertyHint.PROPERTY_HINT_ENUM,
                "hint_string" : "Blue,Red,Green,Yellow,White"
            })
    ProjectSettings.save() # does not seem to actually do anything, but added on advice

After activating the plugin and checking the Project Settings window, I can see the Plugins/Testing category with the Test Value option, as a basic setting and listing the values correctly from the enum property hint. I leave it alone for the moment.

I then have a simple Control scene that runs a print call from _ready():

print(ProjectSettings.get_setting("plugins/testing/test_value"))

It prints <null>

Checking the project.godot file, there is no added [plugins] category at all, much less testing/test_value=0

I go back into the editor, go to Project Settings, find my setting, change the value, and close Project Settings. Running my scene, it prints the correct value. Checking project.godot, the setting is now there with the correct value.

What do?

Are you checking the error?

var err := ProjectSettings.save()
if err == OK:
    print("Saved project settings successfully")
else:
    push_error("Failed to save project settings because: ", error_string(err))

Got 0 (OK)

Try moving set_initial_value and add_property_info to outside the if block and see if it fixes. I believe those are supposed to be defined every time the plugin is started, because they are not persisted anywhere. This is how I do for my plugins at least.

I don’t know exactly why this happens, but I know Godot removes values that match the initial value defined, so maybe it gets confused when it starts the plugin and it’s not defined and removes the persited valeu regardless

Will do, and I think I take your meaning but just to make sure: The value is set, but it’s not being removed from project.godot, it simply never gets added even when I’m looking straight at it in the Project Settings window.

I think I might have misunderstood your issue. I read it again and it seems like in your first execution you do not change any values in the project settings right?

If that’s the case, the behaviour you are describing is correct.

set_initial_value is just a hint to the Godot editor to know what to show in the interface if no value is set, but I don’t think it works as a default value for get_settings. Your code should be responsible for handling undefined values and fallback to your default value.

In your case you are indeed setting the value, but as you set the same value as initial value after, it triggers the behaviour I mentioned that removes default settings from the project file.

Eyyy! On your advice I commented out the set_initial_value line and it immediately worked. I think maybe the documentation for set_initial_value needs to be updated to make that clearer.

Thank you!