diff --git a/loader/include/Geode/cocos/base_nodes/CCNode.h b/loader/include/Geode/cocos/base_nodes/CCNode.h index 8382e5651..dd4e9989f 100644 --- a/loader/include/Geode/cocos/base_nodes/CCNode.h +++ b/loader/include/Geode/cocos/base_nodes/CCNode.h @@ -648,7 +648,7 @@ class CC_DLL CCNode : public CCObject inline auto getChildrenExt() { // CCArrayExt is defined in geode/utils/cocos.hpp, which we cannot include due to circular includes. // This is an incredibly hacky way to still be able to use the type - + using CCArrayExt = geode::CCArrayExtCheck::type; static_assert(!std::is_void_v, "Please include to use getChildrenExt()"); @@ -1144,6 +1144,23 @@ class CC_DLL CCNode : public CCObject GEODE_DLL geode::EventListenerProtocol* getEventListener(std::string const& id); GEODE_DLL size_t getEventListenerCount(); + /** + * Get child at index. Checks bounds. A negative + * index will get the child starting from the end + * @returns Child at index cast to the given type, + * or nullptr if index exceeds bounds + */ + template > + T* getChildByIndex(int i) { + // start from end for negative index + if (i < 0) i = this->getChildrenCount() + i; + // check if backwards index is out of bounds + if (i < 0) return nullptr; + // check if forwards index is out of bounds + if (static_cast(this->getChildrenCount()) <= i) return nullptr; + return static_cast(this->getChildren()->objectAtIndex(i)); + } + /** * Get nth child that is a given type. Checks bounds. * @returns Child at index cast to the given type, diff --git a/loader/include/Geode/utils/cocos.hpp b/loader/include/Geode/utils/cocos.hpp index ba1854c40..ee1beec1a 100644 --- a/loader/include/Geode/utils/cocos.hpp +++ b/loader/include/Geode/utils/cocos.hpp @@ -642,18 +642,13 @@ namespace geode::cocos { /** * Get child at index. Checks bounds. A negative * index will get the child starting from the end + * @deprecated Use CCNode::getChildByIndex instead * @returns Child at index cast to the given type, * or nullptr if index exceeds bounds */ template static T* getChild(cocos2d::CCNode* x, int i) { - // start from end for negative index - if (i < 0) i = x->getChildrenCount() + i; - // check if backwards index is out of bounds - if (i < 0) return nullptr; - // check if forwards index is out of bounds - if (static_cast(x->getChildrenCount()) <= i) return nullptr; - return static_cast(x->getChildren()->objectAtIndex(i)); + return x->getChildByIndex(i); } /**