How to get parent node in GDNative ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By soulldev

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

Parent
    Child

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.

1 Like
:bust_in_silhouette: Reply From: atopetrick

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

:bust_in_silhouette: Reply From: Binsk

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()
print(parent.name)

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();
godot::Godot::print(pos);