I am trying to make a class which inherits from another class. The below example works except that I can not access anything in the node tree from the inherited class. Please note that subClass is an inner class but also does not seem to work with a normal class (gd script file) be it inherited or not. I can access functions and variables in the class which I inherited from but if I try to call something like "print (get_path())" it just prints nothing and throws an error:
0:00:02:0489 - Condition ' !is_inside_tree() ' is true. returned: NodePath()
----------
Type:Error
Description:
Time: 0:00:02:0489
C Error: Condition ' !is_inside_tree() ' is true. returned: NodePath()
C Source: scene\main\node.cpp:1780
C Function: Node::get_path
Here is the below code and project file in a downloadable zip.
mainClassScript.gd attached to the root node, a Spatial Node called main:
extends Spatial
var testVar = "this is a test var"
var containsObject
func _ready():
printPath() # prints: "/root/main"
containsObject = $sub.subClass.new()
func testFunc():
testVar = "test var changed!"
func printPath():
print (get_path())
func _unhandled_input(event):
containsObject.handleInput(event)
subClassScript.gd attached to a Spatial Node called sub which is under "main" in the node tree:
extends Node
class subClass:
extends "mainClassScript.gd"
func _init():
pass
func handleInput(event):
if event.is_action_pressed('ui_accept'): # press SPACEBAR
print(testVar) # properly prints out: "this is a test var"
testFunc() # modifies testVar to "test var changed!"
print(testVar) # properly prints out: "test var changed!"
printPath() # prints nothing and produces error: Condition ' !is_inside_tree() ' is true. returned: NodePath()
Any help you can offer would be greatly appreciated! Thank you!