Plugin Howtos: Make a plugin to edit selected Objects

Plugin Howtos

Godot’s interface showing the new plugin at the bottom

I’ll show you how to make a plugin that will go into the Bottom Panel. I’ll also show you how to associate file types (classes) with the plugin so that it opens when you double-click them (Like how the visual shader editor opens when you edit a visual shader resource.)

To start, make a folder called “addons” in your Godot root folder. Under that make another to hold your entire plugin; let’s call it “uselessplugin”. From now on you do all your work in here.

How to make a basic plugin

  1. Make a config file.
    • You can either make it manually, or go Project → Project Settings → Plugins → Create New Plugin
  2. Write the plugin’s code
    • gdscript in this case, or some other language (ymmv).
  3. Enable the plugin under Project → Project Settings → Plugins

The Config file

Here’s the one in my sample project:

[plugin]

name="Useless Plugin"
description="To show some plugin howtos"
author="Donn Ingle"
version="0.1"
script="useless_plugin.gd"

The script variable points to the main plugin code (in the same dir as the config file.)

The plugin code

Start a new script, in this case “useless_plugin.gd”. Here is the code with comments to explain it:

@tool # This is required because you are running code in the Editor
class_name UselessPlugin # This is the class name of your plugin
extends EditorPlugin # This is what makes your code an actual plugin

## The plugin goes into the Bottom Panel (alongside Shader Editor)

var main_view: Control # what our plugin will look like
var bottom_button : Button # the button that can toggle it on and off

# Plugin startup
func _enter_tree() -> void:
	if Engine.is_editor_hint():
		# main_view is a control; I'll use it right here, but it's
		# usually a scene, like:
		#   main_view = preload("some_scene.tscene").instantiate()
		#   (were some_scene's root-node is a Control.)
		main_view = Label.new()
		main_view.text = "I am blank"

		# Plonk it in the bottom panel
		bottom_button = add_control_to_bottom_panel(main_view, "Useless")

# Plugin teardown
func _exit_tree() -> void:
	if is_instance_valid(main_view):
		# Kick out the main_view
		remove_control_from_bottom_panel(main_view)
		# And free it
		main_view.queue_free()


func _get_plugin_name() -> String:
	return "Useless Plugin"

Not too bad! :smiling_face: Now it’s up to you to make your plugin do what you need.

How to edit (associate files) with your plugin

Your plugin can edit files of some type, Resources for example. First make sure you have something to edit, so let’s make a quick resource script and some resource files from it:

class_name SillyResource
extends Resource

## This is just a silly resource script which we'll use
## to make silly resource files for our plugin to open

@export var silly_name := "Dracfoola"

Then make one or two resources from that script: Right-click in the FileSystem and choose New Resource. Then filter for SillyResource in the chooser. Give your file a name and save it.

In each of the new resource files there is an exported var called silly_name. Give them different names in the Inspector (double-click each file to edit it) and be sure to save them.

Now, add the following two functions to the end of your plugin code:


# This func says "Hey, I'm cool with XYZ kind of objects"
func _handles(object: Object) -> bool:
	# We say we're good with SillyResources.
	return object is SillyResource


# NB: Because of _handles(), this _edit() func is enabled.
# It's what runs when you double-click (or open) a file of
# some kind.
func _edit(object: Object) -> void:
	# Oddly, we must once more test the object's class:
	if object is not SillyResource:
		return

	# Now to display something in the plugin!

	if is_instance_valid(main_view):
		# NB: This is what opens the panel properly
		make_bottom_panel_item_visible(main_view)

		# Now you make your main_view do whatever you need:
		main_view.text = "Opened SillyResource: %s" % [object.silly_name]

The _handles function enable the _edit function. Between them they let you filter what kind of object you want to deal with in your plugin.

And that’s it! You should now be able to double-click on the various silly resource files you just made and see the plugin open and display the silly name!

HTH

Demo project here: Godot Things / tutorials / Plugin Howtos · GitLab

PS - More tuts over on my wiki : Home · Wiki · Donn Ingle —Dbat / lessons-in-dev · GitLab

4 Likes