Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | XCendoX |
Im making a game 2d and this i have this error in my health script:
Attempt to call function ‘set_value’ in base ‘null instance’ on a null instance.
get_node: (Node not found: "player1/HealthBar_p1" (relative to "/root/Player1Script").)
Error de C++: Method failed. Returning: nullptr
Fuente C++: scene/main/node.cpp:1465 @ get_node()
Rastreo de Pila: player_1_script.gd:34 @ _physics_process()
This is the code:
extends KinematicBody2D
var health = 100
func _ready():
pass
func _physics_process(delta):
get_node("player1/HealthBar_p1").set_value(health)
I copied the path by right clicking on the node
Image of the nodes:
can anybody help me?
So that get_node()
call isn’t finding the node you’ve specified. For the node path you’ve provided to work as expected, your scene tree needs to look like this:
KinematicBody2D # with any name, but the above script is on this node
- player1
- HealthBar_p1
If that’s not how your scene tree is structured, that reference will fail (like it has).
jgodfrey | 2022-12-14 20:06
Oh, wait. I just realized that the error is with a node named ProgressBar
, that’s not shown in your above code. Still, the above explanation stands, just with a different node reference…
jgodfrey | 2022-12-14 20:08
My scene tree is:
player1 # a KinematicBody2D with the script
- HealthBar_p1
I tried putting
get_node("HealthBar_p1").set_value(health)
but the error persist
XCendoX | 2022-12-14 20:17
Based on your latest (above) comment, that should work. So, I think something must not be exactly as you expect. I’m not sure what else to say without more info.
Maybe you can post more of the code (for context) and an image of your scene tree?
jgodfrey | 2022-12-14 20:31
I know this may sound simplistic but are you sure you have got the capitalization correct? could it be for instance that the node is actually called Healthbar_p1
rather than HealthBar_p1
?
Gluon | 2022-12-14 20:44
Oh Sorry I just noticed this is about progressbar not healthbar but the same thing may apply.
Gluon | 2022-12-14 20:47
I thought about that possibility but i copied the name to make sure it’s correct
XCendoX | 2022-12-14 20:50
Related to Gluon’s confusion regarding which node we’re talking about here (and my exact same confusion above), are you sure you’re not confused here XCendoX?
That is, you’re error mentions ProgressBar
, but your code shows HealthBar_p1
. You fixed the path to HealthBar_p1
based on our above discussion, but is the error still with ProgressBar
, where you maybe have not fixed the path according to the above?
jgodfrey | 2022-12-14 20:52
Replying to jgodfrey, HealthBar_p1 is a ProgressBar that I have renamed
XCendoX | 2022-12-14 20:57
Thats your problem then, you have a reference somewhere in your code to a node which you have renamed.
Gluon | 2022-12-14 20:59
But, according to the error you posted, your code is attempting to reference a node named ProgressBar
. Or, are you saying that you renamed the node after posting, and that error no longer mentions ProgressBar
?
jgodfrey | 2022-12-14 21:00
If you have a look jgodfrey you will see it specifies line 34 but looking at the code posted there is no way that line of code is on line 34. I think that somewhere else in the code he still has a reference to progressbar but because he has renamed it this now throws an error.
Gluon | 2022-12-14 21:10
It’s because in the publication I have cut out part of the unnecessary code to avoid confusion, if you want I can put the entire code
XCendoX | 2022-12-14 21:23
So were you getting the same error before you renamed it?
Gluon | 2022-12-14 21:37
Yes, I don’t know why
XCendoX | 2022-12-14 23:47
Okay it has to be something to do with the node tree structure then, are you able to take a picture of your node structure and upload it somewhere we can take a look?
Gluon | 2022-12-14 23:53
Imgur: The magic of the Internet
Can you see it?
XCendoX | 2022-12-14 23:58
Yes. And, the offending script is on which node? Node2D
or player1
or something else?
jgodfrey | 2022-12-15 00:00
Yes I can thats great thanks, I assume this is the script on Player1 not on Node2d? If thats the case I would say that you should be looking at
get_node("HealthBar_p1").set_value(health)
but I know you said you tried that and got the same error yes?
Gluon | 2022-12-15 00:02
Yes, i tried that and it didn’t work and the script is in Player1
XCendoX | 2022-12-15 00:13
My objective is to assign the value of the progressbar to a variable
XCendoX | 2022-12-15 00:15
I just want to check while it didnt work was it the same error or a different error message that you get?
Gluon | 2022-12-15 00:16
It was the same error
Before
#Code
get_node("player1/HealthBar_p1").set_value(health)
#Error
get_node: (Node not found: "player1/HealthBar_p1" (relative to "/root/Player1Script").)
After
#Code
get_node("HealthBar_p1").set_value(health)
#Error
get_node: (Node not found: "HealthBar_p1" (relative to "/root/Player1Script").)
XCendoX | 2022-12-15 00:23
Hmm sorry I have created a little test program and just cannot recreate your issue. Can you please try something else for me?
try adding this at the top of your script
onready var HealthCapture = get_node("HealthBar_p1")
and then where you are trying to change the variable in the script try this instead
HealthCapture.health = 1
I would just like to see if you get the same error? Obviously change the 1 to whatever makes sense in your script.
Gluon | 2022-12-15 00:46
Oh sorry also add a script to healthbar and add a variable health as well or the above wont change anything. If this works you can add something like this into the script on healthbar
var health = 100
func _process(delta):
self.value = health
Gluon | 2022-12-15 00:49
Thanks for the help but i am not trying to change a variable, i want the value of a variable to control the percentage of the progress bar
XCendoX | 2022-12-15 03:15
Yes I appreciate that, the second script on the healthbar in my second message would make the change. But thats okay if you dont want to make the changes. I am not sure what is causing it though as I was unable to replicate your issue.
Gluon | 2022-12-15 08:23