Skip to content

2.1. GDScript Code Example

Lucas Melo edited this page Oct 30, 2025 · 4 revisions

Basic Health System

# Create a health stat
# Default start: min=0, max=100, current=100
var health = StatPool.new()


## Health initialization and signal connection.
func _ready() -> void:
    health.min_value = 0
    health.max_value = 200
    health.value = 150

    health.value_changed.connect(_on_health_changed)
    health.depleted.connect(_on_player_died)
    health.fully_restored.connect(_on_resurrected)


## Example for how to use this plugin
func examples() -> void:
    # Take damage
    health.decrease(25)  
    # Heal
    health.increase(10)
    # will return 0.65 (65%)
    print(health.get_percentage())


func _on_health_changed(old_value, new_value, increased):
    health_bar.value = health.get_percentage() * 100


func _on_player_died():
    print("Game Over!")


func _on_resurrected():
    print("Respawn player")

Stamina System

# Default start: min=0, max=100, current=100
var stamina = StatPool.new()
var running: bool = false


## Drain stamina while running
func _process(delta):
    if Input.is_action_pressed("run") and not stamina.is_depleted():
        running = true
        stamina.decrease(30 * delta)
    else:
        running = false
    
    # Regenerate stamina
    if not running and not stamina.is_filled():
        stamina.increase(20 * delta)

Hunger System

var health := StatPool.new()
var hunger := StatPool.new()


## Signal connection
func _ready() -> void:
    hunger.depleted.connect(_on_starving)
    hunger.value = 50


## Eat food
func eat_apple():
    hunger.increase(25)


## Will die if is starving
func _on_starving(stat):
    health.deplete()

Clone this wiki locally