groups.rst 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.
  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 add nodes in the current scene to groups using the Groups tab in the
  21. Node dock.
  22. .. image:: img/groups_node_tab.png
  23. Select one or more nodes in the Scene dock and write the group name in the
  24. field, then click Add.
  25. .. image:: img/groups_add_node_to_group.png
  26. You should now see the group appear.
  27. .. image:: img/groups_node_after_adding.png
  28. In a complex project, you may end up with many groups or large scenes with many
  29. nodes. You can add or remove any node to groups using the Group Editor window.
  30. To access it, click the Manage Groups button.
  31. .. image:: img/groups_manage_groups_button.png
  32. The Group Editor window appears. Here's a screenshot from a complex project to
  33. illustrate the tool's purpose.
  34. .. image:: img/groups_group_editor_window.png
  35. It has three columns:
  36. 1. A list of groups used by nodes in the current scene.
  37. 2. A list of nodes that are not part of the selected group.
  38. 3. A list of nodes in the group.
  39. The fields at the bottom allow you to add new groups or filter nodes in the
  40. second and third columns.
  41. .. note:: Any node name that's greyed out means the node was added to the group
  42. in a different scene and you cannot edit it here. This happens on
  43. scene instances in particular.
  44. Using code
  45. ~~~~~~~~~~
  46. You can also manage groups from scripts. The following code adds the node to
  47. which you attach the script to the ``guards`` group as soon as it enters the
  48. scene tree.
  49. .. tabs::
  50. .. code-tab:: gdscript GDScript
  51. func _ready():
  52. add_to_group("guards")
  53. .. code-tab:: csharp
  54. public override void _Ready()
  55. {
  56. base._Ready();
  57. AddToGroup("guards");
  58. }
  59. Imagine you're creating an infiltration game. When an
  60. enemy spots the player, you want all guards and robots to be on alert.
  61. In the fictional example below, we use ``SceneTree.call_group()`` to alert all
  62. enemies that the player was spotted.
  63. .. tabs::
  64. .. code-tab:: gdscript GDScript
  65. func _on_Player_spotted():
  66. get_tree().call_group("guards", "enter_alert_mode")
  67. .. code-tab:: csharp
  68. public void _OnPlayerDiscovered()
  69. {
  70. GetTree().CallGroup("guards", "enter_alert_mode");
  71. }
  72. The above code calls the function ``enter_alert_mode`` on every member of the
  73. group ``guards``.
  74. To get the full list of nodes in the ``guards`` group as an array, you can call
  75. :ref:`SceneTree.get_nodes_in_group()
  76. <class_SceneTree_method_get_nodes_in_group>`:
  77. .. tabs::
  78. .. code-tab:: gdscript GDScript
  79. var guards = get_tree().get_nodes_in_group("guards")
  80. .. code-tab:: csharp
  81. var guards = GetTree().GetNodesInGroup("guards");
  82. The :ref:`SceneTree <class_SceneTree>` class provides many more useful methods
  83. to interact with scenes, their node hierarchy, and groups. It allows you to
  84. switch scenes easily or reload them, quit the game or pause and unpause it. It
  85. also provides useful signals.