making_plugins.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. .. _doc_making_plugins:
  2. Making plugins
  3. ==============
  4. About plugins
  5. ~~~~~~~~~~~~~
  6. A plugin is a great way to extend the editor with useful tools. It can be made
  7. entirely with GDScript and standard scenes, without even reloading the editor.
  8. Unlike modules, you don't need to create C++ code nor recompile the engine.
  9. While this makes plugins less powerful, there are still many things you can
  10. do with them. Note that a plugin is similar to any scene you can already
  11. make, except it is created using a script to add editor functionality.
  12. This tutorial will guide you through the creation of two plugins so
  13. you can understand how they work and be able to develop your own. The first
  14. is a custom node that you can add to any scene in the project, and the
  15. other is a custom dock added to the editor.
  16. Creating a plugin
  17. ~~~~~~~~~~~~~~~~~
  18. Before starting, create a new empty project wherever you want. This will serve
  19. as a base to develop and test the plugins.
  20. The first thing you need for the editor to identify a new plugin is to
  21. create two files: a ``plugin.cfg`` for configuration and a tool script with the
  22. functionality. Plugins have a standard path like ``addons/plugin_name`` inside
  23. the project folder. Godot provides a dialog for generating those files and
  24. placing them where they need to be.
  25. In the main toolbar, click the ``Project`` dropdown. Then click
  26. ``Project Settings...``. Go to the ``Plugins`` tab and then click
  27. on the ``Create`` button in the top-right.
  28. You will see the dialog appear, like so:
  29. .. image:: img/making_plugins-create_plugin_dialog.png
  30. The placeholder text in each field describes how it affects the plugin's
  31. creation of the files and the config file's values.
  32. To continue with the example, use the following values:
  33. .. tabs::
  34. .. code-tab:: ini GDScript
  35. Plugin Name: My Custom Node
  36. Subfolder: my_custom_node
  37. Description: A custom node made to extend the Godot Engine.
  38. Author: Your Name Here
  39. Version: 1.0.0
  40. Language: GDScript
  41. Script Name: custom_node.gd
  42. Activate now: No
  43. .. code-tab:: ini C#
  44. Plugin Name: My Custom Node
  45. Subfolder: my_custom_node
  46. Description: A custom node made to extend the Godot Engine.
  47. Author: Your Name Here
  48. Version: 1.0.0
  49. Language: C#
  50. Script Name: CustomNode.cs
  51. Activate now: No
  52. .. warning::
  53. Unchecking the ``Activate now?`` option in C# is always required because,
  54. like every other C# script, the EditorPlugin script needs to be compiled which
  55. requires building the project. After building the project the plugin can be
  56. enabled in the ``Plugins`` tab of ``Project Settings``.
  57. You should end up with a directory structure like this:
  58. .. image:: img/making_plugins-my_custom_mode_folder.png
  59. ``plugin.cfg`` is an INI file with metadata about your plugin.
  60. The name and description help people understand what it does.
  61. Your name helps you get properly credited for your work.
  62. The version number helps others know if they have an outdated version;
  63. if you are unsure on how to come up with the version number, check out `Semantic Versioning <https://semver.org/>`_.
  64. The main script file will instruct Godot what your plugin does in the editor
  65. once it is active.
  66. The script file
  67. ^^^^^^^^^^^^^^^
  68. Upon creation of the plugin, the dialog will automatically open the
  69. EditorPlugin script for you. The script has two requirements that you cannot
  70. change: it must be a ``@tool`` script, or else it will not load properly in the
  71. editor, and it must inherit from :ref:`class_EditorPlugin`.
  72. .. warning::
  73. In addition to the EditorPlugin script, any other GDScript that your plugin uses
  74. must *also* be a tool. Any GDScript without ``@tool`` imported into the editor
  75. will act like an empty file!
  76. It's important to deal with initialization and clean-up of resources.
  77. A good practice is to use the virtual function
  78. :ref:`_enter_tree() <class_Node_method__enter_tree>` to initialize your plugin and
  79. :ref:`_exit_tree() <class_Node_method__exit_tree>` to clean it up. Thankfully,
  80. the dialog generates these callbacks for you. Your script should look something
  81. like this:
  82. .. _doc_making_plugins_template_code:
  83. .. tabs::
  84. .. code-tab:: gdscript GDScript
  85. @tool
  86. extends EditorPlugin
  87. func _enter_tree():
  88. # Initialization of the plugin goes here.
  89. pass
  90. func _exit_tree():
  91. # Clean-up of the plugin goes here.
  92. pass
  93. .. code-tab:: csharp
  94. #if TOOLS
  95. using Godot;
  96. using System;
  97. [Tool]
  98. public class CustomNode : EditorPlugin
  99. {
  100. public override void _EnterTree()
  101. {
  102. // Initialization of the plugin goes here.
  103. }
  104. public override void _ExitTree()
  105. {
  106. // Clean-up of the plugin goes here.
  107. }
  108. }
  109. #endif
  110. This is a good template to use when creating new plugins.
  111. A custom node
  112. ~~~~~~~~~~~~~
  113. Sometimes you want a certain behavior in many nodes, such as a custom scene
  114. or control that can be reused. Instancing is helpful in a lot of cases, but
  115. sometimes it can be cumbersome, especially if you're using it in many
  116. projects. A good solution to this is to make a plugin that adds a node with a
  117. custom behavior.
  118. .. warning::
  119. Nodes added via an EditorPlugin are "CustomType" nodes. While they work
  120. with any scripting language, they have fewer features than
  121. :ref:`the Script Class system <doc_gdscript_basics_class_name>`. If you
  122. are writing GDScript or NativeScript, we recommend using Script Classes instead.
  123. To create a new node type, you can use the function
  124. :ref:`add_custom_type() <class_EditorPlugin_method_add_custom_type>` from the
  125. :ref:`class_EditorPlugin` class. This function can add new types to the editor
  126. (nodes or resources). However, before you can create the type, you need a script
  127. that will act as the logic for the type. While that script doesn't have to use
  128. the ``@tool`` keyword, it can be added so the script runs in the editor.
  129. For this tutorial, we'll create a button that prints a message when
  130. clicked. For that, we'll need a script that extends from
  131. :ref:`class_Button`. It could also extend
  132. :ref:`class_BaseButton` if you prefer:
  133. .. tabs::
  134. .. code-tab:: gdscript GDScript
  135. @tool
  136. extends Button
  137. func _enter_tree():
  138. pressed.connect(clicked)
  139. func clicked():
  140. print("You clicked me!")
  141. .. code-tab:: csharp
  142. using Godot;
  143. using System;
  144. [Tool]
  145. public class MyButton : Button
  146. {
  147. public override void _EnterTree()
  148. {
  149. Pressed += Clicked;
  150. }
  151. public void Clicked()
  152. {
  153. GD.Print("You clicked me!");
  154. }
  155. }
  156. That's it for our basic button. You can save this as ``my_button.gd`` inside the
  157. plugin folder. You'll also need a 16×16 icon to show in the scene tree. If you
  158. don't have one, you can grab the default one from the engine and save it in your
  159. `addons/my_custom_node` folder as `icon.png`, or use the default Godot logo
  160. (`preload("res://icon.png")`). You can also use SVG icons if desired.
  161. .. image:: img/making_plugins-custom_node_icon.png
  162. Now, we need to add it as a custom type so it shows on the **Create New Node**
  163. dialog. For that, change the ``custom_node.gd`` script to the following:
  164. .. tabs::
  165. .. code-tab:: gdscript GDScript
  166. @tool
  167. extends EditorPlugin
  168. func _enter_tree():
  169. # Initialization of the plugin goes here.
  170. # Add the new type with a name, a parent type, a script and an icon.
  171. add_custom_type("MyButton", "Button", preload("my_button.gd"), preload("icon.png"))
  172. func _exit_tree():
  173. # Clean-up of the plugin goes here.
  174. # Always remember to remove it from the engine when deactivated.
  175. remove_custom_type("MyButton")
  176. .. code-tab:: csharp
  177. #if TOOLS
  178. using Godot;
  179. using System;
  180. [Tool]
  181. public class CustomNode : EditorPlugin
  182. {
  183. public override void _EnterTree()
  184. {
  185. // Initialization of the plugin goes here.
  186. // Add the new type with a name, a parent type, a script and an icon.
  187. var script = GD.Load<Script>("MyButton.cs");
  188. var texture = GD.Load<Texture>("icon.png");
  189. AddCustomType("MyButton", "Button", script, texture);
  190. }
  191. public override void _ExitTree()
  192. {
  193. // Clean-up of the plugin goes here.
  194. // Always remember to remove it from the engine when deactivated.
  195. RemoveCustomType("MyButton");
  196. }
  197. }
  198. #endif
  199. With that done, the plugin should already be available in the plugin list in the
  200. **Project Settings**, so activate it as explained in `Checking the results`_.
  201. Then try it out by adding your new node:
  202. .. image:: img/making_plugins-custom_node_create.png
  203. When you add the node, you can see that it already has the script you created
  204. attached to it. Set a text to the button, save and run the scene. When you
  205. click the button, you can see some text in the console:
  206. .. image:: img/making_plugins-custom_node_console.png
  207. A custom dock
  208. ^^^^^^^^^^^^^
  209. Sometimes, you need to extend the editor and add tools that are always available.
  210. An easy way to do it is to add a new dock with a plugin. Docks are just scenes
  211. based on Control, so they are created in a way similar to usual GUI scenes.
  212. Creating a custom dock is done just like a custom node. Create a new
  213. ``plugin.cfg`` file in the ``addons/my_custom_dock`` folder, then
  214. add the following content to it:
  215. .. tabs::
  216. .. code-tab:: gdscript GDScript
  217. [plugin]
  218. name="My Custom Dock"
  219. description="A custom dock made so I can learn how to make plugins."
  220. author="Your Name Here"
  221. version="1.0"
  222. script="custom_dock.gd"
  223. .. code-tab:: csharp
  224. [plugin]
  225. name="My Custom Dock"
  226. description="A custom dock made so I can learn how to make plugins."
  227. author="Your Name Here"
  228. version="1.0"
  229. script="CustomDock.cs"
  230. Then create the script ``custom_dock.gd`` in the same folder. Fill it with the
  231. :ref:`template we've seen before <doc_making_plugins_template_code>` to get a
  232. good start.
  233. Since we're trying to add a new custom dock, we need to create the contents of
  234. the dock. This is nothing more than a standard Godot scene: just create
  235. a new scene in the editor then edit it.
  236. For an editor dock, the root node **must** be a :ref:`Control <class_Control>`
  237. or one of its child classes. For this tutorial, you can create a single button.
  238. The name of the root node will also be the name that appears on the dock tab,
  239. so be sure to give it a short and descriptive name.
  240. Also, don't forget to add some text to your button.
  241. .. image:: img/making_plugins-my_custom_dock_scene.png
  242. Save this scene as ``my_dock.tscn``. Now, we need to grab the scene we created
  243. then add it as a dock in the editor. For this, you can rely on the function
  244. :ref:`add_control_to_dock() <class_EditorPlugin_method_add_control_to_dock>` from the
  245. :ref:`EditorPlugin <class_EditorPlugin>` class.
  246. You need to select a dock position and define the control to add
  247. (which is the scene you just created). Don't forget to
  248. **remove the dock** when the plugin is deactivated.
  249. The script could look like this:
  250. .. tabs::
  251. .. code-tab:: gdscript GDScript
  252. @tool
  253. extends EditorPlugin
  254. # A class member to hold the dock during the plugin life cycle.
  255. var dock
  256. func _enter_tree():
  257. # Initialization of the plugin goes here.
  258. # Load the dock scene and instantiate it.
  259. dock = preload("res://addons/my_custom_dock/my_dock.tscn").instantiate()
  260. # Add the loaded scene to the docks.
  261. add_control_to_dock(DOCK_SLOT_LEFT_UL, dock)
  262. # Note that LEFT_UL means the left of the editor, upper-left dock.
  263. func _exit_tree():
  264. # Clean-up of the plugin goes here.
  265. # Remove the dock.
  266. remove_control_from_docks(dock)
  267. # Erase the control from the memory.
  268. dock.free()
  269. .. code-tab:: csharp
  270. #if TOOLS
  271. using Godot;
  272. using System;
  273. [Tool]
  274. public class CustomDock : EditorPlugin
  275. {
  276. Control dock;
  277. public override void _EnterTree()
  278. {
  279. dock = (Control)GD.Load<PackedScene>("addons/my_custom_dock/my_dock.tscn").Instantiate();
  280. AddControlToDock(DockSlot.LeftUl, dock);
  281. }
  282. public override void _ExitTree()
  283. {
  284. // Clean-up of the plugin goes here.
  285. // Remove the dock.
  286. RemoveControlFromDocks(dock);
  287. // Erase the control from the memory.
  288. dock.Free();
  289. }
  290. }
  291. #endif
  292. Note that, while the dock will initially appear at its specified position,
  293. the user can freely change its position and save the resulting layout.
  294. Checking the results
  295. ^^^^^^^^^^^^^^^^^^^^
  296. It's now time to check the results of your work. Open the **Project
  297. Settings** and click on the **Plugins** tab. Your plugin should be the only one
  298. on the list. If it is not showing, click on the **Update** button in the
  299. top-right corner.
  300. .. image:: img/making_plugins-project_settings.png
  301. You can see the plugin is inactive on the **Status** column; click on the status
  302. to select **Active**. The dock should become visible before you even close
  303. the settings window. You should now have a custom dock:
  304. .. image:: img/making_plugins-custom_dock.png
  305. Going beyond
  306. ~~~~~~~~~~~~
  307. Now that you've learned how to make basic plugins, you can extend the editor in
  308. several ways. Lots of functionality can be added to the editor with GDScript;
  309. it is a powerful way to create specialized editors without having to delve into
  310. C++ modules.
  311. You can make your own plugins to help yourself and share them in the
  312. `Asset Library <https://godotengine.org/asset-library/>`_ so that people
  313. can benefit from your work.
  314. .. _doc_making_plugins_autoload:
  315. Registering autoloads/singletons in plugins
  316. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  317. It is possible for editor plugins to automatically register
  318. :ref:`autoloads <doc_singletons_autoload>` when the plugin is enabled.
  319. This also includes unregistering the autoload when the plugin is disabled.
  320. This makes setting up plugins faster for users, as they no longer have to manually
  321. add autoloads to their project settings if your editor plugin requires the use of
  322. an autoload.
  323. Use the following code to register a singleton from an editor plugin:
  324. ::
  325. @tool
  326. extends EditorPlugin
  327. # Replace this value with a PascalCase autoload name, as per the GDScript style guide.
  328. const AUTOLOAD_NAME = "SomeAutoload"
  329. func _enter_tree():
  330. # The autoload can be a scene or script file.
  331. add_autoload_singleton(AUTOLOAD_NAME, "res://addons/my_addon/some_autoload.tscn")
  332. func _exit_tree():
  333. remove_autoload_singleton(AUTOLOAD_NAME)