WEBVTT

00:06.850 --> 00:07.750
Welcome.

00:07.750 --> 00:13.120
Now, being a top down game will be wanting to click on enemies to attack them.

00:13.120 --> 00:19.260
It would be nice if we could have some sort of highlight effect to show us which enemy is being targeted

00:19.270 --> 00:19.750
Now.

00:19.750 --> 00:25.540
The player controller class has ways to see what's beneath the mouse cursor, so this is a great class

00:25.540 --> 00:26.770
to handle this in.

00:26.800 --> 00:33.910
Now let's say that we have a goblin spear in the world and we hover over it with our mouse cursor.

00:33.910 --> 00:38.860
What we want is for that enemy to be highlighted with some sort of red outline.

00:38.860 --> 00:46.540
But if we hover over another enemy of another type, say a goblin slingshot, we want that enemy to

00:46.540 --> 00:47.440
highlight.

00:47.440 --> 00:54.280
Now, we could get the actor under our mouse cursor and say, cast it to one of these classes and highlight

00:54.280 --> 00:57.040
that actor if it's of the correct type.

00:57.040 --> 01:02.020
In fact, these enemies share the same parent class, the ora enemy class.

01:02.020 --> 01:08.690
So we could cast to that, but that limits us to only being able to highlight objects that derive from

01:08.690 --> 01:09.800
that class.

01:10.040 --> 01:16.040
On top of that, it makes the Ora player controller class dependent on the Ora enemy class.

01:16.040 --> 01:18.950
And we'd like our code to be more versatile than that.

01:18.980 --> 01:23.030
To achieve this versatility, we can create an interface class.

01:23.030 --> 01:27.950
Now the name isn't all that important, but I'll call this the enemy interface class.

01:27.950 --> 01:33.170
And when hovering over an actor, we can check to see if it implements this interface.

01:33.170 --> 01:37.310
If it does, we can then call an interface function on that interface.

01:37.310 --> 01:43.430
This gives us more flexibility because the Ora player controller doesn't need to know what should happen

01:43.430 --> 01:45.830
to an actor when hovering over it.

01:45.860 --> 01:51.560
All it knows is that if the actor implements the interface, it should call that interface function

01:51.560 --> 01:57.020
and the actor can override that function and implement it in any way it wants.

01:57.050 --> 02:01.340
Different enemy classes can have different functionality for the highlight function.

02:01.340 --> 02:06.770
We could add this interface to a barrel or a door and handle highlighting those objects differently.

02:06.770 --> 02:08.540
If we wanted to do that.

02:08.540 --> 02:14.330
In that case, the name enemy interface might be less appropriate for the name and it might be better

02:14.330 --> 02:18.350
to call it something else like target interface or highlight interface.

02:18.350 --> 02:25.160
But either way, an interface is the way to go if we'd like this type of versatility, and this is what

02:25.190 --> 02:28.820
experienced developers call good coding practice.

02:28.820 --> 02:30.650
So let's make our interface.

02:30.650 --> 02:32.480
So we'd like to make an interface.

02:32.480 --> 02:38.870
I'm going to go into my C plus plus classes folder into Ora public and I'll make the class here.

02:39.200 --> 02:43.730
And if I scroll all the way down to the bottom, we have unreal interface.

02:43.730 --> 02:48.110
We're going to use that and I'd like to keep my interface in its own folder.

02:48.110 --> 02:54.800
I'm going to call this folder interaction and I'll place this interface here and I'm going to call this

02:54.800 --> 02:58.250
enemy interface so I can create the class.

02:58.250 --> 03:04.340
I'm going to click No and go ahead and close the editor so I can now have my interface here and here

03:04.340 --> 03:06.370
it pops up in interaction.

03:06.380 --> 03:08.930
Here's enemy interface Dot H.

03:09.230 --> 03:16.010
Now enemy interface is going to have a couple of functions I'd like to call a function that will allow

03:16.040 --> 03:19.550
me to highlight the enemy when we hover over it.

03:19.550 --> 03:27.830
So I'm going to make a virtual function here, virtual void, and I'll call this simply highlight actor.

03:28.670 --> 03:36.800
Now, if I place an equals zero here, then this will now be a pure virtual function, which means that

03:36.800 --> 03:40.400
we will not provide a definition here in this class.

03:40.400 --> 03:45.800
This class is now considered abstract and must be derived from.

03:45.800 --> 03:49.730
That's how we're going to implement this interface in the enemy class.

03:49.730 --> 03:57.410
And any class that implements this interface will be required to override this pure virtual function

03:57.410 --> 03:59.450
and provide a definition for it.

03:59.510 --> 04:08.330
I'm going to make another one virtual void and call this Unhighlight actor and I'll make that also pure

04:08.360 --> 04:09.170
virtual.

04:09.170 --> 04:15.950
And that way we'll have two interface functions that we can call to highlight and unhighlight any actor

04:15.950 --> 04:17.600
that implements this interface.

04:17.750 --> 04:21.200
So let's implement this on the enemy.

04:21.200 --> 04:27.920
I'd like the Ora enemy class to implement it, so I'm going to go to Ora Enemy dot H and right here

04:27.920 --> 04:29.720
is where I can inherit.

04:29.720 --> 04:39.080
From this class I'm going to say public I enemy interface and remember the include for enemy interface

04:39.080 --> 04:44.870
must be here in the header file if we're going to inherit from that interface.

04:44.870 --> 04:48.890
Class writer just popped it up there for me automatically.

04:48.920 --> 04:56.030
Visual Studio won't do that, so you'll need to include the header file for this class now, because

04:56.030 --> 05:03.080
we're implementing this interface now, the Ora enemy class must override those pure virtual functions.

05:03.170 --> 05:05.150
I'm going to show you what happens if we don't do that.

05:05.150 --> 05:06.440
I'm going to go ahead and.

05:06.680 --> 05:07.460
Pile.

05:08.990 --> 05:10.700
Let's see what errors we get.

05:10.730 --> 05:12.440
Now here's what we get.

05:12.470 --> 05:16.040
Ora enemy cannot instantiate abstract class.

05:16.070 --> 05:23.630
Remember, as soon as we added a pure virtual function to enemy interface, it became an abstract class

05:23.630 --> 05:30.650
and you cannot instantiate an abstract class without overriding its pure virtual functions.

05:30.920 --> 05:33.290
So we have to override both of these.

05:33.320 --> 05:39.620
I'll go ahead and copy both of them and come back to Ora Enemy and create a public section.

05:40.610 --> 05:42.210
I'm going to paste them in.

05:42.230 --> 05:45.580
Now, we're not going to put equals zero here.

05:45.590 --> 05:53.000
What we do is we mark these with override instead to make it clear that we're overriding these and we

05:53.000 --> 05:55.640
can create some function definitions for them.

05:57.560 --> 06:00.650
So generate definitions by declarations.

06:00.680 --> 06:01.970
Click okay.

06:01.970 --> 06:05.720
And here we have highlight actor and Unhighlight actor.

06:06.350 --> 06:12.170
So now that our enemies implement this interface and we have the ability to call interface functions

06:12.170 --> 06:18.320
on any actors that we know implement this interface, we can now set up our player controller to see

06:18.320 --> 06:25.010
what actors exist under the mouse cursor and if they implement that interface, we can highlight them.

06:25.010 --> 06:26.690
So that'll be the next thing we do.

06:26.720 --> 06:31.640
For now, we can compile this and make sure we have no compiler errors.

06:31.640 --> 06:36.900
And with a successful build, we'll wrap up this video and continue in the next one.

06:36.920 --> 06:38.090
I'll see you soon.
