Unexpected results from get_parent()

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

So I was experimenting with the following setup:

ParentNode (Node)
|_ ChildNode (Node)
|_GrandchildNode (Node)

ParentNode.gd:

extends Node

onready var parent = get_parent()

func _ready():
	print( "From parent:" )
	print( parent.name )

ChildNode.gd:

extends "res://ParentNode.gd"

onready var child_parent = get_parent()

func _ready():
	print( "From child:" )
	print( child_parent.name )

GrandchildNode.gd:

extends "res://ChildNode.gd"

onready var grandchild_parent = get_parent()

func _ready():
	print( "From grandchild:" )
	print( grandchild_parent.name )

The output is peculiar:

From parent:
child_node
From child:
child_node
From grandchild:
child_node
From parent:
Parent_node
From child:
Parent_node
From parent:
root

It looks to me that this is through a process of constructing the tree, the ready function is repeatedly called by the nodes as they are added to the scene tree. If I remove inherited from, it seems to work fine.

This is obviously a simpler version. The trouble is, I have a more complicated version where the child nodes inherited from the parent node. I then instance this scene in another scene. I want the parent node to access a sibling node in the scene tree, but I think the “extends [parent node]” part messes everything up. For example, imagine I instanced the above in to another scene ( a character scene, or something):

Character
|
|_ Sprite
|_ParentNode
|_AnimationTree

I want to access the AnimationTree from the ParentNode, but it just returns null instance. When I can assign a node, it does the same peculiar thing as before: for example, instead of returning AnimationTree, it will return ChildNode, or it thinks the ParentNodes’ parent is ChildNode…

Is there a way to use “extends [parent node]” without getting this strange behaviour?