groups.rst 3.4 KB

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