Skip to content

Instantly share code, notes, and snippets.

@brettchalupa
Created April 10, 2023 13:23
Show Gist options
  • Save brettchalupa/cb42c399d82e2af549317f3fc35fb4c2 to your computer and use it in GitHub Desktop.
Save brettchalupa/cb42c399d82e2af549317f3fc35fb4c2 to your computer and use it in GitHub Desktop.
GDScript in Godot 4 @onready var Explained
extends Node2D
const MAX_HEALTH = 5
var health = MAX_HEALTH
@onready var health_bar = $HealthBar
@onready var health_label = $HealthLabel
func _ready() -> void:
update_health_ui()
health_bar.max_value = MAX_HEALTH
func update_health_ui():
set_health_label()
set_health_bar()
func set_health_label() -> void:
health_label.text = "Health: %s" % health
func set_health_bar() -> void:
health_bar.value = health
func _input(event: InputEvent) -> void:
if event.is_action_pressed("ui_accept"):
damage()
func damage() -> void:
health -= 1
if health < 0:
health = MAX_HEALTH
update_health_ui()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment