Skip to content

Commit a509959

Browse files
committed
add CCNode::getChildByIndex
1 parent 7a08b4e commit a509959

File tree

1 file changed

+18
-1
lines changed
  • loader/include/Geode/cocos/base_nodes

1 file changed

+18
-1
lines changed

loader/include/Geode/cocos/base_nodes/CCNode.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ class CC_DLL CCNode : public CCObject
648648
inline auto getChildrenExt() {
649649
// CCArrayExt is defined in geode/utils/cocos.hpp, which we cannot include due to circular includes.
650650
// This is an incredibly hacky way to still be able to use the type
651-
651+
652652
using CCArrayExt = geode::CCArrayExtCheck<T, PleaseDontChangeMe>::type;
653653
static_assert(!std::is_void_v<CCArrayExt>, "Please include <Geode/utils/cocos.hpp> to use getChildrenExt()");
654654

@@ -1182,6 +1182,23 @@ class CC_DLL CCNode : public CCObject
11821182
return nullptr;
11831183
}
11841184

1185+
/**
1186+
* Get child at index. Checks bounds. A negative
1187+
* index will get the child starting from the end
1188+
* @returns Child at index cast to the given type,
1189+
* or nullptr if index exceeds bounds
1190+
*/
1191+
template <class T = cocos2d::CCNode>
1192+
T* getChildByIndex(int i) {
1193+
// start from end for negative index
1194+
if (i < 0) i = this->getChildrenCount() + i;
1195+
// check if backwards index is out of bounds
1196+
if (i < 0) return nullptr;
1197+
// check if forwards index is out of bounds
1198+
if (static_cast<int>(this->getChildrenCount()) <= i) return nullptr;
1199+
return static_cast<T*>(this->getChildren()->objectAtIndex(i));
1200+
}
1201+
11851202
/// @{
11861203
/// @name Shader Program
11871204
/**

0 commit comments

Comments
 (0)