The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

I want to simply display the players current health a number through a label. I kept getting a null instance error by using get_node(), so I used godot's autoload to get the player variables and while I didn't get an error, now another instance of the player is spawned and the health displayed doesn't updated when changed.
There's likely a much simpler way I may have overlooked as i'm still a complete beginner.

UI code:

extends Node2D

var health = player.health

func _process(delta):
    $Label.text = "%s" % health
in Engine by (46 points)

Does the player have the variable health?

The player has a health variable and can have it's health updated just fine, I just can't display that variable for the UI.

I wouldn't use autoload for player if you have more than one instance. What error did you get with get_node()?

that was the issue, when I used autoload in instanced a completely separate unintentional instance of the player. And when I used get_node() it'd say health was null

Is the Label a child of the Player Node? Did you try using the "% Access as Scene Unique Name" option, i.e.

get_node("%Label").text = "%s" % health

Can you share a screenshot of you scene tree?

I tested it real quick and it seems to work for me without issues:

Small Tree of Nodes where Label is child of Player

I then moved the Label furhter up the tree, but used the "Access as Scene Unique Name" option and it still worked with the code I wrote above:

Full Tree where the Label is a child of a CanvasLayer

And when I used get_node() it'd say health was null

Show us the code where you used get_node().

I ended up finding an alternative and far easier method. By creating a global health variable through autoload I could simply call on it in the UI script like this

 extends Node2D

onready var num = $health
onready var fps = $fps

func _process(delta):
    fps.text = str(Engine.get_frames_per_second())

func _ready():
    var player_health = GlobalVar.get_player_health()
    update_health(player_health)

func _on_Player_health_updated(player_health): <---- connected to a signal in the player node
    update_health(player_health)

func update_health(new_value):
    num.text = str(new_value)

2 Answers

0 votes

health is being set at the start of the program. It's not being changed anywhere.

by (1,055 points)
0 votes

Just emit the signal in your Player node on _ready(). There is no need to use a GlobalVar.

export(int) var max_health = 100

var health = self.max_health

func _ready():
      emit_signal("update_health", self.max_health)

func _on_took_damage(damage):
      self.health -= damage
      emit_signal("update_health", self.health)
      # ... 
by (294 points)
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.