[Discussion/Tutorial] How to elegantly write your plugin: Writing Inspector attributes

Recently, the official release introduced custom actions and custom plugins. I immediately got started and began writing plugins for my own projects. However, I quickly encountered an issue: in different scenarios, I often needed various parameters, which made the setup look quite cluttered. While using @export_group() to group exports might help somewhat, the editor felt cumbersome when frequently expanding and collapsing tabs. At that point, I noticed that many official actions display different parameters depending on the selected option, so I started learning how to achieve this effect.

Let’s assume we are writing a dialogue plugin.
First, you need to write the following at the beginning of your code:

@tool

Of course, this is the foundation for plugins. Next, we need to declare a variable. We can start with a boolean value:

@export var is_dialogue : bool= false

OK, now we have a control switch. Next, we need the properties inside it:

@export_multiline var text : String

Let’s open the plugin and see the current state:


Great, all our properties are displayed above. Now for the key part: let’s declare another variable:

var contorl_dialogue : bool

The type of this variable is the same as our initial is_dialogue.

Then, modify the initial variable:

@export var is_dialogue : bool= false:
	set(v):
		contorl_dialogue = v
		notify_property_list_changed()
	get:
		return contorl_dialogue

Then add the method:

func _validate_property(prop: Dictionary) -> void:
	match prop.name:
		"character_name":
			if is_dialogue == false:
				prop.usage &= ~PROPERTY_USAGE_EDITOR
		"text":
			if is_dialogue == false:
				prop.usage &= ~PROPERTY_USAGE_EDITOR

Now let’s see the result:



Not bad, this fulfills our requirement. This code block only appears when the option is selected. Of course, you can replace it with an enum:

enum Type {
	dog,
	cat,
	cow
}

Combined with the declarations and conditional checks mentioned earlier, this achieves the same effect. Used properly, it can significantly improve the readability and cleanliness of your plugin.