From bddf24645d127cbdda13a42598f2a2d31b9b2335 Mon Sep 17 00:00:00 2001
From: Alula <6276139+alula@users.noreply.github.com>
Date: Wed, 14 Jun 2023 00:36:10 +0200
Subject: [PATCH] Add SceneTree.get_node_count_in_group()
---
doc/classes/SceneTree.xml | 7 +++++++
scene/main/scene_tree.cpp | 11 +++++++++++
scene/main/scene_tree.h | 1 +
3 files changed, 19 insertions(+)
diff --git a/doc/classes/SceneTree.xml b/doc/classes/SceneTree.xml
index ce793deec17..92583379ce8 100644
--- a/doc/classes/SceneTree.xml
+++ b/doc/classes/SceneTree.xml
@@ -117,6 +117,13 @@
Returns the number of nodes in this [SceneTree].
+
+
+
+
+ Returns the number of nodes assigned to the given group.
+
+
diff --git a/scene/main/scene_tree.cpp b/scene/main/scene_tree.cpp
index e071011c48d..e46f399248d 100644
--- a/scene/main/scene_tree.cpp
+++ b/scene/main/scene_tree.cpp
@@ -1301,6 +1301,16 @@ bool SceneTree::has_group(const StringName &p_identifier) const {
return group_map.has(p_identifier);
}
+int SceneTree::get_node_count_in_group(const StringName &p_group) const {
+ _THREAD_SAFE_METHOD_
+ HashMap::ConstIterator E = group_map.find(p_group);
+ if (!E) {
+ return 0;
+ }
+
+ return E->value.nodes.size();
+}
+
Node *SceneTree::get_first_node_in_group(const StringName &p_group) {
_THREAD_SAFE_METHOD_
HashMap::Iterator E = group_map.find(p_group);
@@ -1589,6 +1599,7 @@ void SceneTree::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_nodes_in_group", "group"), &SceneTree::_get_nodes_in_group);
ClassDB::bind_method(D_METHOD("get_first_node_in_group", "group"), &SceneTree::get_first_node_in_group);
+ ClassDB::bind_method(D_METHOD("get_node_count_in_group", "group"), &SceneTree::get_node_count_in_group);
ClassDB::bind_method(D_METHOD("set_current_scene", "child_node"), &SceneTree::set_current_scene);
ClassDB::bind_method(D_METHOD("get_current_scene"), &SceneTree::get_current_scene);
diff --git a/scene/main/scene_tree.h b/scene/main/scene_tree.h
index 4e7a10c7e9f..b462cb0be85 100644
--- a/scene/main/scene_tree.h
+++ b/scene/main/scene_tree.h
@@ -392,6 +392,7 @@ public:
void get_nodes_in_group(const StringName &p_group, List *p_list);
Node *get_first_node_in_group(const StringName &p_group);
bool has_group(const StringName &p_identifier) const;
+ int get_node_count_in_group(const StringName &p_group) const;
//void change_scene(const String& p_path);
//Node *get_loaded_scene();