how to add an ammo count in godot 4.2 using the hud? this is what i have so far, and it is not updating. this is in player.gd: func update_ammo_count() -> void: if hud: hud.text = "%d /" % current_ammo # update ammo count print("ammo updated") else: print("not found")
func primary() -> void: if !can_primary or reloading or current_ammo <= 0: return can_primary = false current_ammo -= 1 shoot_sound.play() gun_animated_sprite.play("shoot") primary_timer.start() if current_ammo <= 0: reload() update_ammo_count() #updates count after every shot
func reload() -> void: reloading = true await get_tree().create_timer(reload_time).timeout current_ammo = max_ammo reloading = false can_primary = true update_ammo_count() #updates count after reloading
It looks like you're trying to update the ammo count in your HUD in Godot 4.2, but it seems that the hud
variable is not properly set or the text is not being updated correctly. Here are a few steps to ensure that your ammo count updates correctly:
Ensure hud
is properly assigned: Make sure that the hud
variable is assigned to the correct UI element (like a Label
) in your scene. You can do this in the _ready()
function of your player script.
Check the format of the ammo count: Your current code is using a format string that may not be displaying the total ammo correctly. You might want to include the maximum ammo count in the display.
Update the HUD after every relevant action: Ensure that you call update_ammo_count()
after every action that changes the ammo count.
Here’s an updated version of your code with these considerations:
extends Node2D
# Assuming you have a Label node in your HUD scene
@onready var hud = $HUD/AmmoLabel # Adjust the path to your HUD label
var current_ammo: int = 10 # Example starting ammo
var max_ammo: int = 30
var can_primary: bool = true
var reloading: bool = false
var reload_time: float = 2.0 # Example reload time
var shoot_sound: AudioStreamPlayer # Assign your shoot sound here
var gun_animated_sprite: AnimatedSprite2D # Assign your animated sprite here
func _ready() -> void:
update_ammo_count() # Initialize the ammo count on ready
func update_ammo_count() -> void:
if hud:
hud.text = "%d / %d" % [current_ammo, max_ammo] # Update ammo count
print("ammo updated")
else:
print("HUD not found")
func primary() -> void:
if !can_primary or reloading or current_ammo <= 0:
return
can_primary = false
current_ammo -= 1
shoot_sound.play()
gun_animated_sprite.play("shoot")
primary_timer.start()
if current_ammo <= 0:
reload()
update_ammo_count() # Update count after every shot
func reload() -> void:
reloading = true
await get_tree().create_timer(reload_time).timeout
current_ammo = max_ammo
reloading = false
can_primary = true
update_ammo_count() # Update count after reloading
hud
variable correctly in the _ready()
function. Adjust the path to your HUD label as necessary.current_ammo / max_ammo
, which gives a clearer representation of the ammo status._ready()
function to ensure it displays the starting ammo when the game starts.hud
variable is pointing to the correct node in your scene tree.primary()
and reload()
functions are being called as expected during gameplay. You can add additional print statements to verify this.