Welcome.
Now, being a top down game will be wanting to click on enemies to attack them.
It would be nice if we could have some sort of highlight effect to show us which enemy is being targeted
Now.
The player controller class has ways to see what's beneath the mouse cursor, so this is a great class
to handle this in.
Now let's say that we have a goblin spear in the world and we hover over it with our mouse cursor.
What we want is for that enemy to be highlighted with some sort of red outline.
But if we hover over another enemy of another type, say a goblin slingshot, we want that enemy to
highlight.
Now, we could get the actor under our mouse cursor and say, cast it to one of these classes and highlight
that actor if it's of the correct type.
In fact, these enemies share the same parent class, the ora enemy class.
So we could cast to that, but that limits us to only being able to highlight objects that derive from
that class.
On top of that, it makes the Ora player controller class dependent on the Ora enemy class.
And we'd like our code to be more versatile than that.
To achieve this versatility, we can create an interface class.
Now the name isn't all that important, but I'll call this the enemy interface class.
And when hovering over an actor, we can check to see if it implements this interface.
If it does, we can then call an interface function on that interface.
This gives us more flexibility because the Ora player controller doesn't need to know what should happen
to an actor when hovering over it.
All it knows is that if the actor implements the interface, it should call that interface function
and the actor can override that function and implement it in any way it wants.
Different enemy classes can have different functionality for the highlight function.
We could add this interface to a barrel or a door and handle highlighting those objects differently.
If we wanted to do that.
In that case, the name enemy interface might be less appropriate for the name and it might be better
to call it something else like target interface or highlight interface.
But either way, an interface is the way to go if we'd like this type of versatility, and this is what
experienced developers call good coding practice.
So let's make our interface.
So we'd like to make an interface.
I'm going to go into my C plus plus classes folder into Ora public and I'll make the class here.
And if I scroll all the way down to the bottom, we have unreal interface.
We're going to use that and I'd like to keep my interface in its own folder.
I'm going to call this folder interaction and I'll place this interface here and I'm going to call this
enemy interface so I can create the class.
I'm going to click No and go ahead and close the editor so I can now have my interface here and here
it pops up in interaction.
Here's enemy interface Dot H.
Now enemy interface is going to have a couple of functions I'd like to call a function that will allow
me to highlight the enemy when we hover over it.
So I'm going to make a virtual function here, virtual void, and I'll call this simply highlight actor.
Now, if I place an equals zero here, then this will now be a pure virtual function, which means that
we will not provide a definition here in this class.
This class is now considered abstract and must be derived from.
That's how we're going to implement this interface in the enemy class.
And any class that implements this interface will be required to override this pure virtual function
and provide a definition for it.
I'm going to make another one virtual void and call this Unhighlight actor and I'll make that also pure
virtual.
And that way we'll have two interface functions that we can call to highlight and unhighlight any actor
that implements this interface.
So let's implement this on the enemy.
I'd like the Ora enemy class to implement it, so I'm going to go to Ora Enemy dot H and right here
is where I can inherit.
From this class I'm going to say public I enemy interface and remember the include for enemy interface
must be here in the header file if we're going to inherit from that interface.
Class writer just popped it up there for me automatically.
Visual Studio won't do that, so you'll need to include the header file for this class now, because
we're implementing this interface now, the Ora enemy class must override those pure virtual functions.
I'm going to show you what happens if we don't do that.
I'm going to go ahead and.
Pile.
Let's see what errors we get.
Now here's what we get.
Ora enemy cannot instantiate abstract class.
Remember, as soon as we added a pure virtual function to enemy interface, it became an abstract class
and you cannot instantiate an abstract class without overriding its pure virtual functions.
So we have to override both of these.
I'll go ahead and copy both of them and come back to Ora Enemy and create a public section.
I'm going to paste them in.
Now, we're not going to put equals zero here.
What we do is we mark these with override instead to make it clear that we're overriding these and we
can create some function definitions for them.
So generate definitions by declarations.
Click okay.
And here we have highlight actor and Unhighlight actor.
So now that our enemies implement this interface and we have the ability to call interface functions
on any actors that we know implement this interface, we can now set up our player controller to see
what actors exist under the mouse cursor and if they implement that interface, we can highlight them.
So that'll be the next thing we do.
For now, we can compile this and make sure we have no compiler errors.
And with a successful build, we'll wrap up this video and continue in the next one.
I'll see you soon.