Creating Complete Health System in Godot

This video was cut in to two pieces because of its length. First part describes how to create Health component which is then used in second part where I create HitBox and HurtBox components. Because of that I prepared two separated sections, one for each part. However I was creating this using one project, that’s why there is only one download section with final project from 2nd part of video. Parts of code are available for free for everyone, without login.

Health component

In this tutorial, I guide you through creating a modular health system with temporary immortality in Godot. Discover the essential role of a well-designed health system, responsible for managing player vitality, damage effects, and recovery mechanisms. Gain insights into building a foundation that ensures a dynamic and engaging gaming experience. Stay tuned for the next part, where we explore hitboxes and hurtboxes components for even more advanced gameplay mechanics.

Godot, GameDev, HealthSystem, Immortality, Scripting, IndieDev, 2DGames, ModularDesign

Health Component implementation – Health.gd
class_name Health
extends Node


signal max_health_changed(diff: int)
signal health_changed(diff: int)
signal health_depleted


@export var max_health: int = 3 : set = set_max_health, get = get_max_health
@export var immortality: bool = false : set = set_immortality, get = get_immortality

var immortality_timer: Timer = null

@onready var health: int = max_health : set = set_health, get = get_health


func set_max_health(value: int):
	var clamped_value = 1 if value <= 0 else value
	
	if not clamped_value == max_health:
		var difference = clamped_value - max_health
		max_health = value
		max_health_changed.emit(difference)
		
		if health > max_health:
			health = max_health


func get_max_health() -> int:
	return max_health


func set_immortality(value: bool):
	immortality = value


func get_immortality() -> bool:
	return immortality


func set_temporary_immortality(time: float):
	if immortality_timer == null:
		immortality_timer = Timer.new()
		immortality_timer.one_shot = true
		add_child(immortality_timer)
	
	if immortality_timer.timeout.is_connected(set_immortality):
		immortality_timer.timeout.disconnect(set_immortality)
	
	immortality_timer.set_wait_time(time)
	immortality_timer.timeout.connect(set_immortality.bind(false))
	immortality = true
	immortality_timer.start()


func set_health(value: int):
	if value < health and immortality:
		return
	
	var clamped_value = clampi(value, 0, max_health)
	
	if clamped_value != health:
		var difference = clamped_value - health
		health = value
		health_changed.emit(difference)
		
		if health == 0:
			health_depleted.emit()


func get_health():
	return health

HitBox and HurtBox components

In this sequel, I leverage the modular health system created in the previous tutorial and guide you through implementing hitboxes and hurtboxes in #Godot. We will use Health System created in last video so be sure to check it first. You can also use your own health system with this tutorial.


Download files

Download is available only for logged users.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *



Like the content?

Consider leaving a donation