How do I update the Text in my Label when Variable changes

Godot Version

Godot 4.2.1

Question

I want to update the Label whenever my Variables change, but I dont know how to do that.

This is my Script:

extends CanvasLayer

var speed = PlayerStats.speed
var damage = PlayerStats.damage
var current_jumps = PlayerStats.current_jumps
var jump_count = PlayerStats.jump_count 
var jump_velocity = PlayerStats.jump_velocity
var double_jump_velocity = PlayerStats.double_jump_velocity
var current_health = PlayerStats.current_health
var max_health = PlayerStats.max_health
var gravity_multiplier = PlayerStats.gravity_multiplier

func _process(delta):
	show_hp()
	show_jumps()

func show_hp():
	$Health/HP.text = str(current_health, "/", max_health, " HP")

func show_jumps():
	$Jumps/Jumps.text = str(current_jumps, "/", jump_count)

Based on the code shown it looks like your variables are not being updated after being initialized.

Are you setting them anywhere else, or only updating the values inside PlayerStats?

You could instead convert your show methods into set methods and call them when the PlayerStats values are updated.

e.g.

func update_hp( new_hp ):
	$Health/HP.text = str( new_hp, "/", max_health, " HP")

func update_jumps( new_current_jumps ):
	$Jumps/Jumps.text = str(new_current_jumps, "/", jump_count)

This has the added benefit of not requiring to be called every frame in _process.