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().

# plugin.gd
@tool
extends EditorPlugin

var plugin


func _enter_tree():
    plugin = preload("res://addons/my_inspector_plugin/my_inspector_plugin.gd").new()
    add_inspector_plugin(plugin)


func _exit_tree():
    remove_inspector_plugin(plugin)

Interacting with the inspector

To interact with the inspector dock, your my_inspector_plugin.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.

# my_inspector_plugin.gd
extends EditorInspectorPlugin

var RandomIntEditor = preload("res://addons/my_inspector_plugin/random_int_editor.gd")


func _can_handle(object):
    # We support all objects in this example.
    return true


func _parse_property(object, type, path, hint, hint_text, usage):
    # We handle properties of type integer.
    if type == TYPE_INT:
        # Create an instance of the custom property editor and register
        # it to a specific property path.
        add_property_editor(path, RandomIntEditor.new())
        # Inform the editor to remove the default property editor for
        # this property type.
        return true
    else:
        return false