making_plugins.rst 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 not as powerful, there's still a lot of things you can
  10. do with them. Note that a plugin is not different from any scene you already
  11. can make, except that it is made via script to add functionality.
  12. This tutorial will guide you through the creation of two simple plugins so
  13. you can understand how they work and be able to develop your own. The first
  14. will be a custom node that you can add to any scene in the project and the
  15. other will be 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 base to develop and test the plugins.
  20. The first thing you need to do is to create a new plugin that the editor can
  21. understand as such. For that you need two files: ``plugin.cfg`` for the
  22. configuration and a custom GDScript with the functionality.
  23. Plugins have a standard path like ``addons/plugin_name`` inside the project
  24. folder. So create the folder ``my_custom_node`` inside ``addons``. So you'll
  25. have a directory structure like this:
  26. .. image:: img/making_plugins-my_custom_mode_folder.png
  27. To make the ``plugin.cfg`` file, open your favorite text editor with a blank
  28. file. Godot is not able (yet) to open text files besides scripts, so this must
  29. be done in an external editor. Add the following structure to your
  30. ``plugin.cfg``::
  31. [plugin]
  32. name="My Custom Node"
  33. description="A custom node made to extend the Godot Engine."
  34. author="Your Name Here"
  35. version="1.0"
  36. script="custom_node.gd"
  37. This is a simple ``ini`` file with metadata about your plugin. You need to set
  38. up the name and description so users can understand what it does. Add your
  39. own name so you can be properly credited. A version number is useful so users can see if
  40. they have an outdated version (if you are unsure on how to come up with
  41. the version number, check `SemVer <https://semver.org/>`_). And finally a main
  42. script file to load when your plugin is active.
  43. The script file
  44. ^^^^^^^^^^^^^^^
  45. Open the script editor (F3) and create a new GDScript file called
  46. ``custom_node.gd`` inside the ``my_custom_node`` folder. This script is special
  47. and it has two requirements: it must be a ``tool`` script and it has to
  48. inherit from :ref:`class_EditorPlugin`.
  49. It's important to deal with initialization and clean-up of resources. So a good
  50. practice is to use the virtual function
  51. :ref:`_enter_tree() <class_Node__enter_tree>` to initialize your plugin and
  52. :ref:`_exit_tree() <class_Node__exit_tree>` to clean it up. You can delete the
  53. default GDScript template from your file and replace it with the following
  54. structure:
  55. .. _doc_making_plugins_template_code:
  56. .. code-block:: python
  57. tool
  58. extends EditorPlugin
  59. func _enter_tree():
  60. # Initialization of the plugin goes here
  61. pass
  62. func _exit_tree():
  63. # Clean-up of the plugin goes here
  64. pass
  65. This is a good template to use when devising new plugins.
  66. A custom node
  67. ~~~~~~~~~~~~~~~~~~~~
  68. Sometimes you want a certain behavior in many nodes. Maybe a custom scene
  69. or control that can be reused. Instancing is helpful in a lot of cases but
  70. sometimes it can be cumbersome, especially if you're using it between many
  71. projects. A good solution to this is to make a plugin that adds a node with a
  72. custom behavior.
  73. To create a new node type, you can avail of the function
  74. :ref:`add_custom_type() <class_EditorPlugin_add_custom_type>` from the
  75. :ref:`class_EditorPlugin` class. This function can add new types to the editor,
  76. be it nodes or resources. But before you can create the type you need a script
  77. that will act as the logic for the type. While such script doesn't need to have
  78. the ``tool`` keyword, it is interesting to use it so the user can see it acting
  79. on the editor.
  80. For this tutorial, we'll create a simple button that prints a message when
  81. clicked. And for that we'll need a simple script that extends from
  82. :ref:`class_Button`. It could also extend
  83. :ref:`class_BaseButton` if you prefer::
  84. tool
  85. extends Button
  86. func _enter_tree():
  87. connect("pressed", self, "clicked")
  88. func clicked():
  89. print("You clicked me!")
  90. That's it for our basic button. You can save this as ``button.gd`` inside the
  91. plugin folder. You'll also need a 16x16 icon to show in the scene tree. If you
  92. don't have one, you can grab the default one from the engine:
  93. .. image:: img/making_plugins-custom_node_icon.png
  94. Now we need to add it as a custom type so it shows on the Create New Node
  95. dialog. For that, change the ``custom_node.gd`` script to the following::
  96. tool
  97. extends EditorPlugin
  98. func _enter_tree():
  99. # Initialization of the plugin goes here
  100. # Add the new type with a name, a parent type, a script and an icon
  101. add_custom_type("MyButton", "Button", preload("button.gd"), preload("icon.png"))
  102. func _exit_tree():
  103. # Clean-up of the plugin goes here
  104. # Always remember to remove it from the engine when deactivated
  105. remove_custom_type("MyButton")
  106. With that done, the plugin should already be available in the plugin list at
  107. Project Settings. So activate it and try to add a new node to see the result:
  108. .. image:: img/making_plugins-custom_node_create.png
  109. When you add the node, you can see that it already have the script you created
  110. attached to it. Set a text to the button, save and run the scene. When you
  111. click the button, you can see a text in the console:
  112. .. image:: img/making_plugins-custom_node_console.png
  113. A custom dock
  114. ^^^^^^^^^^^^^
  115. Maybe you need to extend the editor and add tools that are always available.
  116. An easy way to do it is to add a new dock with a plugin. Docks are just scenes
  117. based on control, so how to create them is not far beyond your knowledge.
  118. The way to start this plugin is similar to the custom node. So create a new
  119. ``plugin.cfg`` file in the ``addons/my_custom_dock`` folder. And then with
  120. your favorite text editor add the following content to it::
  121. [plugin]
  122. name="My Custom Dock"
  123. description="A custom dock made so I can learn how to make plugins."
  124. author="Your Name Here"
  125. version="1.0"
  126. script="custom_dock.gd"
  127. Then create the script ``custom_dock.gd`` in the same folder. Fill with the
  128. :ref:`template we've seen before <doc_making_plugins_template_code>` to get a
  129. good start.
  130. Since we're trying to add a new custom dock, we need to create the contents of
  131. such dock. This is nothing more than a standard Godot scene. So you can create
  132. a new scene in the editor and start creating it.
  133. For an editor dock, it is mandatory that the root of the scene is a
  134. :ref:`Control <class_Control>` or one of its child classes. For this tutorial,
  135. you can make a single button. The name of the root node will also be the name
  136. that appears on the dock tab, so be sure to put a descriptive but short one.
  137. Don't forget to add a text to your button.
  138. .. image:: img/making_plugins-my_custom_dock_scene.png
  139. Save this scene as ``my_dock.tscn``.
  140. Now you need to grab that scene you created and add it as a dock in the
  141. editor. For this you can rely on the function
  142. :ref:`add_control_to_dock() <class_EditorPlugin_add_control_to_dock>` from the
  143. :ref:`EditorPlugin <class_EditorPlugin>` class.
  144. The code is straightforward, you need to select a dock position to
  145. add it and have a control to add (which is the scene you just created). It is
  146. also important that you remember to **remove the dock** when the plugin is
  147. deactivated. The code can be like this::
  148. tool
  149. extends EditorPlugin
  150. var dock # A class member to hold the dock during the plugin lifecycle
  151. func _enter_tree():
  152. # Initialization of the plugin goes here
  153. # First load the dock scene and instance it:
  154. dock = preload("res://addons/my_custom_dock/my_dock.tscn").instance()
  155. # Add the loaded scene to the docks:
  156. add_control_to_dock(DOCK_SLOT_LEFT_UL, dock)
  157. # Note that LEFT_UL means the left of the editor, upper-left dock
  158. func _exit_tree():
  159. # Clean-up of the plugin goes here
  160. # Remove the scene from the docks:
  161. remove_control_from_docks(dock) # Remove the dock
  162. dock.free() # Erase the control from the memory
  163. While the dock position is chosen when adding it, the user is free to move it
  164. and save the layout with the dock in any position.
  165. Checking the results
  166. ^^^^^^^^^^^^^^^^^^^^
  167. Now it is the moment to check the results of your work. Open the *Project
  168. Settings* and click on the *Plugins* tab. Your plugin should be the only on
  169. the list. If it is not showing, click on the *Update* button at the top right
  170. corner.
  171. .. image:: img/making_plugins-project_settings.png
  172. At the *Status* column, you can see that the plugin is inactive. So you
  173. need to click on the status to select *Active*. The dock should be immediately
  174. visible, even before you close the settings window. You should
  175. have a custom dock:
  176. .. image:: img/making_plugins-custom_dock.png
  177. Going beyond
  178. ~~~~~~~~~~~~
  179. Now that you learned how to make basic plugins, you can extend the editor in
  180. many nice ways. Many functions can be added to editor on the fly with GDScript,
  181. it is a powerful way to create special editors without having to delve into C++
  182. modules.
  183. You can make your own plugins to help you and also share them in Godot's Asset
  184. Library so many people can benefit of your work.