0 votes

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) 
asked Jan 16 in Engine by Macryc (44 points)

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 "getparent().getparent().variablethatholdsspeedvalue" 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 "variablethatholdsspeedvalue" use:
getnode("speedometerLabel").text = variablethatholdsspeedvalue

If you give me exact node tree you have in your project I can give you exact answer.

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

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...

1 Answer

0 votes

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))
answered Jan 16 by oofman (72 points)
edited Jan 16 by oofman

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

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. :)

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.