‫So we know that we need some components on our base -- class.
‫We know that we need a base mesh for the bottom part of the tank and the turret.
‫We also need a turret mesh for the top part.
‫We need a capsule to handle collision and we need a projectile spawn point.
‫That's going to be a scene component.
‫Now, once we've added these things to the base pod class, then anything that we create based on this
‫class will inherit those things.
‫So we could create a tank based on base -- and then we could go and create a tower also based on base
‫--.
‫And because they're based on base pod, they inherit these components.
‫The tank can have its own base mesh, its own turret mesh, its own capsule and its own projectile spawn
‫point.
‫And likewise, the tower will get its own base mesh turret mesh capsule component and projectile spawn
‫point as well.
‫And we'll see very soon how we can assign different meshes to each of these so they look different.
‫So we know that we need some components.
‫Now it's time to actually add some components to our base -- class.
‫Now, by default, we already have a public section, a protected section, and the second public section
‫down here.
‫There are various reasons to have multiple public sections and we'll talk about those later.
‫But for our components, we're going to create a private section.
‫As you've learned previously, it's good to have member variables be private, so that way they're only
‫accessible from within the class that owns them.
‫So we need some components.
‫The first one I'm going to add is the capsule.
‫So let's create a variable of type you capsule component.
‫Now this is going to be a pointer.
‫We're going to use pointers for the majority of our variables as long as they're not base types like
‫integer or float.
‫And as you know, that's because pointers are lightweight, they're just addresses and they allow us
‫to be more efficient.
‫So we have this U capsule component pointer and we're going to call this capsule comp short for capsule
‫component.
‫Now just above capsule component, we're going to add an unreal engine specific macro and it's you property
‫in all caps with its own parentheses.
‫You property is a way to make unreal engine aware of this variable so that it can participate in unreal
‫engines.
‫Reflection System.
‫The reflection system allows us to do things like expose this variable to blueprints and participate
‫in garbage collection and things like that.
‫So adding the you property is going to be something that will add to our components as we create them.
‫So we have this capsule component and because we're using the YOU capsule component type, we need to
‫let the compiler know about this type.
‫It doesn't know by default what a capsule component is.
‫Now normally to tell the compiler what a capsule component is, we would include a header file up here
‫at the top with our include directives if we go to the unreal engine documentation and search for you
‫capsule component, we can see right here where it says include the header file that we would need to
‫include to tell the compiler about the capsule component.
‫Now we could include this right here at the top and then our U.
‫Capsule component type would be recognized by the compiler and everything would be hunky dory.
‫But general, good practice when it comes to coding these things is to try to keep this header file
‫as small as possible.
‫Whenever we include a header file in this file, a program called the preprocessor takes all of the
‫contents of that header file, strips out some things like comments, and then literally pastes all
‫of the contents of that header file into this file.
‫The result is that this file gets bigger because it has the contents of that included file paste it
‫in here.
‫We only want to include things when we use them.
‫So how do we get away with not including the header file here in the base -- header?
‫Well, we can use something in C++ known as forward declaration.
‫We forward declare this u capsule component type using the class keyword like this.
‫Notice the compiler now shows this type as green whenever we forward declare a variable were then able
‫to get away with not including its header file.
‫It's telling the compiler that this is a type so it'll no longer give us IntelliSense errors, saying
‫that it doesn't recognize this as a type.
‫Now this is an incomplete type.
‫If we actually try to use this type either by accessing members like functions and variables or trying
‫to construct an object of this type, we would get an error because it's an incomplete type.
‫We would need the header file included wherever we use this type to do those things so we could forward
‫declare a pointer of a type to avoid needing the header file.
‫But it's important to note that we do need header files to inherit from particular classes.
‫Notice that this class inherits from a pond and the pond header file is included here at the top.
‫So inheritance does require the header file for the type we're inheriting from.
‫So let's discuss forward declaration and why we want to do it.
‫Let's say we have a class called Base Pon which needs to have a pointer variable of type you capsule
‫component.
‫Now you capsule component is in the capsule component header file.
‫Now we're actually going to use this type in based upon when we construct an object of the U.
‫Capsule component type.
‫So in, based upon all we really need is to have a pointer of type U capsule component.
‫Now the truth is based upon H doesn't actually need the implementation details of the capsule component
‫class.
‫In other words, it doesn't need to know how the functions are defined or even how big the capsule component
‫class is.
‫A capsule component pointer is the same size as any other pointer.
‫It's just an address.
‫Now in the base plan class.
‫We want to construct the capsule component.
‫For this reason, we need to know the size of a capsule component in order to lay out the memory necessary
‫for creating this object.
‫So while the capsule component header is not strictly needed in base Ponca It's absolutely needed in
‫base.
‫Porkpie So good programming practice is to only include headers where you actually need them so we can
‫get away with declaring a pointer of type U capsule component pointer in base product H and then include
‫capsule components where we need the implementation details because we're constructing a capsule component
‫and we need to know the size of a.
‫Capsule component in memory.
‫Now let's discuss the consequences of including the header file in base pond FX instead.
‫As we've seen, we can absolutely do this and you may be thinking, well, we're including it in base
‫pont CP Anyway, so what difference does it make if we include it in one file versus another?
‫Now here's where the problem comes in.
‫Header files are designed to be included by other files.
‫So let's say that we have Base Pond H and we've included capsule components in it.
‫And then further down the road we have some other class and we're going to have a base pond pointer
‫in some other class, just like we had a capsule component pointer in base pod.
‫Now, rather than going through the trouble of forward declaring the base pond pointer and some other
‫class, we're just going to declare it as normal and include base pawn in some other class.
‫So now not only does Base Pond H include the capsule component header, which results in more code being
‫copied into that header file.
‫Now some other class has base upon, which includes capsule component.
‫So all of the content and capsule components is also inside of some other class H and it's not even
‫needed there.
‫Some other class that H only needs to know about base upon it doesn't care about capsule component.
‫Now say we continue this pattern and create some other class called yet another class and yet another
‫class may need to have a pointer of type some other class now rather than forward declaring.
‫We just include some other class H in yet another class H and now inside of yet another class H.
‫Even though we don't even need the header for base pond or capsule component H, we got it in yet another
‫class because some other class includes those header files inside of it.
‫So we have copies of all of the contents of capsule components and based upon H just stuck here in yet
‫another class, bloating up that file and increasing compile times all because we didn't forward declare.
‫So instead the better thing to do is include header files where they're actually needed.
‫So if we take capsule components and only include it in based upon CPP, then later down the road when
‫we create some other class and actually need to use the base pod, we can include the header there and
‫base upon does not include capsule components, so all that extra code is not included in some other
‫class.
‫And then when we go and create yet another class which needs some other class, we can include some
‫other class that h in yet another class that CPP where it's actually needed and then yet another class
‫that H does not get filled up with the contents of other header files that it doesn't need.
‫So when classes have dependencies like this based upon depends on capsule component, it's best to keep
‫those dependencies to a minimum.
‫So I'm going to challenge you to create a compilation error.
‫We shouldn't be afraid of compilation errors.
‫They're just a normal part of the development process.
‫So we're going to generate one intentionally go back and remove the class keyword and compile without
‫forward declaring capsule comp.
‫Now when I say compile, I mean you can compile from Visual Studio code, just a regular C++ compile.
‫You've learned how to do that, or you can simply save your file and Visual Studio code and initiate
‫live coding.
‫Either one of those is fine and once we go through this together, I'll show you what happens with both
‫methods.
‫If you use live coding, of course you're going to be looking at the generated output in the live coding
‫window as opposed to the output that you would see in Visual Studio code.
‫Now spoiler, you're going to get a compilation error, look at the compilation error message, see
‫what it's telling you.
‫This is just a familiarize ourselves with the errors that we generate.
‫Then after this, go back and forward, declare capsule comp using the class keyword.
‫Once you've done that, compile again, you should get a successful compilation this time.
‫Pause the video and give this a try.
‫All right, so let's wreak some havoc on our code here.
‫I'm going to remove the class keyword.
‫Okay, so we've attempted to compile, and here is the output in the terminal.
‫The first thing we see is error and it gives us an error code and it says capsule comp is not a member
‫of a base pawn.
‫We have a note that says C declaration of a base pawn and then we see that we get other errors as a
‫result of this.
‫So oftentimes students will get confused when they attempt to compile and seem multiple errors.
‫So this goes to show that a single mistake can propagate a series of errors.
‫And that doesn't mean you have to go and fix a whole bunch of files.
‫The error could have a single origin here.
‫We know what the origin of the error is.
‫We need to forward declare.
‫Now let's see what happens if we use live coding instead of regular compilation and Visual Studio code.
‫So I'm going to go ahead and click the button to initiate live coding and right away in red I see build
‫failed and we see the same thing here.
‫Right here.
‫Capsule comp is not a member of a base pawn note.
‫See the declaration of a base pawn.
‫So we see the same thing here.
‫So either case we're going to see an error like this when we forget to forward declare.
‫So now we know what it looks like and now we're more informed and less afraid of these mysterious compilation
‫error messages.
‫So what we're going to do is fix this error by forward declaring you capsule component, and then we
‫can attempt to compile again.
‫And now we've compiled without any errors.
‫So now we see what it looks like when we forgot to forward declare a variable.
‫Keep that in mind when you get compiler errors in the future.
‫So in summary, we discussed forward declaration when creating a new pointer and we learned that you
‫should only include what you use in the CP file and we should include as little as possible in the H
‫file.
‫We know that we don't need the header file to declare a new pointer as long as we forward declare and
‫that we do need the header file if we're going to be constructing an object of a particular type.
‫And we do need the header file to access members like functions and variables and we saw that we actually
‫do need the header file for inheritance for inheriting from a class.
‫We need that header file there.
‫So now that we know how to forward declare a type and we have our pointer capsule comp, we're ready
‫to actually construct a capsule component object and we'll do that in the next video.
‫I'll see you soon.