Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion loader/include/Geode/cocos/base_nodes/CCNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, PleaseDontChangeMe>::type;
static_assert(!std::is_void_v<CCArrayExt>, "Please include <Geode/utils/cocos.hpp> to use getChildrenExt()");

Expand Down Expand Up @@ -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 <class InpT = cocos2d::CCNode*, class T = std::remove_pointer_t<InpT>>
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<int>(this->getChildrenCount()) <= i) return nullptr;
return static_cast<T*>(this->getChildren()->objectAtIndex(i));
}

/**
* Get nth child that is a given type. Checks bounds.
* @returns Child at index cast to the given type,
Expand Down
9 changes: 2 additions & 7 deletions loader/include/Geode/utils/cocos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <class T = cocos2d::CCNode>
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<int>(x->getChildrenCount()) <= i) return nullptr;
return static_cast<T*>(x->getChildren()->objectAtIndex(i));
return x->getChildByIndex<T>(i);
}

/**
Expand Down
Loading