‫Hello and welcome.
‫In this lecture we're going to be learning about if statements, essentially C++ is branch statements
‫and we're going to be using it to send our platform backwards and forwards.
‫Let's dive in.
‫So there are a couple of places in the comments now where we mentioned that we should do something if
‫we've gone too far.
‫How do we do this if.
‫Well, previously in Blueprint, we did that if with a branch node.
‫Now in C++ there is a similar concept and it's called an if statement.
‫So here's a branch node that we had as an example previously, and you can see we had this variable,
‫it says, I'm hungry, it's a boolean, and we could put it into the condition and then we could have
‫either a true execution branch or a false execution branch and we could execute code functions.
‫Anything in Blueprint in C++, it's very similar except it's textual.
‫So we'd have a variable called I'm Hungry, which is a boolean, and we can just say if and have parentheses
‫around that.
‫Then we would open up some curly braces after our if statement often on the next line, and then within
‫these curly braces we can execute the stuff that should happen if it is true.
‫So we're saying if I'm hungry, eat junk food while the wife isn't watching and then we can close our
‫curly braces to say, okay, that's the end of the stuff that should happen conditionally.
‫And then there's an option that if we wanted to put a false branch, if we wanted to connect something
‫up to the false here, then we put an else keyword and we can start a second set of braces.
‫And in here we could do keep playing video games and then we can close the braces.
‫Now here we can have as many lines as we like between these braces.
‫It's not necessarily that there's just one single line.
‫So you could add a lot of code in here if you want to, or you can just have a single line.
‫And also with the variable in here, it doesn't have to be a variable that plugs into an if statement.
‫It can be any expression that returns a boolean.
‫So you can do a little bit of calling a function in here.
‫You could use an operator to get a different result.
‫So there's a few different options available to us.
‫But before any of that, we need to configure how far this platform is allowed to go before it should
‫turn back.
‫And to do that, we need a U property.
‫We've currently got our distance moved as a visible anywhere.
‫Let's create a new one just above here a you property that should be edit anywhere because we want to
‫be able to configure it in the editor and I'll use the same category of moving platform from up above
‫and then down below it.
‫Here it's going to be a type float because it's going to be a distance and and I'm going to call it
‫the move distance so we can give it a default value.
‫I'm going to give it 100 units as the default, but then obviously we can override that, change that
‫in the editor, and then we can make use of this over in the C++ file.
‫So the way we're going to do this is we've already got our variable distance moved.
‫I actually want to not be able to observe that anymore.
‫So I'm going to remove the visible anywhere property and the distance moved variable from the header
‫file.
‫Then I'm going to go back into the C++ and we're going to have to declare it as a local variable instead.
‫So the easy way to just change that is to stick a float in front of distance move that basically declares
‫it as a local variable right here inside of tick, which means that we know it's completely fresh each
‫time we use it.
‫So now we need to reverse the direction if we've gone too far.
‫So the way we can do this is we can use less than or equal or equal to or greater than operators that
‫we've seen in the past as nodes in blueprint.
‫But now we can use them as operators like we would plus or multiplication.
‫And the way we can do this is to say, okay, well, we've got the distance moved.
‫Is this greater than the move distance?
‫So we can use the greater than symbol, which is this right Chevron.
‫And then we can put move distance and it looks like I've called it moved distance, but I think it should
‫be the move distance.
‫So I've accidentally had an extra D in there.
‫Let's remove that from the header file and you see this expression here returns a boolean so we can
‫actually use it inside our if statement.
‫So what we can do is we can say if space open brackets and then I'm going to cut this expression and
‫paste it into those brackets.
‫So it's saying if the distance moved is greater than the move distance, then essentially we want to
‫reverse the direction.
‫So that's what we're doing this comment for.
‫We want to reverse the direction of motion and to reverse the direction of motion.
‫We've got a platform velocity.
‫So let's set the platform velocity to the negative of itself, basically reversing its direction.
‫So platform velocity equals.
‫Negative platform velocity.
‫So that's going to basically flip the arrow of the velocity.
‫I'm going to put a semicolon on the end of that line and let's see if this if statement actually works
‫as we intend it to.
‫Let's go over into the editor and hit compile.
‫And now that that's completed successfully, we can go ahead and hit play and see what happens.
‫So I'm expecting it to go forward and backwards pretty much as as it's doing, and that's pretty good.
‫Now, what's actually going on is it's going a little bit past its starting point.
‫The reason for that.
‫So if we have a look, that's the starting point.
‫And if I go and hit play and look to the side from this same angle where my player is standing, you
‫can see it's going the same distance from the starting point in each direction.
‫And that's because when we look at what's being calculated here, the distance moved is from a start
‫location that never changes.
‫We never update that start location, so we're going in the negative direction.
‫We're then going to travel past it the same amount as we traveled the other way.
‫So that's not how I want my platform to work.
‫So I'm going to challenge you to update your start location.
‫We're going to be doing that inside the if statement.
‫So it's only going to happen when we reach the end of our travel and you're going to set it to the current
‫location so that basically we reset our starting point to the other side and then we can be traveling
‫in the opposite direction.
‫So test the results and see if it works as you expect.
‫Pause video and have a go.
‫Okay.
‫Welcome back.
‫So we're going to after we've reversed the velocity here, it doesn't actually matter whether it's after
‫or before because they don't have an impact on each other.
‫We're going to set the start location equal to the current location, which essentially means that in
‫the next frame the distance moved is going to be zero because we know that the distance between the
‫start location and itself will be zero.
‫Let's go back into Unreal, hit the compile button.
‫And when that's done, let's hit play and see what happens now.
‫So I expect this to go forwards and backwards.
‫There you go.
‫About the same amount of units always going back to its starting location, not going back any further,
‫which is fantastic.
‫Just what I wanted to happen.
‫So that's it for this lecture we've learnt how to use our if statements and Boolean expressions in order
‫to do that and we've allowed that to send our platform back and forth.
‫Great stuff.
‫I'll see you in the next lecture.