Creating a Healing System for an RPG Player Character in Godot 2D

Welcome back to our game development blog! Today, we’re building on our previous tutorial about creating a health system by adding a healing system for an RPG player character in Godot 2D. A healing system is crucial for balancing gameplay, providing players with opportunities to recover and continue their adventure. In this tutorial, we’ll cover implementing healing items, healing spells, and updating the health UI.


Setting Up the Project


If you followed our health system tutorial, you should already have a project with a basic player character and health system. If not, follow these steps to set up your project:


1. Create a New Project

Open Godot and create a new project. Name it something like “RPGHealingSystem”.

2. Set Up the Scene

Create a new 2D scene and add a KinematicBody2D node for the player.

Add a Sprite and CollisionShape2D to represent the player visually and physically.

Save the scene as Player.tscn.

3. Health System Script

Ensure your player script includes health management:


extends KinematicBody2D


# Health Variables

var max_health = 100

var current_health = 100


func _ready():

    update_health_ui()


func take_damage(amount):

    current_health -= amount

    if current_health < 0:

        current_health = 0

    update_health_ui()

    check_if_dead()


func heal(amount):

    current_health += amount

    if current_health > max_health:

        current_health = max_health

    update_health_ui()


func check_if_dead():

    if current_health == 0:

        die()


func die():

    # Handle player death (e.g., restart level, show game over screen)

    queue_free()


func update_health_ui():

    $HealthUI.value = current_health


Step 1: Creating Healing Items


First, we’ll create a healing item that the player can collect to restore health:


1. Create a Healing Item Scene

Create a new 2D scene with a KinematicBody2D node. This will be our healing item.

Add a Sprite and CollisionShape2D to represent the item.

Save the scene as HealingItem.tscn.

2. Healing Item Script

Attach a new script to the KinematicBody2D node and handle collision with the player:


extends KinematicBody2D


var heal_amount = 20


func _on_HealingItem_body_entered(body):

    if body.name == "Player":

        body.heal(heal_amount)

        queue_free()


3. Connect Signals

Connect the body_entered signal of the CollisionShape2D node to the script:


func _ready():

    $CollisionShape2D.connect("body_entered", self, "_on_HealingItem_body_entered")


Step 2: Creating Healing Spells


Next, we’ll implement a healing spell that the player can use to restore health:


1. Add Spell Logic to Player Script

In the Player.gd script, add a function to cast a healing spell:


var spell_cooldown = 2.0

var time_since_last_spell = 0.0

var heal_amount = 30


func _ready():

    update_health_ui()

    set_process(true)


func _process(delta):

    time_since_last_spell += delta


func cast_healing_spell():

    if time_since_last_spell >= spell_cooldown:

        heal(heal_amount)

        time_since_last_spell = 0.0


2. Binding the Spell to Input

Bind the healing spell to a key press. Open the Input Map in the Project Settings and add a new action called cast_healing_spell. Assign a key, such as H, to this action.

In the _process function, check for the key press:


func _process(delta):

    time_since_last_spell += delta

    if Input.is_action_just_pressed("cast_healing_spell"):

        cast_healing_spell()


Step 3: Updating the Health UI


To ensure the player can see their health status, we’ll update the health UI:


1. Create a UI Scene

Create a new scene with a Control node as the root. Add a TextureProgress node to represent the health bar.

Save the scene as HealthUI.tscn.

2. Configure the Health Bar

Set the Min value to 0, the Max value to 100, and the Value to 100 to match the player’s initial health.

Assign textures for the Progress and Under properties.

3. Instance the Health UI in the Main Scene

Open the main scene and instance the HealthUI.tscn scene. Make sure to reference it in the player script:


onready var health_ui = get_node("/root/Main/HealthUI/TextureProgress")


func update_health_ui():

    health_ui.value = current_health


Step 4: Testing the Healing System


Finally, let’s test the healing system to ensure everything works correctly:


1. Place the Player and Healing Items

Instance the Player.tscn and HealingItem.tscn scenes in the main scene.

2. Run the Game

Run the game and test collecting healing items and using the healing spell. Check if the health bar updates accordingly and if the player’s health is restored properly.


Conclusion


You’ve successfully implemented a healing system for an RPG player character in Godot 2D! This system includes healing items and spells, providing players with multiple ways to recover health. From here, you can expand the system by adding different types of healing items, cooldown management, or visual and sound effects.


Stay Connected


Follow our blog for more tutorials, tips, and insights into game development with Godot and other engines. Share your experiences and any challenges you encounter in the comments below. Let’s continue to learn and create amazing games together!


Summary of Steps


1. Set Up the Project: Create a new project and set up the player scene with a health system.

2. Create Healing Items: Implement collectible items that restore health.

3. Create Healing Spells: Add functionality for the player to cast healing spells.

4. Update the Health UI: Ensure the health UI updates when health changes.

5. Test the Healing System: Place the player and healing items in the scene and test.


Happy developing!

Comments

Popular posts from this blog

Creating 2D Character Movement in Godot: A Step-by-Step Tutorial

Exploring “Papers, Please”: A Tale of Morality and Bureaucracy