0 votes

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

How can I fix it?
Thank you.

in Engine by (14 points)

1 Answer

0 votes

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.

by (14,952 points)

enter image description here

now he should see himself

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"

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

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)

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.

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.