Refresh drawing in editor

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Fido789
[Tool]
public class Level : Node2D
{
	[Export]
	public int FinishPosition;

	public override void _Draw()
	{
		if (Engine.EditorHint)
		{
			DrawRect(new Rect2(0, -540, FinishPosition, 1080), Color.Color8(255, 0, 0), false);
		}
	}
}

Hello!

The code above draws a red rectangle in editor. The rectangle is as wide as FinishPosition script variable. It works fine, however after FinishPosition is updated I have to close and reopen the scene to reflect the changes and actually resize the rectangle.

So I would like to ask you, is there a way to “refresh” the rectangle right after the FinishPosition is updated in inspector without need for reopening the scene?

I’m not sure if this will help but does calling update_overlays() from the EditorPlugin class work? https://docs.godotengine.org/en/stable/classes/class_editorplugin.html

ChildLearning.Club | 2023-02-22 05:58

By default, Godot only calls _Draw once and that’s it. This is for optimization purposes. If your draw method depends on class fields or any other value that can change, you must call QueueRedraw() whenever those values change so that the node is redrawn.

In your case, since you only depend on exported fields (assuming these fields will only change during dev time and not at runtime), you could simply call QueueRedraw() every frame in the editor.

    public override void _Process(double delta)
    {
        if (Engine.EditorHint) {
            QueueRedraw();
        }
    }

I know your question is old, but I’m leaving the answer here so that it might help other people with the same issue.

1 Like