Error that I do not understand where it is

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Io

Good morning,
I was trying to make a 3D RPG game with Godot Engine, but this error comes up:

How can I fix it?
Thank you.

:bust_in_silhouette: Reply From: jgodfrey

Your player variable is null in when the player.translation code is executed. The screenshot you posted doesn’t show how that’s being set, so I can’t say much more…

So, find out why player is null. If you can’t find the problem on your own, post the script itself (not a screenshot) in a follow-up comment for more input.

basically what we need to know is what are you doing to reference the player in the player variable. That’s probably the source of the problem.

Millard | 2020-11-29 04:32

ok, but how do i insert the script?

Io | 2020-11-29 09:22

ok, but how do i insert the script?

  • Copy the script to the clipboard
  • Add a new Comment to this message
  • Paste the copied script
  • Select the just-pasted text
  • Press the { } button in the editor’s toolbar to format the text as code

jgodfrey | 2020-11-29 15:06

extends KinematicBody

# stats
var curHp : int = 3
var maxHp : int = 3
# attacking
var damage : int = 1
var attackDist : float = 1.5
var attackRate : float = 1.0
# physics
var moveSpeed : float = 2.5

# vectors
var vel : Vector3 = Vector3()
# components
onready var timer = get_node("Timer")
onready var player = get_node("/root/MainScene/Player")

func _ready ():
	timer.wait_time = attackRate

func _on_Timer_timeout():
	if translation.distance_to(player.translation) <= attackDist:

# warning-ignore:unused_argument
func _physics_process (delta):
	var dist = translation.distance_to(player.translation)
	# if we're outside of the attack distance, chase after the player
	if dist > attackDist:
		# calculate the direction between us and the player
		var dir = (player.translation - translation).normalized()
		vel.x = dir.x
		vel.y = 0
		vel.z = dir.z
		# move towards the player
		vel = move_and_slide(vel, Vector3.UP)

func take_damage (damageToTake):
	curHp -= damageToTake
	if curHp <= 0:
# called when our health reaches 0
func die ():

Io | 2020-11-29 15:25

So, this line is failing to find the requested node.

onready var player = get_node("/root/MainScene/Player")

That means the path you’ve defined there isn’t correct for some reason. If that doesn’t help you to find the problem, you need to post the layout of your scene tree (either as formatted text or as an image)…

jgodfrey | 2020-11-29 15:50

ok thanks, I think I managed to solve it, but now it gives me another error.
“Invalid set index ‘text’ (on base: ‘null instance’) with value of type ‘String’.”
and this is the script

extends Control

onready var healthBar = get_node("HealthBar")
onready var goldText = get_node("GoldText")

# called when we take damage
func update_health_bar (curHp, maxHp):
	healthBar.value = (100 / maxHp) * curHp

# called when our gold changes
func update_gold_text (gold):
	goldText.text = "Gold:" + str(gold)

how do i fix it?
Thank you.

Io | 2020-11-29 16:31

So, that’s basically the same problem. In this line of code:

goldText.text = "Gold:" + str(gold)

Your goldText variable is null.

That’s being set here:

onready var goldText = get_node("GoldText")

So, again, that requested get_node path isn’t correct for some reason…

jgodfrey | 2020-11-29 16:33

thanks, but i couldn’t solve it, this is my node tree
enter image description here

where is the problem and how do i solve it?
Thank you

Io | 2020-11-29 17:12

ok, somehow your image failed, theres no link there.

Millard | 2020-11-29 17:14

Your script implies that the GoldText node is an immediate child of the node that has the script associated with it. That must not be the case…

jgodfrey | 2020-11-29 17:18

now he should see himself

Io | 2020-11-29 17:27

It looks like the script is on the UI node and GoldText is a child of HealthBar. If that’s the case, the path to the GoldText node would be "HealthBar/GoldText"

jgodfrey | 2020-11-29 17:44

now I wrote the script

extends Control

onready var healthBar = get_node("HealthBar")
onready var GoldText = get_node("HealthBar/GoldText")

# called when we take damage
func update_health_bar (curHp, maxHp):
	healthBar.value = (100 / maxHp) * curHp

# called when our gold changes
func update_gold_text (gold):
	gold.text = "HealthBar/GoldText" + str("HealthBar/GoldText")

but it comes out written

“Invalid set index ‘text’ (on base: ‘int’) with value of type ‘String’.”

Io | 2020-11-29 17:59

It seems like you have some misunderstandings of fundamental concepts in Godot. Really, you’re not likely to get too far without taking the time to understand the basics. I’d suggest you watch some tutorials to build a solid foundation because debugging your game one error at a time in a single forum question isn’t very efficient for anyone involved…

Anyway, I assume you’re trying to set the text of your GoldText node to the value that’s passed in as gold. If that’s the case, you want this:

func update_gold_text (gold):
    GoldText.text = str(gold)

jgodfrey | 2020-11-29 18:11

To provide a little more assistance…

  • You have a label node in your tree at the path "HealthBar/GoldText".
  • You’ve stored a reference to that node via onready var GoldText = get_node("HealthBar/GoldText")
  • That reference is stored in a variable called GoldText
  • Now, that GoldText variable effectively represents that Label node
  • So, you can set the displayed text in a Label by storing a value in its text property
  • So, the latest code I suggested does just that…
  • GoldText.text = str(gold)

You pass a value into your update_gold_text function (that I assume is an integer). You can’t put that directly into the text property, as that property expects a string, not an integer. So, that str(gold) call takes the integer value (gold) and converts it a string - so it’s the proper data type to store in the text property of the Label.

jgodfrey | 2020-11-29 19:05