Here’s a small, self-contained status bar for an RPG HUD: one icon per active status, a tooltip with the turns left, a pop-in when a status is added, and blinking when it’s about to expire. Tested in Godot 4.5.
class_name StatusBar
extends HBoxContainer
## One icon per active status. Tooltip shows turns left; the icon blinks when it is about to expire.
@export var icon_size := Vector2(24, 24)
var _icons := {} # StringName -> TextureRect
func set_status(id: StringName, icon: Texture2D, label: String, turns_left: int) -> void:
var rect: TextureRect = _icons.get(id)
if rect == null:
rect = TextureRect.new()
rect.texture = icon
rect.custom_minimum_size = icon_size
rect.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
rect.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
rect.mouse_filter = Control.MOUSE_FILTER_PASS # needed for the tooltip
rect.pivot_offset = icon_size / 2
add_child(rect)
_icons[id] = rect
rect.scale = Vector2.ZERO
rect.create_tween().tween_property(rect, "scale", Vector2.ONE, 0.15)
rect.tooltip_text = "%s (%d turn%s left)" % [label, turns_left, "" if turns_left == 1 else "s"]
_set_blink(rect, turns_left <= 2)
func clear_status(id: StringName) -> void:
var rect: TextureRect = _icons.get(id)
if rect:
_icons.erase(id)
rect.queue_free()
func _set_blink(rect: TextureRect, on: bool) -> void:
if rect.has_meta("blink"):
(rect.get_meta("blink") as Tween).kill()
rect.remove_meta("blink")
rect.modulate.a = 1.0
if on:
var tw := rect.create_tween().set_loops()
tw.tween_property(rect, "modulate:a", 0.35, 0.4)
tw.tween_property(rect, "modulate:a", 1.0, 0.4)
rect.set_meta("blink", tw)
Usage: call set_status() whenever a status is applied or ticks down, and clear_status() when it ends. If your battler emits signals like status_added(id, turns) / status_removed(id), connect them straight to these two functions.
Notes:
MOUSE_FILTER_PASSmatters: the default for a TextureRect ignores the mouse, so tooltips never show.- Tweens created with
rect.create_tween()die with the icon, so freeing it never leaves a looping tween behind. - Put the bar under each party member’s HP bar, and a smaller one over enemies’ heads.
If you need icons to test with, there are 12 free ones (status, elements, potions, weapons) here: Free RPG Icons. The full pack has 338. Disclosure: the icons are drawn procedurally by AI-assisted scripts; the code was written with AI help and tested by hand.