Passing a variable value into a label node to display the value

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: 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:
example project image 1
it is the same as (just so you know if you see “$Label” instead “get_node(“Label”)” in the future:
example project image 2

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:

  1. get the the speed variable from the function inside the script attached to Character node
  2. 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

:bust_in_silhouette: Reply From: oofman

From your comments information node tree below is my answer.

If you want to edit label from CharacterNode attached script:

func _ready():
    get_parent.get_node("ControlNode").get_node("Speed_Label_Node").text = var2str(speed_value)

You can also check if you have correct speed_value with:

print(var2str(speed_value))

So yoi’re saying that this code should be included in the CharacterNode attached script, not the ControlNode script? So the other way round,?

Macryc | 2020-01-16 12:00

If you want to edit label from ControlNode attached script:

func _ready():
    get_node("Speed_Label_Node").text = var2str(get_parent().get_node("CharacterNode").speed_value)

In my games I like as less different .gd script files as possible. But it depends only on you how you want your game to look like. :slight_smile:

oofman | 2020-01-16 12:08

It worked, thank you. But now I’m racking my brains about why it didn’t work with my code. The only difference was a get_parent() added in front of get_node, inside the Control node’s script. I am wondering why this fixed the issue. I have other label nodes parented to this Control node in much the same way as the speed label node and I am able to fetch variables from other scenes/scripts with just get_node and an absolute path from root.
Eg, under the same control node I have a label showing a timer. I am able to fetch the timer via this:

onready var Timer = get_node("/root/Level1Scene/PlayerScene/GameTimer")

This works without get_parent() in front, even though the timer sits in a fairly remote node… Hmm.

Macryc | 2020-01-16 12:59