Work in progress

The content of this page was not yet updated for Godot 4.3 and may be outdated. If you know how to improve this page or you can confirm that it’s up to date, feel free to open a pull request.

Making main screen plugins

What this tutorial covers

Main screen plugins allow you to create new UIs in the central part of the editor, which appear next to the “2D”, “3D”, “Script”, and “AssetLib” buttons. Such editor plugins are referred as “Main screen plugins”.

This tutorial leads you through the creation of a basic main screen plugin. For the sake of simplicity, our main screen plugin will contain a single button that prints text to the console.

Initializing the plugin

First create a new plugin from the Plugins menu. For this tutorial, we’ll put it in a folder called main_screen, but you can use any name you’d like.

The plugin script will come with _enter_tree() and _exit_tree() methods, but for a main screen plugin we need to add a few extra methods. Add five extra methods such that the script looks like this:

GDScriptC#

  1. @tool
  2. extends EditorPlugin
  3. func _enter_tree():
  4. pass
  5. func _exit_tree():
  6. pass
  7. func _has_main_screen():
  8. return true
  9. func _make_visible(visible):
  10. pass
  11. func _get_plugin_name():
  12. return "Main Screen Plugin"
  13. func _get_plugin_icon():
  14. return EditorInterface.get_editor_theme().get_icon("Node", "EditorIcons")
  1. #if TOOLS
  2. using Godot;
  3. [Tool]
  4. public partial class MainScreenPlugin : EditorPlugin
  5. {
  6. public override void _EnterTree()
  7. {
  8. }
  9. public override void _ExitTree()
  10. {
  11. }
  12. public override bool _HasMainScreen()
  13. {
  14. return true;
  15. }
  16. public override void _MakeVisible(bool visible)
  17. {
  18. }
  19. public override string _GetPluginName()
  20. {
  21. return "Main Screen Plugin";
  22. }
  23. public override Texture2D _GetPluginIcon()
  24. {
  25. return EditorInterface.GetEditorTheme().GetIcon("Node", "EditorIcons");
  26. }
  27. }
  28. #endif

The important part in this script is the _has_main_screen() function, which is overloaded so it returns true. This function is automatically called by the editor on plugin activation, to tell it that this plugin adds a new center view to the editor. For now, we’ll leave this script as-is and we’ll come back to it later.

Main screen scene

Create a new scene with a root node derived from Control (for this example plugin, we’ll make the root node a CenterContainer). Select this root node, and in the viewport, click the Layout menu and select Full Rect. You also need to enable the Expand vertical size flag in the inspector. The panel now uses all the space available in the main viewport.

Next, let’s add a button to our example main screen plugin. Add a Button node, and set the text to “Print Hello” or similar. Add a script to the button like this:

GDScriptC#

  1. @tool
  2. extends Button
  3. func _on_print_hello_pressed():
  4. print("Hello from the main screen plugin!")
  1. using Godot;
  2. [Tool]
  3. public partial class PrintHello : Button
  4. {
  5. private void OnPrintHelloPressed()
  6. {
  7. GD.Print("Hello from the main screen plugin!");
  8. }
  9. }

Then connect the “pressed” signal to itself. If you need help with signals, see the Using signals article.

We are done with the main screen panel. Save the scene as main_panel.tscn.

Update the plugin script

We need to update the main_screen_plugin.gd script so the plugin instances our main panel scene and places it where it needs to be. Here is the full plugin script:

GDScriptC#

  1. @tool
  2. extends EditorPlugin
  3. const MainPanel = preload("res://addons/main_screen/main_panel.tscn")
  4. var main_panel_instance
  5. func _enter_tree():
  6. main_panel_instance = MainPanel.instantiate()
  7. # Add the main panel to the editor's main viewport.
  8. EditorInterface.get_editor_main_screen().add_child(main_panel_instance)
  9. # Hide the main panel. Very much required.
  10. _make_visible(false)
  11. func _exit_tree():
  12. if main_panel_instance:
  13. main_panel_instance.queue_free()
  14. func _has_main_screen():
  15. return true
  16. func _make_visible(visible):
  17. if main_panel_instance:
  18. main_panel_instance.visible = visible
  19. func _get_plugin_name():
  20. return "Main Screen Plugin"
  21. func _get_plugin_icon():
  22. # Must return some kind of Texture for the icon.
  23. return EditorInterface.get_editor_theme().get_icon("Node", "EditorIcons")
  1. #if TOOLS
  2. using Godot;
  3. [Tool]
  4. public partial class MainScreenPlugin : EditorPlugin
  5. {
  6. PackedScene MainPanel = ResourceLoader.Load<PackedScene>("res://addons/main_screen/main_panel.tscn");
  7. Control MainPanelInstance;
  8. public override void _EnterTree()
  9. {
  10. MainPanelInstance = (Control)MainPanel.Instantiate();
  11. // Add the main panel to the editor's main viewport.
  12. EditorInterface.GetEditorMainScreen().AddChild(MainPanelInstance);
  13. // Hide the main panel. Very much required.
  14. _MakeVisible(false);
  15. }
  16. public override void _ExitTree()
  17. {
  18. if (MainPanelInstance != null)
  19. {
  20. MainPanelInstance.QueueFree();
  21. }
  22. }
  23. public override bool _HasMainScreen()
  24. {
  25. return true;
  26. }
  27. public override void _MakeVisible(bool visible)
  28. {
  29. if (MainPanelInstance != null)
  30. {
  31. MainPanelInstance.Visible = visible;
  32. }
  33. }
  34. public override string _GetPluginName()
  35. {
  36. return "Main Screen Plugin";
  37. }
  38. public override Texture2D _GetPluginIcon()
  39. {
  40. // Must return some kind of Texture for the icon.
  41. return EditorInterface.GetEditorTheme().GetIcon("Node", "EditorIcons");
  42. }
  43. }
  44. #endif

A couple of specific lines were added. MainPanel is a constant that holds a reference to the scene, and we instance it into main_panel_instance.

The _enter_tree() function is called before _ready(). This is where we instance the main panel scene, and add them as children of specific parts of the editor. We use EditorInterface.get_editor_main_screen() to obtain the main editor screen and add our main panel instance as a child to it. We call the _make_visible(false) function to hide the main panel so it doesn’t compete for space when first activating the plugin.

The _exit_tree() function is called when the plugin is deactivated. If the main screen still exists, we call queue_free() to free the instance and remove it from memory.

The _make_visible() function is overridden to hide or show the main panel as needed. This function is automatically called by the editor when the user clicks on the main viewport buttons at the top of the editor.

The _get_plugin_name() and _get_plugin_icon() functions control the displayed name and icon for the plugin’s main viewport button.

Another function you can add is the handles() function, which allows you to handle a node type, automatically focusing the main screen when the type is selected. This is similar to how clicking on a 3D node will automatically switch to the 3D viewport.

Try the plugin

Activate the plugin in the Project Settings. You’ll observe a new button next to 2D, 3D, Script above the main viewport. Clicking it will take you to your new main screen plugin, and the button in the middle will print text.

If you would like to try a finished version of this plugin, check out the plugin demos here: https://github.com/godotengine/godot-demo-projects/tree/master/plugins

If you would like to see a more complete example of what main screen plugins are capable of, check out the 2.5D demo projects here: https://github.com/godotengine/godot-demo-projects/tree/master/misc/2.5d


User-contributed notes

Please read the User-contributed notes policy before submitting a comment.

Previous Next