Making plugins

About plugins

A plugin is a great way to extend the editor with useful tools. It can be made entirely with GDScript and standard scenes, without even reloading the editor. Unlike modules, you don’t need to create C++ code nor recompile the engine. While this makes plugins less powerful, there are still many things you can do with them. Note that a plugin is similar to any scene you can already make, except it is created using a script to add functionality.

This tutorial will guide you through the creation of two simple plugins so you can understand how they work and be able to develop your own. The first will be a custom node that you can add to any scene in the project and the other will be a custom dock added to the editor.

Creating a plugin

Before starting, create a new empty project wherever you want. This will serve as a base to develop and test the plugins.

The first thing you need to do is to create a new plugin the editor can understand as such. You need two files for that: plugin.cfg for the configuration and a custom GDScript with the functionality.

Plugins have a standard path like addons/plugin_name inside the project folder. For this example, create a folder my_custom_node inside addons. You should end up with a directory structure like this:

../../../_images/making_plugins-my_custom_mode_folder.png

Now, open the script editor, click the File menu, choose New TextFile, then navigate to the plugin folder and name the file plugin.cfg. Add the following structure to plugin.cfg:

GDScript

C#

  1. [plugin]
  2. name="My Custom Node"
  3. description="A custom node made to extend the Godot Engine."
  4. author="Your Name Here"
  5. version="1.0.0"
  6. script="custom_node.gd"
  1. [plugin]
  2. name="My Custom Node"
  3. description="A custom node made to extend the Godot Engine."
  4. author="Your Name Here"
  5. version="1.0.0"
  6. script="CustomNode.cs"

This is a simple INI file with metadata about your plugin. You need to set the name and description so people can understand what it does. Don’t forget to add your own name so you can be properly credited. Add a version number so people can see if they have an outdated version; if you are unsure on how to come up with the version number, check out Semantic Versioning. Finally, set the main script file to load when your plugin is active.

The script file

Open the script editor (F3) and create a new GDScript file called custom_node.gd inside the my_custom_node folder. This script is special and it has two requirements: it must be a tool script and it has to inherit from EditorPlugin.

It’s important to deal with initialization and clean-up of resources. A good practice is to use the virtual function _enter_tree() to initialize your plugin and _exit_tree() to clean it up. You can delete the default GDScript template from your file and replace it with the following structure:

GDScript

C#

  1. tool
  2. extends EditorPlugin
  3. func _enter_tree():
  4. # Initialization of the plugin goes here
  5. pass
  6. func _exit_tree():
  7. # Clean-up of the plugin goes here
  8. pass
  1. #if TOOLS
  2. using Godot;
  3. using System;
  4. [Tool]
  5. public class CustomNode : EditorPlugin
  6. {
  7. public override void _EnterTree()
  8. {
  9. // Initialization of the plugin goes here
  10. }
  11. public override void _ExitTree()
  12. {
  13. // Initialization of the plugin goes here
  14. }
  15. }
  16. #endif

This is a good template to use when creating new plugins.

A custom node

Sometimes you want a certain behavior in many nodes, such as a custom scene or control that can be reused. Instancing is helpful in a lot of cases, but sometimes it can be cumbersome, especially if you’re using it in many projects. A good solution to this is to make a plugin that adds a node with a custom behavior.

To create a new node type, you can use the function add_custom_type() from the EditorPlugin class. This function can add new types to the editor (nodes or resources). However, before you can create the type, you need a script that will act as the logic for the type. While that script doesn’t have to use the tool keyword, it can be added so the script runs in the editor.

For this tutorial, we’ll create a simple button that prints a message when clicked. For that, we’ll need a simple script that extends from Button. It could also extend BaseButton if you prefer:

GDScript

C#

  1. tool
  2. extends Button
  3. func _enter_tree():
  4. connect("pressed", self, "clicked")
  5. func clicked():
  6. print("You clicked me!")
  1. using Godot;
  2. using System;
  3. [Tool]
  4. public class MyButton : Button
  5. {
  6. public override void _EnterTree()
  7. {
  8. Connect("pressed", this, "clicked");
  9. }
  10. public void clicked()
  11. {
  12. GD.Print("You clicked me!");
  13. }
  14. }

That’s it for our basic button. You can save this as my_button.gd inside the plugin folder. You’ll also need a 16×16 icon to show in the scene tree. If you don’t have one, you can grab the default one from the engine and save it in your addons/my_custom_node folder as icon.png, or use the default Godot logo (preload(“res://icon.png”)). You can also use SVG icons if desired.

../../../_images/making_plugins-custom_node_icon.png

Now, we need to add it as a custom type so it shows on the Create New Node dialog. For that, change the custom_node.gd script to the following:

GDScript

C#

  1. tool
  2. extends EditorPlugin
  3. func _enter_tree():
  4. # Initialization of the plugin goes here
  5. # Add the new type with a name, a parent type, a script and an icon
  6. add_custom_type("MyButton", "Button", preload("my_button.gd"), preload("icon.png"))
  7. func _exit_tree():
  8. # Clean-up of the plugin goes here
  9. # Always remember to remove it from the engine when deactivated
  10. remove_custom_type("MyButton")
  1. #if TOOLS
  2. using Godot;
  3. using System;
  4. [Tool]
  5. public class CustomNode : EditorPlugin
  6. {
  7. public override void _EnterTree()
  8. {
  9. // Initialization of the plugin goes here
  10. // Add the new type with a name, a parent type, a script and an icon
  11. var script = GD.Load<Script>("addons/MyButton.cs");
  12. var texture = GD.Load<Texture>("icon.png");
  13. AddCustomType("MyButton", "Button", script, texture);
  14. }
  15. public override void _ExitTree()
  16. {
  17. // Clean-up of the plugin goes here
  18. // Always remember to remove it from the engine when deactivated
  19. RemoveCustomType("MyButton");
  20. }
  21. }
  22. #endif

With that done, the plugin should already be available in the plugin list in the Project Settings, so activate it as explained in Checking the results.

Then try it out by adding your new node:

../../../_images/making_plugins-custom_node_create.png

When you add the node, you can see that it already has the script you created attached to it. Set a text to the button, save and run the scene. When you click the button, you can see some text in the console:

../../../_images/making_plugins-custom_node_console.png

A custom dock

Sometimes, you need to extend the editor and add tools that are always available. An easy way to do it is to add a new dock with a plugin. Docks are just scenes based on Control, so they are created in a way similar to usual GUI scenes.

Creating a custom dock is done just like a custom node. Create a new plugin.cfg file in the addons/my_custom_dock folder, then add the following content to it:

GDScript

C#

  1. [plugin]
  2. name="My Custom Dock"
  3. description="A custom dock made so I can learn how to make plugins."
  4. author="Your Name Here"
  5. version="1.0"
  6. script="custom_dock.gd"
  1. [plugin]
  2. name="My Custom Dock"
  3. description="A custom dock made so I can learn how to make plugins."
  4. author="Your Name Here"
  5. version="1.0"
  6. script="CustomDock.cs"

Then create the script custom_dock.gd in the same folder. Fill it with the template we’ve seen before to get a good start.

Since we’re trying to add a new custom dock, we need to create the contents of the dock. This is nothing more than a standard Godot scene: just create a new scene in the editor then edit it.

For an editor dock, the root node must be a Control or one of its child classes. For this tutorial, you can create a single button. The name of the root node will also be the name that appears on the dock tab, so be sure to give it a short and descriptive name. Also, don’t forget to add some text to your button.

../../../_images/making_plugins-my_custom_dock_scene.png

Save this scene as my_dock.tscn. Now, we need to grab the scene we created then add it as a dock in the editor. For this, you can rely on the function add_control_to_dock() from the EditorPlugin class.

You need to select a dock position and define the control to add (which is the scene you just created). Don’t forget to remove the dock when the plugin is deactivated. The script could look like this:

GDScript

C#

  1. tool
  2. extends EditorPlugin
  3. # A class member to hold the dock during the plugin lifecycle
  4. var dock
  5. func _enter_tree():
  6. # Initialization of the plugin goes here
  7. # Load the dock scene and instance it
  8. dock = preload("res://addons/my_custom_dock/my_dock.tscn").instance()
  9. # Add the loaded scene to the docks
  10. add_control_to_dock(DOCK_SLOT_LEFT_UL, dock)
  11. # Note that LEFT_UL means the left of the editor, upper-left dock
  12. func _exit_tree():
  13. # Clean-up of the plugin goes here
  14. # Remove the dock
  15. remove_control_from_docks(dock)
  16. # Erase the control from the memory
  17. dock.free()
  1. #if TOOLS
  2. using Godot;
  3. using System;
  4. [Tool]
  5. public class CustomDock : EditorPlugin
  6. {
  7. Control dock;
  8. public override void _EnterTree()
  9. {
  10. dock = (Control)GD.Load<PackedScene>("addons/my_custom_dock/my_dock.tscn").Instance();
  11. AddControlToDock(DockSlot.LeftUl, dock);
  12. }
  13. public override void _ExitTree()
  14. {
  15. // Clean-up of the plugin goes here
  16. // Remove the dock
  17. RemoveControlFromDocks(dock);
  18. // Erase the control from the memory
  19. dock.Free();
  20. }
  21. }
  22. #endif

Note that, while the dock will initially appear at its specified position, the user can freely change its position and save the resulting layout.

Checking the results

It’s now time to check the results of your work. Open the Project Settings and click on the Plugins tab. Your plugin should be the only one on the list. If it is not showing, click on the Update button in the top-right corner.

../../../_images/making_plugins-project_settings.png

You can see the plugin is inactive on the Status column; click on the status to select Active. The dock should become visible before you even close the settings window. You should now have a custom dock:

../../../_images/making_plugins-custom_dock.png

Going beyond

Now that you’ve learned how to make basic plugins, you can extend the editor in several ways. Lots of functionality can be added to the editor with GDScript; it is a powerful way to create specialized editors without having to delve into C++ modules.

You can make your own plugins to help yourself and share them in the Asset Library so that people can benefit from your work.