2023-01-12 20:49:14 +01:00
|
|
|
|
2022-03-18 17:46:08 +01:00
|
|
|
|
2024-04-30 20:17:58 +02:00
|
|
|
# Groups
|
2022-03-18 17:46:08 +01:00
|
|
|
|
2024-03-16 20:56:52 +01:00
|
|
|
Groups in Pandemonium work like tags in other software. You can add a node to as many
|
2022-03-18 17:46:08 +01:00
|
|
|
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.
|
|
|
|
|
|
|
|
|
2024-04-30 20:17:58 +02:00
|
|
|
## Managing groups
|
2022-09-10 12:15:58 +02:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
There are two ways to add/remove nodes to groups:
|
|
|
|
|
|
|
|
- During design, by using the Node dock in the editor.
|
2023-01-12 19:30:47 +01:00
|
|
|
- During execution, by calling `Node.add_to_group()`
|
|
|
|
or `Node.remove_from_group()`.
|
2022-03-18 17:46:08 +01:00
|
|
|
|
|
|
|
|
2024-04-30 20:17:58 +02:00
|
|
|
### Using the Node dock
|
2022-03-18 17:46:08 +01:00
|
|
|
|
|
|
|
You can add nodes in the current scene to groups using the Groups tab in the
|
|
|
|
Node dock.
|
|
|
|
|
2023-01-12 20:16:00 +01:00
|
|
|
![](img/groups_node_tab.png)
|
2022-03-18 17:46:08 +01:00
|
|
|
|
|
|
|
Select one or more nodes in the Scene dock and write the group name in the
|
|
|
|
field, then click Add.
|
|
|
|
|
2023-01-12 20:16:00 +01:00
|
|
|
![](img/groups_add_node_to_group.png)
|
2022-03-18 17:46:08 +01:00
|
|
|
|
|
|
|
You should now see the group appear.
|
|
|
|
|
2023-01-12 20:16:00 +01:00
|
|
|
![](img/groups_node_after_adding.png)
|
2022-03-18 17:46:08 +01:00
|
|
|
|
|
|
|
In a complex project, you may end up with many groups or large scenes with many
|
|
|
|
nodes. You can add or remove any node to groups using the Group Editor window.
|
|
|
|
To access it, click the Manage Groups button.
|
|
|
|
|
2023-01-12 20:16:00 +01:00
|
|
|
![](img/groups_manage_groups_button.png)
|
2022-03-18 17:46:08 +01:00
|
|
|
|
|
|
|
The Group Editor window appears. Here's a screenshot from a complex project to
|
|
|
|
illustrate the tool's purpose.
|
|
|
|
|
2023-01-12 20:16:00 +01:00
|
|
|
![](img/groups_group_editor_window.png)
|
2022-03-18 17:46:08 +01:00
|
|
|
|
|
|
|
It has three columns:
|
|
|
|
|
|
|
|
1. A list of groups used by nodes in the current scene.
|
|
|
|
2. A list of nodes that are not part of the selected group.
|
|
|
|
3. A list of nodes in the group.
|
|
|
|
|
|
|
|
The fields at the bottom allow you to add new groups or filter nodes in the
|
|
|
|
second and third columns.
|
|
|
|
|
2023-01-12 20:55:57 +01:00
|
|
|
Note:
|
|
|
|
Any node name that's greyed out means the node was added to the group
|
2022-03-18 17:46:08 +01:00
|
|
|
in a different scene and you cannot edit it here. This happens on
|
|
|
|
scene instances in particular.
|
|
|
|
|
2024-04-30 20:17:58 +02:00
|
|
|
### Using code
|
2022-03-18 17:46:08 +01:00
|
|
|
|
|
|
|
You can also manage groups from scripts. The following code adds the node to
|
2023-01-12 19:43:03 +01:00
|
|
|
which you attach the script to the `guards` group as soon as it enters the
|
2022-03-18 17:46:08 +01:00
|
|
|
scene tree.
|
|
|
|
|
2023-01-12 18:31:02 +01:00
|
|
|
gdscript GDScript
|
2022-03-18 17:46:08 +01:00
|
|
|
|
2023-01-12 18:31:02 +01:00
|
|
|
```
|
2024-05-04 14:08:00 +02:00
|
|
|
func _ready():
|
|
|
|
add_to_group("guards")
|
2023-01-12 18:31:02 +01:00
|
|
|
```
|
2022-03-18 17:46:08 +01:00
|
|
|
|
|
|
|
Imagine you're creating an infiltration game. When an
|
|
|
|
enemy spots the player, you want all guards and robots to be on alert.
|
|
|
|
|
2023-01-12 19:43:03 +01:00
|
|
|
In the fictional example below, we use `SceneTree.call_group()` to alert all
|
2022-03-18 17:46:08 +01:00
|
|
|
enemies that the player was spotted.
|
|
|
|
|
2023-01-12 18:31:02 +01:00
|
|
|
gdscript GDScript
|
2022-03-18 17:46:08 +01:00
|
|
|
|
2023-01-12 18:31:02 +01:00
|
|
|
```
|
2024-05-04 14:08:00 +02:00
|
|
|
func _on_Player_spotted():
|
|
|
|
get_tree().call_group("guards", "enter_alert_mode")
|
2023-01-12 18:31:02 +01:00
|
|
|
```
|
2022-03-18 17:46:08 +01:00
|
|
|
|
2023-01-12 19:43:03 +01:00
|
|
|
The above code calls the function `enter_alert_mode` on every member of the
|
|
|
|
group `guards`.
|
2022-03-18 17:46:08 +01:00
|
|
|
|
2023-01-12 19:43:03 +01:00
|
|
|
To get the full list of nodes in the `guards` group as an array, you can call
|
2023-01-12 19:29:11 +01:00
|
|
|
`SceneTree.get_nodes_in_group()
|
2023-01-12 20:47:54 +01:00
|
|
|
( SceneTree_method_get_nodes_in_group )`:
|
2022-03-18 17:46:08 +01:00
|
|
|
|
2023-01-12 18:31:02 +01:00
|
|
|
gdscript GDScript
|
2022-03-18 17:46:08 +01:00
|
|
|
|
2023-01-12 18:31:02 +01:00
|
|
|
```
|
2024-05-04 14:08:00 +02:00
|
|
|
var guards = get_tree().get_nodes_in_group("guards")
|
2023-01-12 18:31:02 +01:00
|
|
|
```
|
2022-03-18 17:46:08 +01:00
|
|
|
|
2023-01-12 19:30:47 +01:00
|
|
|
The `SceneTree` class provides many more useful methods
|
2022-03-18 17:46:08 +01:00
|
|
|
to interact with scenes, their node hierarchy, and groups. It allows you to
|
|
|
|
switch scenes easily or reload them, quit the game or pause and unpause it. It
|
|
|
|
also provides useful signals.
|