#Tool script has set accessor that runs before _Ready

2 messages · Page 1 of 1 (latest)

steady nest
#

Hi, I have an exported property in a tool script. I changed it from its default value, so it's set accessor runs. It runs before the _Ready function, which does some initialization needed for the set accessor. Is there a way to change this behavior?

carmine haven
#

Not really, exported properties are set right after an object is created since it's possible to export properties from non-Node scripts (mostly Resources). You might want to use IsInsideTree for checks, or just don't interact with the tree in the setter.

I usually "solve" like that

public class Foo : Node
{
    private string text;
    [Export] public string Text
    {
        get => text;
        set 
        {
            text = value;
            if (IsInsideTree()) UpdateLabelText();
       } 
   } 

    public override void _Ready() 
    {
        UpdateLabelText();
    } 
    
    private void UpdateLabelText() 
    {
        var label =... //safe method to get nullable label, probably just a field 
        if (label == null) return;

        label.Text = text;
        // something extra
   } 
}