Updating a Stat Interface

Godot Version

Godot 4.2

I have a stat sheet interface in my game that I want to update every time I equip an item. When I equip a helmet the output does show that the stat is increased but it is not shown on the interface.

For example:
defense.text = str(playerstats[“Current_Defense”]) shows a defense of 17 when loading the game but doesn’t change after equipping a piece of armor that adds 4 defense.

I attempted to duplicate the player stat resource but that didn’t work. Any help would be appreciated. Thanks!

A more complete example of your code would probably help. In which function did you include defense.text = str(playerstats[“Current_Defense”])?

extends Control

const playerstats = preload(“res://Stats and Classes/Player_StatManager.tres”)

@onready var hp = $HP/Label
@onready var mp = $MP/Label
@onready var stamina = $STAMINA/Label
@onready var strength = $STRENGTH/Label
@onready var intelligence = $INTELLIGENCE/Label
@onready var agility = $AGILITY/Label
@onready var defense = $DEFENSE/Label
@onready var magical_defense = $“MAGIC DEFENSE/Label”
@onready var critical_strike_chance = $“CRITICAL STRIKE CHANCE/Label”
@onready var hp_regen = $“HP REGEN/Label”
@onready var mp_regen = $“MP REGEN/Label”
@onready var stamina_regen = $“STAMINA REGEN/Label”
@onready var mp_cost_rate = $“MP COST RATE/Label”
@onready var skill_charge_rate = $“SKILL CHARGE RATE/Label”
@onready var recover_rate = $“RECOVERY RATE/Label”
@onready var item_recovery_rate = $“ITEM RECOVERY RATE/Label”
@onready var magic_reflection_rate = $“MAGIC REFLECTION RATE/Label”
@onready var physical_damage_negation_rate = $“PHYSICAL DAMAGE NEGATION RATE/Label”
@onready var magical_damage_negation_rate = $“MAGICAL DAMAGE NEGATION RATE/Label”
@onready var target_rate = $“TARGET RATE/Label”
@onready var experience_rate = $“EXPERIENCE RATE/Label”

Called when the node enters the scene tree for the first time.

func _ready():
hp.text = str(playerstats[“Current_Health”])
mp.text = str(playerstats[“Current_Mana”])
stamina.text = str(playerstats[“Current_Stamina”])
strength.text = str(playerstats[“Current_Strength”])
intelligence.text = str(playerstats[“Current_Intellect”])
agility.text = str(playerstats[“Current_Agility”])
defense.text = str(playerstats[“Current_Defense”])
magical_defense.text = str(playerstats[“Current_Magic_Defense”])
critical_strike_chance.text = str(playerstats[“Current_Critical_Rate”])
hp_regen.text = str(playerstats[“Current_HP_Regeneration_Rate”])
mp_regen.text = str(playerstats[“Current_MP_Regeneration_Rate”])
stamina_regen.text = str(playerstats[“Current_Stamina_Regeneration_Rate”])
mp_cost_rate.text = str(playerstats[“Current_Mana_Cost_Rate”])
skill_charge_rate.text = str(playerstats[“Current_Special_Charge_Rate”])
recover_rate.text = str(playerstats[“Current_Recovery_Rate”])
item_recovery_rate.text = str(playerstats[“Current_Item_Recovery_Rate”])
magic_reflection_rate.text = str(playerstats[“Current_Magic_Reflection”])
physical_damage_negation_rate.text = str(playerstats[“Current_Physical_Damage_Negation_Rate”])
magical_damage_negation_rate.text = str(playerstats[“Current_Magical_Damage_Negation_Rate”])
target_rate.text = str(playerstats[“Current_Target_Rate”])
experience_rate.text = str(playerstats[“Current_Experience_Boost_Rate”])

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(delta):
pass

You are assigning text to your labels in the _ready() function.
The _ready() function runs once when the scene is added to the tree.
So when you add the item to the players inventory the function has already run.

When a players stats have changed you have to update the text of the label.

Please use the code tag </> when posting code.

Right this code will only update the display on _ready() when the game starts. Each stat is being converted into a string and copied into the label’s text once and only at the start of the game. Let’s move it to a update_display function then call that when we update the playerstats, maybe using the changed signal resources have?

func _ready() -> void:
	update_display()
	playerstats.changed.connect(update_display)

func update_display() ->:
	hp.text = str(playerstats[“Current_Health”])
	mp.text = str(playerstats[“Current_Mana”])
	stamina.text = str(playerstats[“Current_Stamina”])
	strength.text = str(playerstats[“Current_Strength”])
	intelligence.text = str(playerstats[“Current_Intellect”])
	agility.text = str(playerstats[“Current_Agility”])
	defense.text = str(playerstats[“Current_Defense”])
	magical_defense.text = str(playerstats[“Current_Magic_Defense”])
	critical_strike_chance.text = str(playerstats[“Current_Critical_Rate”])
	hp_regen.text = str(playerstats[“Current_HP_Regeneration_Rate”])
	mp_regen.text = str(playerstats[“Current_MP_Regeneration_Rate”])
	stamina_regen.text = str(playerstats[“Current_Stamina_Regeneration_Rate”])
	mp_cost_rate.text = str(playerstats[“Current_Mana_Cost_Rate”])
	skill_charge_rate.text = str(playerstats[“Current_Special_Charge_Rate”])
	recover_rate.text = str(playerstats[“Current_Recovery_Rate”])
	item_recovery_rate.text = str(playerstats[“Current_Item_Recovery_Rate”])
	magic_reflection_rate.text = str(playerstats[“Current_Magic_Reflection”])
	physical_damage_negation_rate.text = str(playerstats[“Current_Physical_Damage_Negation_Rate”])
	magical_damage_negation_rate.text = str(playerstats[“Current_Magical_Damage_Negation_Rate”])
	target_rate.text = str(playerstats[“Current_Target_Rate”])
	experience_rate.text = str(playerstats[“Current_Experience_Boost_Rate”])

However the changed signal might not work for you. In this case let’s just call the update_display() function when equipping items.

It’s still not working. Perhaps i’m calling the function incorrectly. Should I instantiate it? I used var CharacterStatsheet = load(“res://Equipment/character_statsheet.gd”) for the variable followed by CharacterStatsheet.update_display(). When I tried instantiating it before it returned all the values of the resources to null.

Could you post how you are updating the stats when equipping new items? Seems like you are loading the script and need to create a reasource or load a resource directly.

```
paste code with code formatting too please
```

paste code with code formatting too please

</>
extends Node

const playerstats = preload(“res://Stats and Classes/Player_StatManager.tres”)

func apply_equipment_stats(item):
match item.serial_number:
“0001”:
playerstats.Current_Defense += 4
print(“Player defense:”, playerstats.Current_Defense)

func lower_equipment_stats(item):
match item.serial_number:
“0001”:
playerstats.Current_Defense -= 4
print(“Player Defense:”, playerstats.Current_Defense)

I think Im using the code formatting correctly. Thanks for the help so far!

on code formatting, close the </> button in the editor will make a block for you to paste code in it will look like this when you click the button on a new line

```
type or paste code here
```

Then you just paste your code with ctrl+v

type or paste code here
1 Like
extends Node

const playerstats = preload(“res://Stats and Classes/Player_StatManager.tres”)

func apply_equipment_stats(item):
match item.serial_number:
“0001”:
playerstats.Current_Defense += 4
print(“Player defense:”, playerstats.Current_Defense)

func lower_equipment_stats(item):
match item.serial_number:
“0001”:
playerstats.Current_Defense -= 4
print(“Player Defense:”, playerstats.Current_Defense)

Thanks again!

Try calling update_display when updating equipment stats, I am not sure where you would get a reference to the interface, but here is an example

apply_equipment_stats(sword)
$StatsDisplay.update_display()

Thanks for your help. I ended up getting rid of the resources for my stats and used variables for my stats on my character script and used signals to update the label.

Thanks again!