Health in Games
Let’s talk about an important concept that you’ll find in countless games today, namely Health. Health -sometimes abbreviated to HP for Health Points– is common. It’s normally an attempt to quantify how healthy a character is, such as the player or an NPC. In most cases, HP is represented by a progress bar spanning from 0 to 100. 100 means full health and 0 is death. Naturally, getting damaged by bullets, punches, lava pits, spikes and other dangers reduces your health. Whereas collecting goodies like hearts, turkeys, burgers, and medi-kits will increase- or maybe even fully restore- your health. It’s that simple. Or is it? Well. As we’ll see in this article, coding health can be deceptively simple. Simple to make, but it’s easy to overlook optimisation. We’ll be working here with the Unity engine- but the same ideas apply across all tools. So even if you’re working in Godot, GameMaker or Unreal, you’ll encounter similar problems. So let’s create a general purpose health script that can be added to any in-game character. Both the Player and NPCs- and even objects like swords and shields.

In this article you’ll learn to…

  • Design and Plan Health Scripts in C#
  • Use C# Properties for Validating Health
  • Detect Health Change and Death Conditions
  • Use Events for Responding to Health Changes
  • Add flourishes, like Health Regeneration

Requirements

Unity 2019 or above, C# Basic Knowledge

Understanding C# Properties

We all want our code to be super-accurate. For example, if the player’s health changes during gameplay. we want to know by how much and when. We don’t want to know by roughly how much, or 3 seconds after it all happened. That’s too little information and too late. We want an immediate and direct picture of a character’s health. So, it seems tempting to dump all health checking code inside an Update event. After all, Update happens every frame, right? We want to capture changes as soon as they happen- and so Update is perfect. But doing this is a big mistake. Really big.

Let’s fix this problem right away by learning C# Properties. They act like gateways to variables. We can create a variable (like HealthPoints) and have a gatekeeper watch for changes to them. When changes happen, we get notified. And we can even validate the changes to accept or reject them, protecting our variables from being assigned invalid values. Inappropriate values for health might be: -10 or 909979. We don’t want that and C# Properties are what’s needed to fix it. Check out our video above to learn about C# Properties. They’re really simple to learn.

Getting Started with Health

Okay. So you’ve learned about C# Properties. That’s great. Now we can apply them to creating a general purpose health script. It’s a script that you can attach to many things. This includes the player, NPCs and even inanimate objects- like swords, chairs and desks- to give them a durability quality. In making this script, we need to get away from using the Update function and instead rely on properties to detect changes to the health points.

Properties are great because they let us continue using variables as normal- in assignment and arithmetical statements, for example- but they now work like functions. We get to validate variable values, prevent invalid assignments, and even notify listeners when important events happen. Check out our YouTube video, as above, on creating a basic health script that uses C# Properties.

Move Further with Health

You’ve now got a solid Health Script foundation. You’ve seen how to make changes to health points easily using properties. This is much better than using the Update function to check values every frame. Perfect. Let’s make our Health Script better by supporting Events and Regeneration features.

Events are simply functions that run every time a key event happens. For Health, we’ll certainly want to run events for Health Change and Health Expired events. That is, whenever the health increases or decreases, and whenever the health reaches 0. On these events, we want to run potentially any kind of behaviour- perhaps different behaviours for different characters. The data type UnityEvent can help us do this!

In addition to events, we may want regeneration abilities. Specifically, for our health to generate continually, over time  back to its full state. This is easy to do, and we can use Time.deltaTime to achieve it. Take a look at our YouTube tutorial on making Better Health Scripts, as above.

Conclusion

Excellent. You’ve now created a Health Script that supports property based health points, events and regeneration. This script can be attached to the player, NPCS and even to inanimate objects for supporting any health or durability based concepts.

Share:
Written by Alan Thorn
Alan Thorn is a Co-Founder of BeIndie.Biz and is its main gamedev instructor. He’s a game-development generalist with a passion for free and open-source software, especially Blender, Godot and Inkscape. Alan has worked on 33 games over the past 20 years using many different tools. He’s written 30 books and presented 30 video courses on gamedev, covering lots of subjects. This includes: Unity, Blender, GIMP, Unreal, Godot, GameMaker, C# Programming, animation and tons more. Basically, he loves making games and teaching the process! Alan is currently the Head of Games for the BAFTA-winning National Film and Television school, London. He leads their innovation-focussed post-graduate course in Games Design and Development. Alan also founded indie-studio Wax Lyrical Games in 2010, which developed the award-winning and best-selling adventure game Baron Wittard: Nemesis of Ragnarok. Alan enjoys sunshine, philosophy, good humour, mathematics and being indie! His personal site is: https://www.alanthorngames.com