From bdf614d3d75551164fdd1eed7fb5539cafa30330 Mon Sep 17 00:00:00 2001 From: SekoiaTree Date: Tue, 25 Aug 2020 14:46:33 +0200 Subject: [PATCH] Made get_child support negative indexes, with documentation --- doc/classes/Node.xml | 1 + scene/main/node.cpp | 3 +++ 2 files changed, 4 insertions(+) diff --git a/doc/classes/Node.xml b/doc/classes/Node.xml index 8f66c9df383..72088e89d1d 100644 --- a/doc/classes/Node.xml +++ b/doc/classes/Node.xml @@ -213,6 +213,7 @@ Returns a child node by its index (see [method get_child_count]). This method is often used for iterating all children of a node. + Negative indices access the children from the last one. To access a child node via its name, use [method get_node]. diff --git a/scene/main/node.cpp b/scene/main/node.cpp index 653eb698d47..6b304c03d2a 100644 --- a/scene/main/node.cpp +++ b/scene/main/node.cpp @@ -1327,6 +1327,9 @@ int Node::get_child_count() const { } Node *Node::get_child(int p_index) const { + if (p_index < 0) { + p_index += data.children.size(); + } ERR_FAIL_INDEX_V(p_index, data.children.size(), nullptr); return data.children[p_index];