Forcing re-render on exported property update

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By dearme

I would like to know if there is a more elegant solution than what I have accomplished.

I would like the tool to trigger editor/game re-render whenever there is a change of the exported value of the said tool… I am using C# and here is the script that draws a circle:

using Godot;

[Tool]
public partial class CirclePath : Path2D
{
    private float _startRadius = 100.0f;

    [Export]
    public float StartRadius {
        get
        {
            return _startRadius;
        }
        set{
            _startRadius = value;
            ExecuteScript();
        }
    }

    private int _numPoints = 10;

    [Export]
    public int NumPoints
    {
        get
        {
            return _numPoints;
        }
        set
        {
            _numPoints = value;
            ExecuteScript();
        }
    }

    public override void _Ready()
    {
        ExecuteScript();
    }

    private void ExecuteScript()
    {
        this.Curve = new Curve2D();

        if (NumPoints == 0)
            return;

        for (int i = 0; i < NumPoints; i++)
        {
            Vector2 point = new Vector2(0, -StartRadius)
               .Rotated(((i / (float)NumPoints) * Mathf.Tau));

            this.Curve.AddPoint(point);
        }

        // end the circle
        this.Curve.AddPoint(new Vector2(0, -StartRadius));
    }
}

The problem here is that I have to

  • override the properties’ setter
  • introduce the fields and
  • manually invoke ExecuteScript(); method
    to force the redrawing of the circle when either NumPoints or StartRadius properties change. The more the properties there are the more there is boilerplate code.

I would like to avoid overriding the properties’ getters and setters every time. Is this possible to accomplish somehow?

edit: just to elaborate. If you leave the property as is [Export] public float StartRadius {get; set; } and then go to the editor of the scene and modify the value of the script, then in order to see the change you will have to close the scene and reopen it. I suppose in this case the _Ready is called and the circle gets drawn with the updated values. I would like the redraw/re-render to occur without the additional manipulations such as closing and re-opening of the scene every time the any of the script exported properties change.