Set Editable children through script fails

Godot Version

4.3

Question

Hi all,

I have a scene object that I would like to be set to allow editable children when the user adds it to their scene (in the editor). like this -
Screenshot 2024-11-20 at 12.03.30 pm

I made this simple script that I’ve added to the object -

using Godot;

[Tool]
public partial class TestInit : Node3D
{
	public override void _Ready()
	{
		SetEditableInstance(this, true);
	}
}

But it fails with this error - scene/main/node.cpp:2503 - Condition “!is_ancestor_of(p_node)” is true.

I guess it just doesn’t like to set itself as editable? Any ideas?

Thanks,
Pete

Have you tried getting the immediate parent get_parent() then called editable on yourself as a child?

Thanks @pennyloafers, I tried this and it didn’t work. Didn’t throw an error though so it might have been on the right track.

this.GetParent().SetEditableInstance(this, true);

It’s probably weird because it’s in a [Tool] script :man_shrugging: It’s okay, I’ll just manually do it :slight_smile:

1 Like

I would have to look in the source code, you may need to notify the editor of the change, but this is a guess.

I also don’t fully understand the reason why an ancestry matters. In some sense if the edited scene is the root ( I.e. it is already is in an edit mode) what will the function do? That’s what I would also want to investigate.

Yeah… I think you are editable but the SceneTreeDock needs to be updated, but I don’t see a means to notify the dock of the change and replace the placeholder with your tool scene instance.

Hey thanks for checking that out in more detail! :slight_smile:
It’s all good, I sometimes get a bit obsessed with automating things and I thought it might have just been something simple I was getting wrong :crazy_face:.

Okay this is a bit whacky but it doesn’t throw an error.

    private bool setEditable;
    public override void _Process(double delta)
    {
        if (!Engine.IsEditorHint()) return;
        if (setEditable) return;
        this.GetParent().SetEditableInstance(this,true);
        GD.Print(this.GetParent().IsEditableInstance(this));// Returns true
        setEditable = true;
    }

Screenshot 2024-11-26 at 5.31.56 pm

Only thing is it doesn’t update in the editor until you reload the scene… :thinking: Is there a way to maybe refresh the current tree?

Actually, this seems to be working now!

this.GetParent().SetEditableInstance(this,true);

I must have been doing something stupid :roll_eyes: