From 9baaf4dc4225eb1369b4fefbd200f7f28ced2c61 Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Wed, 24 Aug 2022 19:33:01 +0200 Subject: [PATCH] Document registering an autoload when an editor plugin is enabled --- tutorials/plugins/editor/making_plugins.rst | 32 +++++++++++++++++++++ tutorials/scripting/singletons_autoload.rst | 6 ++++ 2 files changed, 38 insertions(+) diff --git a/tutorials/plugins/editor/making_plugins.rst b/tutorials/plugins/editor/making_plugins.rst index 8941aaef723..2170bcd6712 100644 --- a/tutorials/plugins/editor/making_plugins.rst +++ b/tutorials/plugins/editor/making_plugins.rst @@ -399,3 +399,35 @@ 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. + +.. _doc_making_plugins_autoload: + +Registering autoloads/singletons in plugins +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +It is possible for editor plugins to automatically register +:ref:`autoloads ` when the plugin is enabled. +This also includes unregistering the autoload when the plugin is disabled. + +This makes setting up plugins faster for users, as they no longer have to manually +add autoloads to their project settings if your editor plugin requires the use of +an autoload. + +Use the following code to register a singleton from an editor plugin: + +:: + + @tool + extends EditorPlugin + + # Replace this value with a PascalCase autoload name, as per the GDScript style guide. + const AUTOLOAD_NAME = "SomeAutoload" + + + func _enter_tree(): + # The autoload can be a scene or script file. + add_autoload_singleton(AUTOLOAD_NAME, "res://addons/my_addon/some_autoload.tscn") + + + func _exit_tree(): + remove_autoload_singleton(AUTOLOAD_NAME) diff --git a/tutorials/scripting/singletons_autoload.rst b/tutorials/scripting/singletons_autoload.rst index 13a5eedf724..61c317bf054 100644 --- a/tutorials/scripting/singletons_autoload.rst +++ b/tutorials/scripting/singletons_autoload.rst @@ -40,6 +40,12 @@ Autoloading nodes and scripts can give us these characteristics. Godot won't make an AutoLoad a "true" singleton as per the singleton design pattern. It may still be instanced more than once by the user if desired. +.. tip:: + + If you're creating an autoload as part of an editor plugin, consider + :ref:`registering it automatically in the Project Settings ` + when the plugin is enabled. + AutoLoad --------