Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
Skupiny
Groups in Godot work like tags in other software. You can add a node to as many groups as you want. Then, in code, you can use the SceneTree to:
Get a list of nodes in a group.
Call a method on all nodes in a group.
Send a notification to all nodes in a group.
This is a useful feature to organize large scenes and decouple code.
Managing groups
Groups are created by adding a node to a new group name, and likewise they are removed by removing all nodes from a given group.
Existují dva způsoby přidávání/odebírání uzlů do skupin:
During design, by using the Node dock in the editor, or the Global Groups in project settings.
During execution, by calling Node.add_to_group() or Node.remove_from_group().
Použití doku uzlu
You can create new groups using the Groups tab in the Node dock.

Select a node in the Scene dock then click the add button with the + symbol.

You should now see the Create New Group modal appear. Write the group name in the field.
You can optionally mark the option "Global", which will make the group visible project-wide, and able to be reused in any project scene. This will also allow you to give it a description.
When done, press Ok to create it.

You should see the new groups appear in the Groups tab under Scene Groups if the Global option was unmarked, or under Global Groups if that option was marked.
A selected Node from the Scene dock can be added into groups by marking the checkbox on the left side of the groups in the Groups dock. The node you had selected when creating a new group will be automatically checked.

All groups present in the project that were marked as Global, created from any scene, will be visible under Global Groups.
Any other group derived from nodes in the current scene will appear under Scene Groups.
Varování
The same underlying logic is used for both Global and Scene groups. Groups with the same name are considered one and the same. This feature is purely organizational.

You can manage Global Groups in the Global Groups dock, inside Project Settings. There, you will be able to add new global groups, or change existing groups' names and descriptions.

Pomocí kódu
Skupiny můžete spravovat také ze skriptů. Následující kód přidá uzel, ke kterému připojíte skript, do skupiny guards
, jakmile vstoupí do stromu scény.
func _ready():
add_to_group("guards")
public override void _Ready()
{
base._Ready();
AddToGroup("guards");
}
Imagine you're creating an infiltration game. When an enemy spots the player, you want all guards and robots to be on alert.
In the fictional example below, we use SceneTree.call_group()
to alert all
enemies that the player was spotted.
func _on_player_spotted():
get_tree().call_group("guards", "enter_alert_mode")
public void _OnPlayerDiscovered()
{
GetTree().CallGroup("guards", "enter_alert_mode");
}
Výše uvedený kód volá funkci enter_alert_mode
u každého člena skupiny strážců
.
Je také možné získat úplný seznam uzlů nepřátel
voláním SceneTree.get_nodes_in_group ():
var guards = get_tree().get_nodes_in_group("guards")
var guards = GetTree().GetNodesInGroup("guards");
Třída SceneTree poskytuje mnoho dalších užitečných metod pro práci se scénami, jejich hierarchií uzlů a skupinami. Umožňuje snadno přepínat scény nebo je znovu načítat, ukončovat hru nebo ji pozastavovat a rušit. Poskytuje také užitečné signály.