‫Hello and welcome.
‫In this lecture we're going to be making our trigger component print re locking when it has got an empty
‫space and when we put the gargoyle in, it will unlock.
‫And if I take it out again, it will be lock again.
‫And the way we're going to do this is by pulling out our functionality for finding whether we have an
‫acceptable actor into its own separate function.
‫And we're going to learn about early returns within four loops.
‫To do that, let's dive in.
‫So this chunk of code that we've got in tech component is already starting to feel like a function to
‫me.
‫Essentially, it's a function that gets hold of the acceptable actor, if we've got one, so we can
‫go ahead to our trigger component.
‫And under the private section, create such a function.
‫Now, what should its return type be?
‫Well, I think it should be an A actor pointer, which can always be a null pointer if there is no acceptable
‫actor.
‫So we can then call this get acceptable actor empty parentheses.
‫We don't need to give it anything.
‫And because it doesn't change our function, we can make it const.
‫Let's go ahead and implement this over in the CPP file.
‫We're going to indent it, give it a you trigger component colon, colon and replace it semicolon with
‫braces.
‫And then the code we want is basically the contents of tic component, except what we don't want to
‫be doing is just logging that we've unlocked actor here.
‫What we want to do is in the case where we've found this actor, we want to essentially be returning
‫it.
‫So there's a couple of ways we could do this.
‫We could say at the start of our function that there is an a actor pointer, which is going to be the
‫return actor, and by default we set this to null pointer.
‫So there is no actor that we're going to return.
‫But when we go through our for loop and we eventually find an actor that has the acceptable tag, then
‫we can say, Hey, the return actor should actually point to the actor pointer that we've found here
‫in our array.
‫And then at the end of the for loop, after the for loop is finished, we've been through all the actors.
‫We can go ahead and return the return actor variable because that is the right type.
‫That's okay.
‫But it requires us to create a variable and also it requires us to run through the entire for loop,
‫even if we've already found an acceptable actor.
‫So a better way of doing this is using an early return.
‫And we've seen this sort of thing before in our grab a CP where we've done this.
‫If the physics handles equals a null pointer, then we return and the rest of the tick component doesn't
‫need to run.
‫We can use the same kind of efficiency within for loops too.
‫So what I'm going to do is if we've found the acceptable actor, we're just going to do a return actor.
‫We're literally returning the pointer that we're just found in this for loop and the rest of the for
‫loop doesn't have to run.
‫However, if we drop through to the end of the for loop and we have not found anything, we've not returned
‫anything, return would stop the for loop, leave the function.
‫But if we get to the end, we need to return something by default.
‫This is where we can just return null pointer.
‫So this is the default value.
‫It means we've gone through the entire for loop.
‫We've seen that there is no acceptable actor.
‫None of the actors that we're overlapping are.
‫With the correct tag.
‫So we return null pointer.
‫So now that means I have got this redundant return actor variable which I can go ahead and get rid of.
‫And this reads really quite cleanly.
‫We're going through, we're getting the overlapping actors, we are doing a full loop over them.
‫If each actor we test it, has it got the tag, the acceptable tag?
‫If it does, we stop right there.
‫We return that one.
‫If none of them have it, we return a null pointer.
‫So let's challenge you to log if we find the actor.
‫So call our function in your tick component.
‫Check if the pointer is null and log in both cases, both whether we have an actor or if we don't using
‫an if and an else statement.
‫Pause the video and have a go.
‫KAYE.
‫Welcome back.
‫So let's go over to our tech components.
‫What we're going to do is we're going to create a variable, a actor pointer, which is going to be
‫the actor, which is equal to get acceptable actor.
‫Then we're going to do if actor not equal to null pointer, then in the body of this, we're going to
‫do a u log unlocking and then in the else another you log this time re locking because that's the behavior
‫I want our trigger component and move it together to have is that if you've got the gargoyle in there,
‫it's going to unlock, it's going to be using the mover and it's going to be re locking if we un basically
‫uncheck the checkbox on move if we remove the gargoyle.
‫So let's compile that.
‫So let's go ahead and hit play.
‫You can see it's re locking because we don't have a gargoyle in there.
‫Pick up the gargoyle and bring him over.
‫And there you go.
‫It's now unlocking take it out and it's re locking unlocking, re locking.
‫Perfect.
‫If I step inside, nothing happens.
‫It's just re locking.
‫So we've got successfully been able to find out whether we've got something in our trigger component
‫or not.
‫Now we just need to make it do something.
‫And that's going to be the subject of the next lecture.
‫I'll see you there.