@@ -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
@@ -1144,6 +1144,23 @@ class CC_DLL CCNode : public CCObject
11441144 GEODE_DLL geode::EventListenerProtocol* getEventListener (std::string const & id);
11451145 GEODE_DLL size_t getEventListenerCount ();
11461146
1147+ /* *
1148+ * Get child at index. Checks bounds. A negative
1149+ * index will get the child starting from the end
1150+ * @returns Child at index cast to the given type,
1151+ * or nullptr if index exceeds bounds
1152+ */
1153+ template <class InpT = cocos2d::CCNode*, class T = std::remove_pointer_t <InpT>>
1154+ T* getChildByIndex (int i) {
1155+ // start from end for negative index
1156+ if (i < 0 ) i = this ->getChildrenCount () + i;
1157+ // check if backwards index is out of bounds
1158+ if (i < 0 ) return nullptr ;
1159+ // check if forwards index is out of bounds
1160+ if (static_cast <int >(this ->getChildrenCount ()) <= i) return nullptr ;
1161+ return static_cast <T*>(this ->getChildren ()->objectAtIndex (i));
1162+ }
1163+
11471164 /* *
11481165 * Get nth child that is a given type. Checks bounds.
11491166 * @returns Child at index cast to the given type,
0 commit comments