groups.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. .. _doc_groups:
  2. Groups
  3. ======
  4. Groups in Godot work like tags in other software. You can add a node to as many
  5. groups as you want. Then, in code, you can use the SceneTree to:
  6. - Get a list of nodes in a group.
  7. - Call a method on all nodes in a group.
  8. - Send a notification to all nodes in a group.
  9. This is a useful feature to organize large scenes and decouple code.
  10. Managing groups
  11. ---------------
  12. Groups are created by adding a node to a new group name, and likewise they are
  13. removed by removing all nodes from a given group.
  14. There are two ways to add/remove nodes to groups:
  15. - During design, by using the Node dock in the editor, or the Global Groups in project settings.
  16. - During execution, by calling :ref:`Node.add_to_group() <class_Node_method_add_to_group>`
  17. or :ref:`Node.remove_from_group() <class_Node_method_remove_from_group>`.
  18. Using the Node dock
  19. ~~~~~~~~~~~~~~~~~~~
  20. You can create new groups using the Groups tab in the Node dock.
  21. .. image:: img/groups_node_tab.webp
  22. Select one or more nodes in the Scene dock then click the add button with the + symbol.
  23. .. image:: img/groups_add_new_group_button.webp
  24. You should now see the Create New Group modal appear. Write the group name in the field.
  25. You can optionally mark the option "Global", which will make the group visible project-wide,
  26. and able to be reused in any project scene. This will also allow you to give it a description.
  27. When done, press Ok to create it.
  28. .. image:: img/groups_add_new_group_modal.webp
  29. You should see the new groups appear in the Groups tab under Scene Groups if the Global option was
  30. unmarked, or under Global Groups if that option was marked.
  31. Selected Node(s) from the Scene dock can be added into groups by marking the checkbox on the left side
  32. of the groups in the Groups dock. The node(s) you had selected when creating a new group will be automatically checked.
  33. .. image:: img/groups_node_tab_with_created_groups.webp
  34. All groups present in the project that were marked as Global, created from any scene, will be visible under Global Groups.
  35. Any other group derived from nodes in the current scene will appear under Scene Groups.
  36. .. warning:: The same underlying logic is used for both Global and Scene groups.
  37. Groups with the same name are considered one and the same. This feature is purely organizational.
  38. .. image:: img/groups_node_tab_with_multiple_types_of_groups.webp
  39. You can manage Global Groups in the Global Groups dock, inside Project Settings. There, you will be able to add new
  40. global groups, or change existing groups' names and descriptions.
  41. .. image:: img/groups_global_groups_settings.webp
  42. Using code
  43. ~~~~~~~~~~
  44. You can also manage groups from scripts. The following code adds the node to
  45. which you attach the script to the ``guards`` group as soon as it enters the
  46. scene tree.
  47. .. tabs::
  48. .. code-tab:: gdscript GDScript
  49. func _ready():
  50. add_to_group("guards")
  51. .. code-tab:: csharp
  52. public override void _Ready()
  53. {
  54. base._Ready();
  55. AddToGroup("guards");
  56. }
  57. Imagine you're creating an infiltration game. When an
  58. enemy spots the player, you want all guards and robots to be on alert.
  59. In the fictional example below, we use ``SceneTree.call_group()`` to alert all
  60. enemies that the player was spotted.
  61. .. tabs::
  62. .. code-tab:: gdscript GDScript
  63. func _on_player_spotted():
  64. get_tree().call_group("guards", "enter_alert_mode")
  65. .. code-tab:: csharp
  66. public void _OnPlayerDiscovered()
  67. {
  68. GetTree().CallGroup("guards", "enter_alert_mode");
  69. }
  70. The above code calls the function ``enter_alert_mode`` on every member of the
  71. group ``guards``.
  72. To get the full list of nodes in the ``guards`` group as an array, you can call
  73. :ref:`SceneTree.get_nodes_in_group()
  74. <class_SceneTree_method_get_nodes_in_group>`:
  75. .. tabs::
  76. .. code-tab:: gdscript GDScript
  77. var guards = get_tree().get_nodes_in_group("guards")
  78. .. code-tab:: csharp
  79. var guards = GetTree().GetNodesInGroup("guards");
  80. The :ref:`SceneTree <class_SceneTree>` class provides many more useful methods
  81. to interact with scenes, their node hierarchy, and groups. It allows you to
  82. switch scenes easily or reload them, quit the game or pause and unpause it. It
  83. also provides useful signals.