How to make an ARPG in godot 4 #13: Player health 1 | tutorial | zeldalike

MakerTech
30 Apr 202307:43

Summary

TLDRIn this tutorial, the creator demonstrates how to add player health mechanics to an action RPG using Godot 4. Starting with a basic health system, the player’s health decreases upon collision with an enemy hitbox and resets when it reaches zero. The tutorial also covers creating a visual health bar with heart icons using the HBoxContainer node, arranging the hearts dynamically, and positioning them on the screen. The video ends with a setup for updating the health GUI, with plans to expand on player death and respawn mechanics in the next episode.

Takeaways

  • πŸ˜€ Start by adding a `current_health` variable to the player script to track health.
  • πŸ˜€ Set the initial value of `current_health` to 3 and make it an integer variable.
  • πŸ˜€ Decrease the player's health by 1 whenever the player collides with a hitbox in the game.
  • πŸ˜€ Use a print statement to visualize the health decrease in the output window for debugging purposes.
  • πŸ˜€ Add a mechanism to reset the player's health to the maximum health when it falls below 0.
  • πŸ˜€ Create a new variable for `max_health` and make it an exported variable for easier testing and changes.
  • πŸ˜€ Ensure health resets to maximum value by setting `current_health` equal to `max_health` when health reaches 0.
  • πŸ˜€ Design the health display by creating two new scenes: one for the heart container and one for individual heart icons.
  • πŸ˜€ Use an `HBoxContainer` to arrange the heart icons horizontally in the heart container scene.
  • πŸ˜€ Place the heart icons in a `Panel` node, remove the background, and adjust the size for proper fit and positioning.
  • πŸ˜€ Add the heart container to the world scene using a `CanvasLayer` to ensure it stays on top of the game world and moves with the camera.
  • πŸ˜€ Adjust the heart container's position in the top-left corner by changing its anchor preset to the top-right in the layout options.

Q & A

  • What is the purpose of adding a 'current health' variable to the player script?

    -The 'current health' variable is used to track the player's health throughout the game. It is initially set to 3 to represent the player's starting health, and it decreases when the player collides with certain objects like a slime's hitbox.

  • Why is there a need to print the player's current health when it decreases?

    -Printing the player's current health helps the developer see if the health decrement is working correctly in real-time, making it easier to debug and track health changes during development.

  • What happens when the player's health drops below zero?

    -In the current script, when the player's health drops below zero, the player would normally die. However, the script is set to reset the player's health back to the maximum health value when it reaches zero, preventing the player from staying at negative health.

  • What is the purpose of adding a 'maximum health' variable?

    -The 'maximum health' variable is introduced to set an upper limit for the player's health. It allows the player to regain full health when they are hit while having zero health, which is crucial for game mechanics where the player should respawn with full health.

  • Why is the 'maximum health' variable made 'exported'?

    -The 'maximum health' variable is exported so that it can be easily modified in the Godot editor, allowing the developer to test different health values without modifying the script itself.

  • What does the 'unready' keyword do for the 'current health' variable?

    -The 'unready' keyword ensures that the 'current health' variable is not initialized until after the scene has finished loading, which prevents any errors or issues related to accessing the variable before the scene is fully set up.

  • How does the 'Hearts container' scene work?

    -The 'Hearts container' scene is a horizontal box (HBoxContainer) that automatically arranges its child nodes in a row. This is used to display the player's health visually through heart icons, and it allows the developer to easily adjust the spacing between hearts.

  • Why is the 'panel' node used in the heart scene?

    -The 'panel' node is used to create a background for each heart icon. It serves as a container for the heart sprite and allows for easier resizing and positioning of the heart within the GUI.

  • What role does the 'canvas layer' play in the world scene?

    -The 'canvas layer' ensures that the heart container (GUI) is drawn on top of all other game elements and remains fixed in place, even when the camera moves, allowing the health display to always be visible to the player.

  • How does the 'layout' feature help with positioning the heart container?

    -The 'layout' feature is used to adjust the anchor point and position of the heart container. By changing the anchor preset, the developer can move the heart container to different positions on the screen, such as the top right corner, which makes it easy to adapt the UI layout.

Outlines

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Mindmap

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Keywords

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Highlights

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Transcripts

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now
Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Godot 4Action RPGGame DevelopmentHealth SystemPlayer StatsGUI TutorialGame DesignHealth VisualizationGodot TutorialGame MechanicsGame GUI