+1 vote

Hello, if i had a parent node that has a child, like this :


The Parent node has position(x,y), how can i get the position of the parent node in the child node, in GDScript i can use get_owner(), but i need to do it in GDNative.

in Engine by (61 points)

2 Answers

–1 vote

You can use get_parent() to access parent node from child node.

print(get_parent().name) # Will print parent node 
print(get_parent().position) # Will print parent node position

I'm using Godot 3.0.

Hope this will helpful

by (123 points)
0 votes

I realize I'm late here but for anyone else's reference:

Your GDNative class should still have the function get_parent() if it inherits from the godot::Node class.

So let's say you wished to perform the following (GDScript version):

var parent : Node = get_parent()

The GDNative equivalent would be:

godot::Node* parent = get_parent();
godot::Godot::print(parent -> get_name());

When I'm a little lost I find it useful to check the header files for GDNative. In the case of Node.hpp it would be in godot_cpp/include/gen/Node.hpp.

In your case, if you have position(x, y) and know the node is a / is a child of Node2D then you could simply perform:

godot::Node2D* parent = get_parent();
godot::Vector2 pos = parent -> get_position();
by (14 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.