Inspector plugins

The inspector dock allows you to create custom widgets to edit properties through plugins. This can be beneficial when working with custom datatypes and resources, although you can use the feature to change the inspector widgets for built-in types. You can design custom controls for specific properties, entire objects, and even separate controls associated with particular datatypes.

This guide explains how to use the EditorInspectorPlugin and EditorProperty classes to create a custom interface for integers, replacing the default behavior with a button that generates random values between 0 and 99.

../../../_images/inspector_plugin_example.png

The default behavior on the left and the end result on the right.

Setting up your plugin

Create a new empty plugin to get started.

See also

See Making plugins guide to set up your new plugin.

Let’s assume you’ve called your plugin folder my_inspector_plugin. If so, you should end up with a new addons/my_inspector_plugin folder that contains two files: plugin.cfg and plugin.gd.

As before, plugin.gd is a script extending EditorPlugin and you need to introduce new code for its _enter_tree and _exit_tree methods. To set up your inspector plugin, you must load its script, then create and add the instance by calling add_inspector_plugin(). If the plugin is disabled, you should remove the instance you have added by calling remove_inspector_plugin().

Note

Here, you are loading a script and not a packed scene. Therefore you should use new() instead of instance().

GDScript   C#

  1. # plugin.gd
  2. tool
  3. extends EditorPlugin
  4. var plugin
  5. func _enter_tree():
  6. plugin = preload("res://addons/my_inspector_plugin/MyInspectorPlugin.gd").new()
  7. add_inspector_plugin(plugin)
  8. func _exit_tree():
  9. remove_inspector_plugin(plugin)
  1. // Plugin.cs
  2. #if TOOLS
  3. using Godot;
  4. [Tool]
  5. public class Plugin : EditorPlugin
  6. {
  7. private MyInspectorPlugin _plugin;
  8. public override void _EnterTree()
  9. {
  10. _plugin = new MyInspectorPlugin();
  11. AddInspectorPlugin(_plugin);
  12. }
  13. public override void _ExitTree()
  14. {
  15. RemoveInspectorPlugin(_plugin);
  16. }
  17. }
  18. #endif

Interacting with the inspector

To interact with the inspector dock, your MyInspectorPlugin.gd script must extend the EditorInspectorPlugin class. This class provides several virtual methods that affect how the inspector handles properties.

To have any effect at all, the script must implement the can_handle() method. This function is called for each edited Object and must return true if this plugin should handle the object or its properties.

Note

This includes any Resource attached to the object.

You can implement four other methods to add controls to the inspector at specific positions. The parse_begin() and parse_end() methods are called only once at the beginning and the end of parsing for each object, respectively. They can add controls at the top or bottom of the inspector layout by calling add_custom_control().

As the editor parses the object, it calls the parse_category() and parse_property() methods. There, in addition to add_custom_control(), you can call both add_property_editor() and add_property_editor_for_multiple_properties(). Use these last two methods to specifically add EditorProperty-based controls.

GDScript   C#

  1. # MyInspectorPlugin.gd
  2. extends EditorInspectorPlugin
  3. var RandomIntEditor = preload("res://addons/my_inspector_plugin/RandomIntEditor.gd")
  4. func can_handle(object):
  5. # We support all objects in this example.
  6. return true
  7. func parse_property(object, type, path, hint, hint_text, usage):
  8. # We handle properties of type integer.
  9. if type == TYPE_INT:
  10. # Create an instance of the custom property editor and register
  11. # it to a specific property path.
  12. add_property_editor(path, RandomIntEditor.new())
  13. # Inform the editor to remove the default property editor for
  14. # this property type.
  15. return true
  16. else:
  17. return false
  1. // MyInspectorPlugin.cs
  2. #if TOOLS
  3. using Godot;
  4. public class MyInspectorPlugin : EditorInspectorPlugin
  5. {
  6. public override bool CanHandle(Object @object)
  7. {
  8. // We support all objects in this example.
  9. return true;
  10. }
  11. public override bool ParseProperty(Object @object, int type, string path, int hint, string hintText, int usage)
  12. {
  13. // We handle properties of type integer.
  14. if (type == (int)Variant.Type.Int)
  15. {
  16. // Create an instance of the custom property editor and register
  17. // it to a specific property path.
  18. AddPropertyEditor(path, new RandomIntEditor());
  19. // Inform the editor to remove the default property editor for
  20. // this property type.
  21. return true;
  22. }
  23. return false;
  24. }
  25. }
  26. #endif

Adding an interface to edit properties

The EditorProperty class is a special type of Control that can interact with the inspector dock’s edited objects. It doesn’t display anything but can house any other control nodes, including complex scenes.

There are three essential parts to the script extending EditorProperty:

  1. You must define the _init() method to set up the control nodes’ structure.

  2. You should implement the update_property() to handle changes to the data from the outside.

  3. A signal must be emitted at some point to inform the inspector that the control has changed the property using emit_changed.

You can display your custom widget in two ways. Use just the default add_child() method to display it to the right of the property name, and use add_child() followed by set_bottom_editor() to position it below the name.

GDScript   C#

  1. # RandomIntEditor.gd
  2. extends EditorProperty
  3. # The main control for editing the property.
  4. var property_control = Button.new()
  5. # An internal value of the property.
  6. var current_value = 0
  7. # A guard against internal changes when the property is updated.
  8. var updating = false
  9. func _init():
  10. # Add the control as a direct child of EditorProperty node.
  11. add_child(property_control)
  12. # Make sure the control is able to retain the focus.
  13. add_focusable(property_control)
  14. # Setup the initial state and connect to the signal to track changes.
  15. refresh_control_text()
  16. property_control.connect("pressed", self, "_on_button_pressed")
  17. func _on_button_pressed():
  18. # Ignore the signal if the property is currently being updated.
  19. if (updating):
  20. return
  21. # Generate a new random integer between 0 and 99.
  22. current_value = randi() % 100
  23. refresh_control_text()
  24. emit_changed(get_edited_property(), current_value)
  25. func update_property():
  26. # Read the current value from the property.
  27. var new_value = get_edited_object()[get_edited_property()]
  28. if (new_value == current_value):
  29. return
  30. # Update the control with the new value.
  31. updating = true
  32. current_value = new_value
  33. refresh_control_text()
  34. updating = false
  35. func refresh_control_text():
  36. property_control.text = "Value: " + str(current_value)
  1. // RandomIntEditor.cs
  2. #if TOOLS
  3. using Godot;
  4. public class RandomIntEditor : EditorProperty
  5. {
  6. // The main control for editing the property.
  7. private Button _propertyControl = new Button();
  8. // An internal value of the property.
  9. private int _currentValue = 0;
  10. // A guard against internal changes when the property is updated.
  11. private bool _updating = false;
  12. public RandomIntEditor()
  13. {
  14. // Add the control as a direct child of EditorProperty node.
  15. AddChild(_propertyControl);
  16. // Make sure the control is able to retain the focus.
  17. AddFocusable(_propertyControl);
  18. // Setup the initial state and connect to the signal to track changes.
  19. RefreshControlText();
  20. _propertyControl.Connect("pressed", this, nameof(OnButtonPressed));
  21. }
  22. private void OnButtonPressed()
  23. {
  24. // Ignore the signal if the property is currently being updated.
  25. if (_updating)
  26. {
  27. return;
  28. }
  29. // Generate a new random integer between 0 and 99.
  30. _currentValue = (int)GD.Randi() % 100;
  31. RefreshControlText();
  32. EmitChanged(GetEditedProperty(), _currentValue);
  33. }
  34. public override void UpdateProperty()
  35. {
  36. // Read the current value from the property.
  37. var newValue = (int)GetEditedObject().Get(GetEditedProperty());
  38. if (newValue == _currentValue)
  39. {
  40. return;
  41. }
  42. // Update the control with the new value.
  43. _updating = true;
  44. _currentValue = newValue;
  45. RefreshControlText();
  46. _updating = false;
  47. }
  48. private void RefreshControlText()
  49. {
  50. _propertyControl.Text = $"Value: {_currentValue}";
  51. }
  52. }
  53. #endif

Using the example code above you should be able to make a custom widget that replaces the default SpinBox control for integers with a Button that generates random values.