Understanding IDamageable Interfaces (part 1)
Creating the damage system using Interfaces
Since for the damage system we are NOT going to implement a damage method for each and every single enemy, and because it is not optimized to scan for every Trigger name object, we are going to use another method. This new method is very similar to Abstract classes but instead of being inherited, we are going to implement it. This damage system we are going to be using something similar to Abstract classes, this new method is called Interfaces.
Understanding IDamageable Interface:
Interfaces can be attached to any class in any amount, but classes can only inherit only ONE class only. That is the advantage that Interfaces have over Abstract classes.
The IDamageable interface basically simplifies and optimizes the workflow for damageable objects and or enemies. The Traditional way of creating a damage method would be to have our sword object scan whatever “Tag” it collides with. But this method becomes heavy once you introduce multiple of enemies and objects that need to be scanned. But for the Interface instead of scanning for a name tag or checking for colliders, we are going to check for a Interface script. For example the player sword can how swing and hit varies of objects within the scene, and if the object that collides with the sword has an IDamageable interface, then the Damage method is going to be invoked. It doesn’t require a compare Tag function or name scan.
To begin we are going to create a new C# Script which we will be our IDamageable Interface script. Instead of Using Monobehavior or even the class system, we are going to use the “Interface” system.
Because this is an Interface instead of a regular Class, we are unable to use “Fields” which are our regular variables and constants such as bools or Integers or floats. Instead we have to use “properties” which are similar to variables but they have something called “Getters” and “Setters”. These Get and Set ability lets us control what values can be retrieved and placed. For example, I can Get and Set the Health value for each enemy spawn.
Now that we have the basics of the Interface down, we can start applying them to our enemies. In the Moss Giant script we can start applying the Interface by adding a comma beside the Enemy and typing “IDamageable”.
You will noticed that immediately that there will be an error, this is because we are missing several components before this script can meet the conditions. Remember since Interfaces are contracts that must be fulfilled, classes that implement interfaces must abet by them.
In this example, the Moss Giant script is missing both the Health value and Damage Method.
And for those you caught on we now have two Health variables, one from the parent Enemy Script and one from the Interface script. To solve this issues we are going to basically assign the property value as our Abstract class value.
To test the Interface method is working or not, we will now use some Debug Log functions to create that feedback.
In the Skeleton Damage method we are going use a Debug.Log (“Hit”); to check if the sword made contact.
In the Attack Class we are going to get the IDamageable script and give it a handle called “hit”. Then using the if statement to check if the component isn’t null, trigger the Damage method.