The UI is not working properly with the pick-up system

Godot Version

Godot 4.4.1.

Question

Hi everyone, I’m encountering an issue with the UI system in the Godot engine. I have an item pick-up system that lets the player collect materials like logs around the level, but the UI I currently have only works for the first item the player collects. For example, if the player collects the second piece of log in the level, the number on the UI will stay as “x1” instead of becoming “x2”.

Here is the code for the UI scene:

extends Control
class_name HUD

@export var logs_label : Label
@export var stone_label: Label
@export var hop_label:Label
@export var potato_label:Label
@export var reminder: Label

func update_logs_label(number):
	logs_label.text = "x " + str(number)

func update_reminder():
	reminder.text = "Collect materials and ingredients!"

func update_inter():
	reminder.text = "Press E to make alcohol. Press Q to exit brewery."

func update_stone_label(number):
	stone_label.text = "x " + str(number)

func update_hop_label(number):
	hop_label.text = "x " + str(number)

func update_potato_label(number):
	potato_label.text = "x " + str(number)

So it sounds like your log scene is not calling the update_logs_label function. Where do you store the actual ‘logs collected’ value? Lets say you have collected 20 logs, where do you store 20?

This function just updates the label directly, which for a UI, is correct.

What code calls this function? If you are doing it via a signal I would guess your log scene is not connected to it. Or perhaps you are just adding 1 to 0 everytime the log is collected, so your UI update is updating 1 to 1 every time. Without any relevant code, who knows?

Thanks for responding. I think I’m encountering the second situation you said, which is that my UI is updating 1 to 1 every time. Here is the code for my log scene.

extends Area2D

@onready var player = $"/root/PlayerController1"
@onready var hud = get_node("../CanvasLayer/HUD")
var logs = 0

func _on_body_entered(body):
	if body is PlayerController1:
		logs += 1
		hud.update_logs_label(logs)
		queue_free()

Yeah, you cannot keep your log count in your log scene. The log counter is supposed to be counting the log scenes.

Just for now keep it in your UI.

extends Control
class_name HUD

@export var logs_label : Label

var logs_total: int = 0

func update_logs_label(logs_collected: int):
	logs_total += logs_collected
	logs_label.text = "x " + str(logs_total)

And then in your logs scene call it something like this:


func _on_body_entered(body):
	if body is PlayerController1:
		hud.update_logs_label(1)
		queue_free()

Then it should be fine.

1 Like

That works really well! Thank you!

1 Like