What's the best way to colour certain meshes in the editor?

Godot Version

4.2.2

Question

I’m working on a project where I have many instances of a 3D scene dotted around each level. The scene has a Boolean value (currently called _isStarred) and I wanted a way to visualise in the editor which instances have it set to true. My solution was to use the Tool attribute, and create a property with a set method that only runs in the editor. The method sets the MaterialOverride of a MeshInstance3d node to a red material if the boolean is being set to true, and removes the material if being set to false. The code looks something like this:

[Tool]
public partial class SmallBox : RigidBody3D
{
    [Export]
    public bool IsStarred { 
        get => _isStarred;
        set
        {
            if (Engine.IsEditorHint())
            {
                var mesh = GetNode<MeshInstance3D>("MeshInstance3D");
                if (value)
                {
                    mesh.MaterialOverride = GD.Load<Material>("res://redMaterial.tres");
                }
                else
                {
                    mesh.MaterialOverride = null;
                }
            }
            _isStarred = value;
        }
    }

    private bool _isStarred = false;
}

This works, but there’s one small annoyance. There’s a lot of other code in the class (not included here because it’s not relevant), and it’s annoying to have to put if (!Engine.IsEditorHint()) before every bit of code. Also, I’m quite new to editor code so I might be missing a more efficient way of doing things. So I would like to ask if anybody has some advice or a better way of achieving the same thing. Thanks!

Depends on what is needed in-editor and what isn’t, you can disable some override functions with SetProcess(!Engine.IsEditorHint()) this will disable _Process if overriden

1 Like