How to know when a exported resource in a tool script is changed in the editor ?

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

Hello,

I am trying to generate a texture using the OpenSimplexNoise and apply it to a material with a script marked as tool, so I can see the changes in the editor.

But I can’t figure out how to know when a value inside the OpenSimplexNoise resource is changed, so I can update the texture.

I know about NoiseTexture, but I just want to try to do it myself to see how things works.

Here is my current code:

[Tool]
public class RandomMap : MeshInstance
{
    private int width;
    private int height;

    [Export]
    private OpenSimplexNoise openSimplexNoise;

    [Export]
    public int Width
    {
        get { return width; }
        private set { width = value; UpdateTexture(); }
    }

    [Export]
    public int Height
    {
        get { return height; }
        private set { height = value; UpdateTexture(); }
    }
    
    private SpatialMaterial material = null;

    public override void _EnterTree()
    {
        Material currentMaterial = GetSurfaceMaterial(0);

        if (currentMaterial == null || !(currentMaterial is SpatialMaterial))
        {
            material = new SpatialMaterial();
            SetSurfaceMaterial(0, material);
        }
        else
        {
            material = currentMaterial as SpatialMaterial;
        }
    }

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

    private void UpdateTexture()
    {
        ImageTexture texture = new ImageTexture();
        texture.CreateFromImage(openSimplexNoise.GetImage(Width, Height));
        material.AlbedoTexture = texture;
    }
}

I tried using openSimplexNoise.Connect("changed", this, "UpdateTexture") but it didn’t work. I think because it’s code doesn’t run on the editor.