Reputation: 530
I was working on adding an upgrade-like system to my game, but was having some trouble with the get_tree().get_root().get_node()
thing.
In an attempt to retrieve a different node multiple layers up, I go the error
ERROR: Attempt to call function 'get_root' in base 'null instance'\ on a null instance.
If I am correct, this means that it cannot find the node in the tree.
I have also tried using get_parent().get_parent()...
, as well as get_node("../../../.. ...")
with no luck.
Variables to get (points
and pointsAdder
)
extends Area2D
var points = 0.000000000001
var pointAdder = 1
var pointMultiplier = 1
var mouseover = false
var unit
var defaultArrow = load("defaultCursor_16x16.png")
var clickableCursor = load("clickableCursor.png")
func _ready():
get_parent().get_node("scoreLabel").text = "Logs: 0"
func _input(event):
if mouseover and event is InputEventMouseButton and event.pressed and event.button_index == BUTTON_LEFT:
points = (pointAdder* pointMultiplier) +points
if points < 1000:
set_label_text(points)
elif points >= 1000 and points < 1000000:
set_label_text(str(points / 1000) + " K")
unit = "thousand"
set_unit_text()
elif points >= 1000000 and points < 1000000000:
set_label_text(str(points / 1000000) + " M")
unit = "million"
set_unit_text()
elif points >= 1000000000 and points < 1000000000000:
set_label_text(str(points / 1000000000) + " B")
unit = "billion"
set_unit_text()
elif points >= 1000000000000:
set_label_text(str(points / 1000000000000) + " T")
unit = "trillion"
set_unit_text()
elif Input.is_key_pressed(KEY_SPACE) and not event.is_echo():
points = (pointAdder* pointMultiplier) +points
if points < 1000:
set_label_text(points)
elif points >= 1000 and points < 1000000:
set_label_text(str(points / 1000) + " K")
unit = "thousand"
set_unit_text()
elif points >= 1000000 and points < 1000000000:
set_label_text(str(points / 1000000) + " M")
unit = "million"
set_unit_text()
elif points >= 1000000000 and points < 1000000000000:
set_label_text(str(points / 1000000000) + " B")
unit = "billion"
set_unit_text()
elif points >= 1000000000000:
set_label_text(str(points / 1000000000000) + " T")
unit = "trillion"
set_unit_text()
func _on_Area2D_mouse_entered():
Input.set_custom_mouse_cursor(clickableCursor)
mouseover = true
func _on_Area2D_mouse_exited():
Input.set_custom_mouse_cursor(defaultArrow)
mouseover = false
func set_label_text(text_to_send):
get_parent().get_node("scoreLabel").text = ("Logs: "+str(text_to_send))
func set_unit_text():
get_parent().get_node("scoreUnitLabel").text = unit
And the code I am attempting to retrieve them in:
Extends Button
var points_cost = 10
func _on_Button_down():
if get_tree().get_root().get_node("treeClickableArea").points >= points_cost:
points_cost = (points_cost/2) + points_cost
get_tree().get_root().get_node("treeClickableArea").pointAdder = get_tree().get_root().get_node("").pointAdder + 1
get_tree().get_root().get_node("treeClickableArea").points = get_tree().get_root().get_node("treeClickableArea").points - points_cost
\Node tree:
┖╴Spatial
┠╴○ backgroundMap2
┠╴▦ backgroundMap
┠╴☺ scoreBackground
┠╴Tᵀ scoreLabel
┠╴Tᵀ scoreUnitLabel
┠╴☺ logSymbol
┠╴▭ treeClickableArea
┃ ┠╴☺ treeSprite
┃ ┖╴treeCollider
┠╴☺ upgradeBackground
┖╴▣ tabContainer
┠╴▯ perClick
┃ ┠╴perUpgradeScroll
┃ ┃ ┖╴▬ Button [scriptHavingIssuesHere]
┃ ┖╴Tᵀ Label
┖╴▯ perClickMultiplier
Could anybody clarify why I am getting this error? What exactly does it mean? Thank you for you time, I greatly appreciate any help.
Upvotes: 0
Views: 2288
Reputation: 801
Ok, another edit. I'm leaving the code from the edit I made before in case you need it for reference. I'm calling it EDIT#1.
The new example I'm posting is from a very simple test program I made. I'll call it EDIT#2.
EDIT#2 - TEST PROGRAM
Ok, so I made a simple 2D scene consisting of only a root node and 2 sprites that I named "Test1" and "Test2". I attached a script to both of the sprite nodes, each containing a variable called "points". In the first script I made points = 60,000,000
, and in the second, I made points = 16
. In the first script (Test1), I used onready var testNode
to load the Test2
node and store it as the variable testNode
. I then made a simple input event that prints the points
value of the testNode
variable when I press the key I assigned to "move forward". The expected result is to have the value 16 print to the console, despite both scripts containing a variable named points
which each have different values (to prove i'm getting the number from the second script). For me, it works just fine. Here is the code I used for the scripts:
Node setup
-Node2d
--Test1(sprite) - script 1
--Test2(sprite) - script 2
Script 1 -
extends Sprite
var points = 60000000
onready var testNode = get_parent().get_node("Test2")
func _input(event):
if Input.is_action_just_pressed("move_forward"):
print(testNode.points)
Script 2 -
extends Sprite
var points = 16
Hopefully this stripped down example can help provide some insight into what is going on. Hope it helps.
EDIT#1 - (PREVIOUS)NEW CODE EXAMPLE - for reference
I changed the code to be more reflective of our discussion. This is how it should look in final form. Notice I changed the name of the variable treePoints
to treeClickNode
for clarity.
extends Button
var points_cost = 10
onready var treeClickNode = get_tree().get_root().get_node("treeClickableArea")
func _on_Button_down():
if treeClickNode.points >= points_cost:
points_cost = (points_cost/2) + points_cost
treeClickNode.pointAdder += 1
treeClickNode.points = treeClickNode.points - points_cost
Upvotes: 1