scene_unique_nodes.rst 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. .. _doc_scene_unique_nodes:
  2. Scene Unique Nodes
  3. ==================
  4. Introduction
  5. ------------
  6. Using ``get_node()`` to reference nodes from a script can sometimes be fragile.
  7. If you move a button in a UI scene from one panel to another, the button's node
  8. path changes, and if a script uses ``get_node()`` with a hard-coded node path,
  9. the script will not be able to find the button anymore.
  10. In situations like this, the node can be turned into a scene
  11. unique node to avoid having to update the script every time
  12. the node's path is changed.
  13. Creation and usage
  14. ------------------
  15. There are two ways to create a scene unique node.
  16. In the Scene tree dock, right-click on a node and select
  17. **Access as Unique Name** in the context menu.
  18. .. image:: img/unique_name.webp
  19. After selecting the option, the node will now have a percent symbol (**%**) next
  20. to its name in the scene tree:
  21. .. image:: img/percent.webp
  22. You can also do this while renaming the node by adding "%" to the beginning of the name.
  23. Once you confirm, the percent symbol will appear next to its name.
  24. You can now use the node in your script. For example, you can reference it with
  25. a ``get_node()`` method call by typing the % symbol, followed by the node's
  26. name:
  27. .. tabs::
  28. .. code-tab:: gdscript GDScript
  29. get_node("%RedButton").text = "Hello"
  30. %RedButton.text = "Hello" # Shorter syntax
  31. .. code-tab:: csharp
  32. GetNode<Button>("%RedButton").Text = "Hello";
  33. Same-scene limitation
  34. ---------------------
  35. A scene unique node can only be retrieved by a node inside the same scene. To
  36. demonstrate this limitation, consider this example **Player** scene that
  37. instances a **Sword** scene:
  38. .. image:: img/unique_name_scene_instance_example.webp
  39. Here are the results of ``get_node()`` calls inside the **Player** script:
  40. - ``get_node("%Eyes")`` returns the **Eyes** node.
  41. - ``get_node("%Hilt")`` returns ``null``.
  42. These are the results of ``get_node()`` calls inside the **Sword** script:
  43. - ``get_node("%Eyes")`` returns ``null``.
  44. - ``get_node("%Hilt")`` returns the **Hilt** node.
  45. If a script has access to a node in another scene, it can call ``get_node()`` on
  46. that node to get scene unique nodes from that node's scene. This also works in a
  47. node path, which avoids multiple ``get_node()`` calls. Here are two ways to get
  48. the **Hilt** node from the **Player** script using scene unique nodes:
  49. - ``get_node("Hand/Sword").get_node("%Hilt")`` returns the **Hilt** node.
  50. - ``get_node("Hand/Sword/%Hilt")`` also returns the **Hilt** node.
  51. Scene unique names don't only work at the end of a node path. They can be used
  52. in the middle to navigate from one node to another. For example, the **Sword** node
  53. is marked as a scene unique node in the **Player** scene, so this is possible:
  54. - ``get_node("%Sword/%Hilt")`` returns the **Hilt** node.
  55. Alternatives
  56. ------------
  57. Scene unique nodes are a useful tool to navigate a scene. However, there are
  58. some situations where other techniques may be better.
  59. A :ref:`Group <doc_groups>` allows locating a node (or a group of many nodes)
  60. from any other node, no matter what scene the two nodes are located in.
  61. A :ref:`Singleton (Autoload) <doc_singletons_autoload>` is an always loaded node
  62. that can be accessed directly by any node regardless of the scene. These are useful
  63. when some data or functionality is shared globally.
  64. :ref:`Node.find_child() <class_Node_method_find_child>` finds a node by name
  65. without knowing its full path. This seems similar to a scene unique node, but
  66. this method is able to find nodes in nested scenes, and doesn't require marking
  67. the node in the scene editor in any way. However, this method is slow. Scene
  68. unique nodes are cached by Godot and are fast to retrieve, but each time the
  69. method is called, ``find_child()`` needs to check every descendant (every child,
  70. grandchild, and so on).