Day 117 — Creating an IDamageable Interface

Connor Fullarton
4 min readJul 10, 2021

--

Hey and welcome!

Today’s going to be a fun one because it’s going to be our first time looking into interfaces and how we can use them to really make our code a lot more efficient.

Back in our 2D Galaxy Shooter when we wanted the enemy to take damage from the player laser or the player itself we used an OnTriggerEnter in each different enemy script which would handle the damage the enemy would take. This is an alright method to do if you only have one enemy but more than 1 it’s getting a bit tedious to add in all this code we kept doing. The readability of the code would also drop and in general it could be a lot to think about each time we created a new enemy.

Alternatively there was the option to add the OnTriggerEnter to the Player and Laser objects instead but this is still a bit long and tedious as well and is demanding more mental power than we need.

With interfaces though all we need to do is check if the object we’re colliding with is inheriting the IDamageable interface and then run the appropriate Damage() method associated with it.

Let’s see how this functions in our current game though, go ahead and create a new script called IDamageable making sure that it starts with an uppercase i and then add in this code:

public interface IDamageable
{
// This is called an auto property
int Health { get; set; }
void Damage();
}

That’s honestly all you need here, pretty cool stuff! What this is going to do is force any script that inherits this to make sure that it includes a Health property and a Damage() method.

Head on over to your Skeleton script now or whichever enemy type you’ve been using to test with and add in this bit of code:

public class Skeleton : Enemy, IDamageable
{
public int Health { get; set; }
public override void Init()
{
base.Init();
Health = base.health;
}
public void Damage()
{
Health--;
anim.SetTrigger("Hit");
if (Health < 1)
{
Destroy(this.gameObject);
}
}

}

You can see at the start here that we’re inheriting IDamageable in the same way as we would inherit a class. The interesting part of this is that we can only inherit one class at a time but we can inherit as many interfaces as we want!

Next you can see that we have the Health property and Damage() method included in here, without these we would get errors in the console and won’t compile the code. Apart from that we just have some pretty standard code for taking damage.

There is a trigger in here for a new Hit animation for this enemy, you should be good and familiar with setting that up on your own.

The only thing different from the norm here is that we have a transition from the Any state to our Hit state and it’s here that we have the condition for the trigger.

Ok now for the last step, go ahead and create an Attack script and attach it to the Hit_Box object and add in this bit of code:

private bool _canDamage = true;
private void OnTriggerEnter2D(Collider2D other)
{
IDamageable hit = other.GetComponent<IDamageable>();
if (hit != null)
{
if (_canDamage == true)
{
hit.Damage();
_canDamage = false;
StartCoroutine(ResetDamageRoutine());
}
}
}
IEnumerator ResetDamageRoutine()
{
yield return new WaitForSeconds(0.5f);
_canDamage = true;
}

Now this is where we see the full use of our interface and it’s really cool. So what’s happening here is that when our hit box collides with another collider it’s checking to see if the object is inheriting IDamageable and then runs the Damage() method associated with it. Since the Damage() method is mandatory this won’t cause any issues, we’re not even caring what object we’re colliding with. All that we’re interested in here is if it’s inheriting IDamageable or not!

You should have something like the above working for you now. I’ll leave things here but there’s definitely going to be another upcoming article on Interfaces because of how handy they are and how much I’ve been liking them.

--

--

Connor Fullarton
Connor Fullarton

Written by Connor Fullarton

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.

No responses yet