![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Macryc |
Please help me with my code - I am getting the ‘invalid get index ‘variable’ on base Nil’ error with this.
I am trying to have a label display a value from a function in a separate node. I am accessing that node’s function via get node.function() and am then trying to have the label print the value from that function. No joy. Here’s the code:
extends Control
#fetching the function from another scene:
onready var speed_value = get_node("/root/Main”).speedometer_function_which_returns_speed_value_in_variable()
#fetching the label node:
onready var speedometer = get_node("speedometer_Label”)
func _physics_process(delta):
speedometer.text = String(speed_value.variable_that_holds_speed_value)
Need more information. Can you drop image or ASCII representation of your node tree? For example mine is:
it is the same as (just so you know if you see “$Label” instead “get_node(“Label”)” in the future:
My best guess is that you are not catching your nodes by the tree you created. Try to use “get_parent().get_parent().variable_that_holds_speed_value” if your label is inside 2x your node which has the attached script.
In other case if your label is outside your node which has the attached script with “variable_that_holds_speed_value” use:
get_node(“speedometer_Label”).text = variable_that_holds_speed_value
If you give me exact node tree you have in your project I can give you exact answer.
oofman | 2020-01-16 11:09
Hi oofman. My hierarchy is as follows:
MainNode
CharacterNode (this one has a script attached that has the speed calculating function in it, and assigns value to the speed variable on every frame)
SomeNode
SomeNode
ControlNode
Speed_Label_Node
So, in a script that is attached to the Control node (of which Label is a child) I am trying (with the code above) to:
- get the the speed variable from the function inside the script attached to Character node
- pass this variable into the label so it can be displayed
Macryc | 2020-01-16 11:35
I think it’s this part that isn’t working:
onready var speed_value = get_node("/root/MainNode/CharacterNode”).speedometer_function_which_returns_speed_value_in_variable()
It looks like i’m not able to access that function from the Control node’s script…
Macryc | 2020-01-16 11:41