‫Hello and welcome.
‫In this lecture, we're going to be learning about Boolean operators so that we can make complicated
‫statements that require multiple booleans to be true, such as having a gargoyle only accepted if it
‫is the correct tag and also it isn't grabbed.
‫So when I let go of the gargoyle, you can see that the wall only responds at that point.
‫Let's dive in and see how this works.
‫So now ideally we would like our trigger component to ignore or not take on an acceptable actor that
‫is currently being grabbed.
‫So to change this, I'm going to go down in to our acceptable actor implementation in trigger component
‫CP and you can see in our if statement we're currently saying let's take an actor if it has the acceptable
‫tag, and that's the only thing we are doing.
‫But what I want to do is make this conditional on two things.
‫How do we combine two Boolean values?
‫Well, there's a couple of options.
‫We've got some operators for this.
‫One of them is the ampersand ampersand operator, which we read as.
‫And and this basically means this will be combined two booleans so that it's only true if both of them
‫are true.
‫So if we had true and true, then that is going to be equal to true.
‫Whereas if either of these is false, so if the one on the right is false, then the whole thing is
‫going to be false, etc. There's another option here, which is to use this double bar operator like
‫so and this one is read as or.
‫And basically this means that if either one of these two is true, then the result will be true.
‫And also, if both of them are true, the result will be true.
‫But if both of them are false, then the result is going to be false.
‫So these are some of the common expressions that we use.
‫One other thing that we use is the exclamation point, and the exclamation point basically inverts whatever
‫boolean value we've got and we read it as not.
‫So you can have false, but because I've put a knot in front of it, this whole value, this expression
‫becomes true.
‫And if I were to order it with false, then well, because one of them at least is true, then the result
‫is going to be true.
‫So what we want to do here is essentially we want to say that if it has an actor tag, that is the acceptable
‫act tag, but it doesn't have.
‫So in this case and it doesn't have the actor tag grabbed then is acceptable.
‫So many challenge for you.
‫How would you use those three operators that I've just explained?
‫You don't have to use all of them, but which ones would you use and how would you write out this if
‫statement?
‫Okay.
‫So I'm going to put this into Boolean variables to make it super clear.
‫So you've got bool that has acceptable tag is going to be equal to actor has tag acceptable tag like
‫so and put a semicolon on the end of the line.
‫Now another bool is going to be whether it is grabbed and we can get this by going actor arrow actor
‫has tag and this case it's just going to be grabbed like so so we can say that whether it's grabbed
‫or not on this basis then in the if what we want to say is has acceptable tag and so that's ampersand
‫ampersand not that's an exclamation point is grabbed like so so that reads fairly well if you read out
‫the ampersand and the exclamation point as and and not so if acceptable has acceptable tag and not is
‫grabbed, then we return this actor.
‫Let's compile it and see if it works.
‫So let's go ahead and hit play.
‫We'll grab our gargoyle and run over to the trigger area.
‫And you see, I'm putting the gargoyle in, but nothing is happening because I've not released it yet.
‫So what if I try and pop the gargoyle down here?
‫There you go.
‫As soon as I release it, it activates it, grabs the gargoyle away from me and it disappears into the
‫ground.
‫Now there are a couple of places in Grab a CP where we've got two if statements where we could just
‫have one if we were to use the and or and not operated.
‫So one of the trick here is that when you're doing something like this where we're checking whether
‫something is not a null pointer, the other option is you can simply remove this not equal to null pointer.
‫And if you simply say if and then put in some sort of pointer as the boolean, then that automatically
‫checks that it isn't a null pointer.
‫So here it's saying if the physics handle is available here, it's saying if the grab component is available,
‫then we can go ahead and use it.
‫So I'd like to challenge you to combine your if statements.
‫So you're going to join them together in tick components.
‫Think about whether you need to use the ampersand, ampersand, the and the OR or whether you need to
‫use a negation or a knot or an exclamation point and then join them together in release as well and
‫test out your functionality and see if it still works.
‫Pause the video and have a go.
‫Okay.
‫Welcome back.
‫So we're going to make use of this because basically what we want to be doing in release is we're saying
‫if we have got a valid physics handle and that physics handle has got a grabbed component, then we
‫want to do the body of this if statement.
‫So it's quite simple to write it like that.
‫We can say if physics handle and physics handle get grabbed component, then we can do the following.
‫So that allows us to remove this early return if statement and make the code a little bit shorter.
‫Now, the eagle eyed among you may worry that, hey, we are referencing this physics handle inside
‫the if statement in order to evaluate whether this and statement should work.
‫Now there is a feature of the and that it will evaluate the thing on the left first.
‫And if that thing is false, then it doesn't even evaluate the thing on the right.
‫It doesn't need to.
‫It already knows that the result is going to be false.
‫It doesn't need to evaluate anything any further.
‫So that means it is safe to do something like this where the result on the right to calculate it is
‫only safe to do if the result on the left is true.
‫And this is quite a handy property and you'll see this used quite a lot when you are using pointers
‫that first you will check the pointer and then you'll have an ampersand.
‫Ampersand, and then you will use the pointer in the right hand side.
‫But please, please, please don't be tempted to swap these two around in order.
‫It will not work the other way around and you will get crashes.
‫So let's go up to our tick component where we're essentially doing exactly the same thing.
‫We wanted to check that we've got a grabbed component.
‫So what we can do up here is we can do physics handle and physics handle.
‫Arrow grabbed component.
‫We don't need that not equal to null pointer to keep it neat and then we can go ahead and remove the
‫if statement with the return and just keep the lines of code down, keep the legibility a little bit
‫higher.
‫Great stuff.
‫So in this lecture we've seen how to use the and or and not operators in C++.
‫I'll see you in the next lecture.