‫Hello and welcome.
‫In this lecture, we're going to be looking at a special function called the constructor that allows
‫us to give default values to properties in our class.
‫And we'll see when it's called in the editor.
‫Let's dive in and find out.
‫Now, with this trigger component, I would like to add a tick component function in here so that we
‫could check which components, which other actors are currently overlapping this trigger component and
‫we could activate if it is the right kind of actor.
‫The problem is, well, we'll see what the problem is.
‫Let's try and implement a tick function.
‫If we go over to the grab H to see what the declaration of a tick component function should look like,
‫it needs to be in the public section.
‫It's a virtual void, tick component, delta time.
‫There's lots of parameters, so let's just copy it.
‫Go over to the tick component header, add it in underneath our begin play and then we're going to copy
‫that definition, take it over into our trigger component.
‫CP And one thing that we'll notice is there's a couple of key words here that we can remove.
‫We can remove the virtual keyword.
‫That's generally a rule that we can follow.
‫We can also remove the override keyword.
‫These are things that don't need to be in the C++ file.
‫We need to add the class name.
‫So you trigger component colon.
‫Colon.
‫Then we replace our semicolon with some braces and we need to make the call to the super tick component.
‫So because that's quite a long call, I'm going to copy it from our grab a CP and paste it in to this
‫one.
‫Now what this is doing is it's essentially calling the parent's tick component function.
‫So the parent in this case being our box component.
‫So it's important to call that in case there is any functionality there that needs to be used and enabled
‫for this component to work.
‫If we don't do that, we're essentially bypassing any of that functionality and things can break.
‫So very important to always remember to call to super when we are implementing begin, play and tick
‫component.
‫Now let's copy our log message from Begin Play and put it in tick component.
‫And instead of saying tick component logging and say Tick component is ticking in here and we're going
‫to go ahead and compile and see whether that tick is actually working once the compiler has finished,
‫let's go ahead and hit play, open up our output log and.
‫Hmm, well, nothing seems to be happening now.
‫The reason for this is that optimization wise, they automatically and unreal most components have their
‫tick switched off.
‫So actors usually have it switched on by default, but components usually have it switched off.
‫So in fact, if we look at some of the other components we've got such as our move that we know ticking
‫does work for, you can see that at the top of the file there is this.
‫You move a function and it's got this primary component tick dot B can ever tick equals true if you
‫don't set this property on the class.
‫So this is a property of the class as a property of the classical primary component tick and it is a
‫struct that has a B can ever tick property within it.
‫So what we're doing here is we are setting this default value on the mover class when we construct it.
‫That's what this function is.
‫It's called a constructor and it is a function that gets called whenever we create a new mover component
‫to automatically set default values, we can set some default values over in the header file like we've
‫done with should move move time.
‫But alternative to doing it over in the header file is that we can actually set these properties in
‫the constructor.
‫Now I'd like you to notice the syntax here we go over into the move a header file.
‫You can see that a constructor is named exactly the same as the class.
‫So the you mover is the class name for our mover component and its constructor is also called you mover
‫notice.
‫It has no return type either.
‫It doesn't make sense to return anything because we are constructing a new object.
‫We are just making this class happen.
‫So all we need is access to the properties of this class and also notice that it is set to public.
‫So it's in the public section of our class definition.
‫So I'd like to challenge you to enable ticking for yourself.
‫So you're going to create a constructor in the header and keep bearing in mind that syntax that we've
‫seen both in the mover H and the grab a lot of H, you're going to enable ticking as we've seen again
‫in those other components in their CP files.
‫So just you can copy how that's being done.
‫You can log from the constructor as well to try and see when does it get called?
‫Is it like begin play?
‫Or does it get called before begin play or what?
‫Go and pause the video and have a go.
‫Kay.
‫Welcome back.
‫So we're going to go in to our trig components, which are going to create a new public section in here.
‫We're remembering the colon at the end and then we need to call it exactly the same as the class.
‫So that's not trigger component.
‫It's not you trigger it is you trigger component in full.
‫And then we don't have any arguments to this constructor, so we put just empty parentheses and then
‫a semicolon at the end of the line.
‫Now we can go over to the CPP file and we can make the implementation here.
‫Again, we don't need a return type.
‫So it's just you trigger component colon, colon, you trigger component a bit repetitive, but that's
‫the way it works.
‫Then we put some curly braces and in here we can do any of our set up code to give the default values
‫to the class instance.
‫So if we go over to the grab a CPP and have a look at what it's doing, you can see it's got its primary
‫component tick dot B can ever tick equal to true.
‫So let's type that out.
‫Primary component tick dot B can ever tick.
‫B is just a prefix we often use when things have a boolean type set equal to true with a lowercase t.
‫So because changes to a constructor don't always get properly propagated in live coding, I'm going
‫to close down the editor.
‫I'm going to rebuild from within Visual Studio code, then I'm going to relaunch from the EU project
‫file.
‫When that's done, I'm going to go ahead and hit play, have a look at our output log and sure enough,
‫we can see that the trigger component is ticking.
‫So I'm going to remove that log message because it's annoying and close down the output log in Visual
‫Studio code, remove this log message in the tick component.
‫We can see that it's working now.
‫And what we did want to do is do a log in our constructor.
‫So let's do a new log saying constructing, see if live coding can pick it up this time and then we're
‫going to go to our output log and have a look straight away.
‫Nothing showing up in here.
‫Well, if I go ahead and hit play, I have a look at my output log.
‫I can see the log message that the trigger component is alive and long in advance of this.
‫Actually, you can see that it is constructing way ahead when it's just creating the world.
‫It starts constructing very early before obviously begin play because we need to get the whole world
‫ready before we start playing.
‫So it happens when we hit play, but in actual fact it also happens in the editor.
‫So if I select some actor here and then add a component to it and add our trigger, then have a look
‫at my output log.
‫Then you can see the constructing message was actually printed.
‫So we need to be careful what we do in our constructors because they actually get called the C++ code
‫gets called just when we're editing, not even playing our game.
‫These objects are being created and therefore set up and therefore need to call their constructor.
‫I'm going to go ahead and delete the trigger again and clear up a little bit by removing our constructing
‫log message and our begin play log message as well.
‫Great stuff.
‫So in this lecture we've seen how we can set up our own components so that they are almost identical
‫to the ones created in the templates for scene components and ACT components, we've seen how to use
‫a constructor to give default values to properties in our class.
‫I'll see you in the next lecture.