-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Unity Certified Programmer: Exam Guide
By :
The IActorTemplate interface is what we are using to prompt damage control, death, and scriptable object assets. The reason for using an interface such as this is that it ties general uses together between classes that inherit it.
A total of six classes will be using the IActorTemplate interface, which is as follows:
PlayerPlayerBulletPlayerSpawnerEnemyEnemyBulletEnemySpawnerThe following diagram shows the IActorTemplate interface with a partial overview of our game framework:
Figure 2.30 – IActorTemplate UML
Let's create our interface and explain its content along the way:
Assets/Scripts folder with the filename IActorTemplate.public interface IActorTemplate
{
int SendDamage();
void TakeDamage(int incomingDamage);
void Die();
void ActorStats(SOActorModel actorModel);
}The code we just entered looks like we have declared a class, but it acts fundamentally differently. Instead of using the class keyword, we enter interface followed by the name of the interface, IActorTemplate. It's not a requirement to start any interface name with an I, but it makes the script easily identifiable.
Within the interface, we make a list of methods that act like contracts to whichever class implements them. For example, the Player script that we'll create later on in the chapter inherits the IActorTemplate interface. The Player script must declare the function names from IActorTemplate or the Player script will throw an error.
Inside the scope of the interface, we declare methods without accessors (it doesn't require private or public at the beginning of each method). Methods also don't require any content in them (that is, they are empty bodies).
For more information about interfaces, check out https://learn.unity.com/tutorial/interfaces.
The last method in our interface is ActorStats, which takes a SOActorModel type. SOActorModel is a scriptable object that we are going to explain and create in the next section.