From a570a90c32def53e97c70a949a15f36d10baa155 Mon Sep 17 00:00:00 2001 From: Thibault Jouannic Date: Wed, 12 Feb 2025 14:16:57 +0100 Subject: [PATCH 1/3] Add a clarification about named class registry --- tutorials/scripting/gdscript/gdscript_basics.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tutorials/scripting/gdscript/gdscript_basics.rst b/tutorials/scripting/gdscript/gdscript_basics.rst index 60fa034dfc9..ad5d7b8ee77 100644 --- a/tutorials/scripting/gdscript/gdscript_basics.rst +++ b/tutorials/scripting/gdscript/gdscript_basics.rst @@ -1951,6 +1951,20 @@ If you want to use ``extends`` too, you can keep both on the same line:: class_name MyNode extends Node +Named classes are globally registered, hence they become available in other scripts without the need to `load` or `preload` them. For example:: + + # In a file 'game.gd'. + + # If you uncomment the next line, you will trigger a warning: + # The variable "Character" has the same name as a global class defined in "character.gd". + # var Character = load("character.gd") + + var player: Character + + func new_game() -> void: + player = Character.new() + + .. note:: Godot initializes non-static variables every time you create an instance, From 297b8a195a0d39d184e9a3b5a80c4ae81a75e92c Mon Sep 17 00:00:00 2001 From: "Thibault J." Date: Thu, 13 Feb 2025 09:03:38 +0100 Subject: [PATCH 2/3] Split the code example into two parts Commit PR sugestion Co-authored-by: tetrapod <145553014+tetrapod00@users.noreply.github.com> --- .../scripting/gdscript/gdscript_basics.rst | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/tutorials/scripting/gdscript/gdscript_basics.rst b/tutorials/scripting/gdscript/gdscript_basics.rst index ad5d7b8ee77..b9c0c4a8232 100644 --- a/tutorials/scripting/gdscript/gdscript_basics.rst +++ b/tutorials/scripting/gdscript/gdscript_basics.rst @@ -1951,19 +1951,24 @@ If you want to use ``extends`` too, you can keep both on the same line:: class_name MyNode extends Node -Named classes are globally registered, hence they become available in other scripts without the need to `load` or `preload` them. For example:: +Named classes are globally registered, which means they become available to use +in other scripts without the need to ``load`` or ``preload`` them: - # In a file 'game.gd'. +.. code-block:: gdscript - # If you uncomment the next line, you will trigger a warning: - # The variable "Character" has the same name as a global class defined in "character.gd". - # var Character = load("character.gd") + var player - var player: Character - - func new_game() -> void: + func _ready(): player = Character.new() +Using the name of a named class as a variable name will cause a +:ref:`SHADOWED_GLOBAL_IDENTIFIER ` +warning. For example, using ``Character`` as a variable name will shadow the +``Character`` class you just defined, and cause a warning: + +.. code-block:: gdscript + + var Character .. note:: From 3fdc4009188f7e2796c5a843db8c08fffece58fd Mon Sep 17 00:00:00 2001 From: tetrapod <145553014+tetrapod00@users.noreply.github.com> Date: Sat, 15 Feb 2025 10:53:26 -0800 Subject: [PATCH 3/3] Remove unrelated paragraph on shadowing --- tutorials/scripting/gdscript/gdscript_basics.rst | 9 --------- 1 file changed, 9 deletions(-) diff --git a/tutorials/scripting/gdscript/gdscript_basics.rst b/tutorials/scripting/gdscript/gdscript_basics.rst index b9c0c4a8232..c5488a27605 100644 --- a/tutorials/scripting/gdscript/gdscript_basics.rst +++ b/tutorials/scripting/gdscript/gdscript_basics.rst @@ -1961,15 +1961,6 @@ in other scripts without the need to ``load`` or ``preload`` them: func _ready(): player = Character.new() -Using the name of a named class as a variable name will cause a -:ref:`SHADOWED_GLOBAL_IDENTIFIER ` -warning. For example, using ``Character`` as a variable name will shadow the -``Character`` class you just defined, and cause a warning: - -.. code-block:: gdscript - - var Character - .. note:: Godot initializes non-static variables every time you create an instance,