I have a subclass that uses methods and variables from another script which is the superclass. Currently this is done by making a reference to the superclass using var superclass = get_node("superclass_path")
. I wanted to change it so the subclass script extends the superclass script. I tried adding class_name superclass
to the superclass, and changing extends Node
in the subclass to extends superclass
. However when I do this, suddenly the superclass can no longer find any of its nodes, and I get a Node not found
error. Why is this happening?
A class is not a scene. The class does not have any nodes. Instead you have a script attached to a node.
So, your superclass
is attached to a node that has other nodes as children which you can get, for example using get_node("superclass_path")
.
Then you make another class that extends superclass
. Where do you attach it? Do you attach it to a node that has the same children? If you don't, then there is no reason to expect get_node("superclass_path")
will get them.
You might be interested in scene inheritance. You can create a scene derived form another scene from the context menu in the FielSystem panel and selecting "New Inherited Scene". The inherited scene will include the original (with its nodes and their properties, including the scripts). And then you can replace those properties. So you can replace the scripts of the nodes in the inherited scene if that is what you want… And the script you use may or may not extend the scripts of the original.