‫Hello and welcome.
‫In this lecture, we are going to be setting up the link between the mover and the trigger using a technique
‫called dependency injection so that by the end of the lecture, if we stick the gargoyle in, then the
‫door moves down, or at least partially does.
‫Let's dive in and see how this works.
‫And I grab a cup.
‫We've seen that there is a dependency, what we call a dependency for the physics handle.
‫This grabber cannot do its work unless you have a physics handle component somewhere around now it just
‫so happens that the trigger component is the same.
‫The trigger component, in order to do its work properly, needs to have access to a mover component.
‫However, it's a little bit trickier in this case because sure, in the case of our secret door, the
‫trigger and the moving object are within the same actor.
‫So you could get an object by finding the a component of type on the actor.
‫But what if and we will have this situation in my script where I want my trigger to be on a different
‫part of the room from the actor that is moving well to allow for that kind of flexibility.
‫We want it such that the trigger doesn't have to go and find its own dependencies.
‫And there's actually a name for this kind of program concept.
‫It's called dependency injection.
‫Basically, it's in the case where we've got something that says, I need another object, but I can't
‫find it myself or I don't want to find it myself.
‫And it's often sensible to not leave it up to the object that's doing its own job, making it have another
‫job of trying to get hold of an object.
‫So the way we can do this is to create a mechanism for blueprints to tell the trigger component where
‫to find a mover.
‫And the way we're going to do this is by creating a blueprint callable function on our trigger component.
‫So it's going to be public.
‫We're going to put it in the public section underneath the tick component, and it's going to be a U
‫function so that it can be then in the parentheses, blueprint callable.
‫And there doesn't need to be much to this function.
‫It's going to be void because it don't need it to return a thing, but it's going to be set mover so
‫it's going to set a link to the mover and in order for it to set a link to the mover, it needs to take
‫in a pointer to a mover component.
‫So that's going to be you mover star and we can just call this the mover like so and put semicolon at
‫the end of that line.
‫And we need to store this somewhere.
‫So we're going to create a new private variable.
‫It's not going to be a new property and it's just going to be a U move pointer, which we're going to
‫call a mover as well.
‫Like so and what we're going to do is implement this set mover over in the CPP so that it sets this
‫move a pointer, which by default is going to be null.
‫And we do have to do one more thing.
‫We have to include the header file for our mover.
‫Now in this case, because the header file is in the same folder as the trigger component, all we have
‫to do is hash include quote mover H and that's going to make sure that the trigger component is aware
‫of the mover type.
‫So let's go over into our trigger component.
‫CP And I'm going to put this below our tick component that we're going to implement set mover.
‫We're going to put the class you trigger component before and indent it.
‫Remove the semicolon and add braces.
‫And essentially all we're going to have to do here, let's change the name just in the C++ file.
‫It doesn't actually matter whether the name of the parameters matches between header and C++.
‫So we can change it here and say that the new mover is what's coming in to set mover.
‫And so we can set our local variable, our class variable mover equal to the new mover like.
‫So now it's actually pretty important that you don't skip this renaming step because if the input parameter
‫is the same name as one of the variables on the class, then unreal by default will set that to be an
‫error.
‫It's called shadowing and Unreal doesn't want you to do it, so you do need to make sure you have your
‫parameter name differently to the variable now because these new functions don't always show up with
‫live coding changes.
‫I'm going to close down the editor recompile from Visual Studio code and then relaunch from the new
‫project.
‫Now let's go to the content drawer, open up our secret wall blueprint and then from our Begin Play
‫node in the event graph, let's pull off of here and search for set mover.
‫Now notice there are two options here.
‫One, it would be to set the component which is also called mover, and one is a function on the trigger
‫component.
‫So we want to select the one that's a function on the trigger component.
‫And then what we want to do is drag in our mover component as the other arguments to this function and
‫that should do the trick.
‫It's set the mover at begin play.
‫So now essentially what we want to happen is that our trigger component should be setting and setting
‫the should move boolean in here.
‫So that's going to be my challenge to you to create some public functions in mover and mover.
‫CP It should be set, should move and it should update that you property variable.
‫In fact, you can make it not a new property anymore because we're going to be setting it through code
‫now and then you can put it in the public section.
‫It needs to be public because we're going to try and access it from our trigger component.
‫And you can call it on the mover pointer that we've got in the trigger component from its tick component
‫setting, the appropriate boolean, whether we want it to unlock or lock again, pause the video and
‫have a go.
‫Okay welcome back.
‫So I'm going to remove the property from should move I'm going to go to the public section just under
‫TTIC component and I'm going to create a void set should move taking in a boolean which we're going
‫to call should move like so.
‫And then we're going to take this and go over to the move CP and implement it, replacing the semicolon,
‫adding in the you move colon, colon class and then basically again we can call this the new should
‫move and we can say that should move equals new should move.
‫Now you might be wondering, why don't we just make the variables themselves publicly accessible so
‫that we can set those directly?
‫That's generally not good practice in C++ because if you let somebody change your variables, you never
‫know what could be happening to them.
‫Whereas if you have a method there to do it for you, it allows you later on to change how you structure
‫internal variables without having to change your public functions, which means there's fewer updates
‫to different classes that you have to do.
‫So generally, as a rule, we always use setters and getters if we need to access variables in other
‫classes.
‫So now that we've created that new function, let's go over into trig component CP where we've currently
‫got our log messages.
‫What we want to do is access our mover pointer, so we're going to do mover arrow set should move.
‫And in the case where we've got an acceptable actor, that should be true.
‫We should move semicolon at the end of the line.
‫And in the else case, well then we probably shouldn't.
‫So we're going to do move the arrow set should move and in parentheses put false remove that extra R
‫that I had on the end of my method and let's recompile now in this case because I've only added this
‫function in C++ land and it's not blueprint callable.
‫I don't think I need to actually restart the editor for this functionality to work, close down our
‫output log, go towards our gargoyle and start playing and pick him up.
‫Let's go out of this cage and stick it in the alcove and they go, It's moving down, but it stops as
‫soon as the gargoyle falls out, which is not ideal because the gargoyle is not really held in place
‫and the wall isn't fully down.
‫And because it's simulating physics, it's going to get popped out of that alcove, isn't it?
‫So what we need to do is make sure once we drop it into our trigger area, it gets fixed in there and
‫just stays.
‫So that's going to be the subject of the next lecture.
‫I'll see you there.