Skip to content

Commit

Permalink
Added Node.find_node(mask) function
Browse files Browse the repository at this point in the history
by popular request
  • Loading branch information
reduz committed Jun 8, 2015
1 parent 798b55d commit f052153
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
23 changes: 23 additions & 0 deletions scene/main/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,28 @@ bool Node::has_node(const NodePath& p_path) const {
return _get_node(p_path)!=NULL;
}


Node* Node::find_node(const String& p_mask,bool p_recursive,bool p_owned) const {

Node * const*cptr = data.children.ptr();
int ccount = data.children.size();
for(int i=0;i<ccount;i++) {
if (p_owned && !cptr[i]->data.owner)
continue;
if (cptr[i]->data.name.operator String().match(p_mask))
return cptr[i];

if (!p_recursive)
continue;

Node* ret = cptr[i]->find_node(p_mask,true,p_owned);
if (ret)
return ret;
}
return NULL;

}

Node *Node::get_parent() const {

return data.parent;
Expand Down Expand Up @@ -1807,6 +1829,7 @@ void Node::_bind_methods() {
ObjectTypeDB::bind_method(_MD("has_node","path"),&Node::has_node);
ObjectTypeDB::bind_method(_MD("get_node:Node","path"),&Node::get_node);
ObjectTypeDB::bind_method(_MD("get_parent:Parent"),&Node::get_parent);
ObjectTypeDB::bind_method(_MD("find_node:Node","mask","recursive","owned"),&Node::get_node,DEFVAL(true),DEFVAL(true));
ObjectTypeDB::bind_method(_MD("has_node_and_resource","path"),&Node::has_node_and_resource);
ObjectTypeDB::bind_method(_MD("get_node_and_resource","path"),&Node::_get_node_and_resource);

Expand Down
2 changes: 2 additions & 0 deletions scene/main/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class Node : public Object {
Node *_get_node(const NodePath& p_path) const;



void _validate_child_name(Node *p_name);

void _propagate_reverse_notification(int p_notification);
Expand Down Expand Up @@ -186,6 +187,7 @@ friend class PackedScene;
Node *get_child(int p_index) const;
bool has_node(const NodePath& p_path) const;
Node *get_node(const NodePath& p_path) const;
Node* find_node(const String& p_mask,bool p_recursive=true,bool p_owned=true) const;
bool has_node_and_resource(const NodePath& p_path) const;
Node *get_node_and_resource(const NodePath& p_path,RES& r_res) const;

Expand Down

0 comments on commit f052153

Please sign in to comment.