﻿1
00:00:04,350 --> 00:00:05,430
‫Hello and welcome.

2
00:00:05,430 --> 00:00:10,500
‫In this lecture we're going to be learning about if statements, essentially C++ is branch statements

3
00:00:10,500 --> 00:00:14,580
‫and we're going to be using it to send our platform backwards and forwards.

4
00:00:14,580 --> 00:00:15,450
‫Let's dive in.

5
00:00:16,240 --> 00:00:21,580
‫So there are a couple of places in the comments now where we mentioned that we should do something if

6
00:00:21,580 --> 00:00:23,440
‫we've gone too far.

7
00:00:23,470 --> 00:00:25,920
‫How do we do this if.

8
00:00:25,930 --> 00:00:31,410
‫Well, previously in Blueprint, we did that if with a branch node.

9
00:00:31,420 --> 00:00:36,760
‫Now in C++ there is a similar concept and it's called an if statement.

10
00:00:36,760 --> 00:00:42,070
‫So here's a branch node that we had as an example previously, and you can see we had this variable,

11
00:00:42,070 --> 00:00:47,590
‫it says, I'm hungry, it's a boolean, and we could put it into the condition and then we could have

12
00:00:47,590 --> 00:00:53,370
‫either a true execution branch or a false execution branch and we could execute code functions.

13
00:00:53,380 --> 00:00:58,300
‫Anything in Blueprint in C++, it's very similar except it's textual.

14
00:00:58,300 --> 00:01:04,840
‫So we'd have a variable called I'm Hungry, which is a boolean, and we can just say if and have parentheses

15
00:01:04,840 --> 00:01:05,710
‫around that.

16
00:01:06,190 --> 00:01:14,260
‫Then we would open up some curly braces after our if statement often on the next line, and then within

17
00:01:14,260 --> 00:01:19,630
‫these curly braces we can execute the stuff that should happen if it is true.

18
00:01:19,630 --> 00:01:25,300
‫So we're saying if I'm hungry, eat junk food while the wife isn't watching and then we can close our

19
00:01:25,300 --> 00:01:30,640
‫curly braces to say, okay, that's the end of the stuff that should happen conditionally.

20
00:01:31,090 --> 00:01:35,590
‫And then there's an option that if we wanted to put a false branch, if we wanted to connect something

21
00:01:35,590 --> 00:01:42,220
‫up to the false here, then we put an else keyword and we can start a second set of braces.

22
00:01:42,220 --> 00:01:46,570
‫And in here we could do keep playing video games and then we can close the braces.

23
00:01:46,570 --> 00:01:50,260
‫Now here we can have as many lines as we like between these braces.

24
00:01:50,260 --> 00:01:52,780
‫It's not necessarily that there's just one single line.

25
00:01:52,780 --> 00:01:57,280
‫So you could add a lot of code in here if you want to, or you can just have a single line.

26
00:01:57,280 --> 00:02:01,690
‫And also with the variable in here, it doesn't have to be a variable that plugs into an if statement.

27
00:02:01,690 --> 00:02:04,990
‫It can be any expression that returns a boolean.

28
00:02:04,990 --> 00:02:08,710
‫So you can do a little bit of calling a function in here.

29
00:02:08,710 --> 00:02:12,520
‫You could use an operator to get a different result.

30
00:02:12,520 --> 00:02:15,280
‫So there's a few different options available to us.

31
00:02:15,280 --> 00:02:21,550
‫But before any of that, we need to configure how far this platform is allowed to go before it should

32
00:02:21,550 --> 00:02:22,360
‫turn back.

33
00:02:22,360 --> 00:02:24,910
‫And to do that, we need a U property.

34
00:02:24,910 --> 00:02:28,330
‫We've currently got our distance moved as a visible anywhere.

35
00:02:28,330 --> 00:02:34,480
‫Let's create a new one just above here a you property that should be edit anywhere because we want to

36
00:02:34,480 --> 00:02:41,560
‫be able to configure it in the editor and I'll use the same category of moving platform from up above

37
00:02:41,770 --> 00:02:43,120
‫and then down below it.

38
00:02:43,120 --> 00:02:48,970
‫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

39
00:02:48,970 --> 00:02:53,410
‫the move distance so we can give it a default value.

40
00:02:53,410 --> 00:02:57,730
‫I'm going to give it 100 units as the default, but then obviously we can override that, change that

41
00:02:57,730 --> 00:03:02,380
‫in the editor, and then we can make use of this over in the C++ file.

42
00:03:02,380 --> 00:03:06,490
‫So the way we're going to do this is we've already got our variable distance moved.

43
00:03:06,670 --> 00:03:09,190
‫I actually want to not be able to observe that anymore.

44
00:03:09,190 --> 00:03:14,290
‫So I'm going to remove the visible anywhere property and the distance moved variable from the header

45
00:03:14,290 --> 00:03:14,800
‫file.

46
00:03:15,250 --> 00:03:20,230
‫Then I'm going to go back into the C++ and we're going to have to declare it as a local variable instead.

47
00:03:20,230 --> 00:03:26,620
‫So the easy way to just change that is to stick a float in front of distance move that basically declares

48
00:03:26,620 --> 00:03:31,300
‫it as a local variable right here inside of tick, which means that we know it's completely fresh each

49
00:03:31,300 --> 00:03:32,590
‫time we use it.

50
00:03:32,860 --> 00:03:35,800
‫So now we need to reverse the direction if we've gone too far.

51
00:03:35,800 --> 00:03:43,900
‫So the way we can do this is we can use less than or equal or equal to or greater than operators that

52
00:03:43,900 --> 00:03:46,960
‫we've seen in the past as nodes in blueprint.

53
00:03:46,960 --> 00:03:51,490
‫But now we can use them as operators like we would plus or multiplication.

54
00:03:51,790 --> 00:03:56,590
‫And the way we can do this is to say, okay, well, we've got the distance moved.

55
00:03:56,740 --> 00:04:00,340
‫Is this greater than the move distance?

56
00:04:00,340 --> 00:04:04,450
‫So we can use the greater than symbol, which is this right Chevron.

57
00:04:04,750 --> 00:04:10,600
‫And then we can put move distance and it looks like I've called it moved distance, but I think it should

58
00:04:10,600 --> 00:04:11,440
‫be the move distance.

59
00:04:11,440 --> 00:04:13,510
‫So I've accidentally had an extra D in there.

60
00:04:13,510 --> 00:04:19,930
‫Let's remove that from the header file and you see this expression here returns a boolean so we can

61
00:04:19,930 --> 00:04:22,750
‫actually use it inside our if statement.

62
00:04:22,750 --> 00:04:32,350
‫So what we can do is we can say if space open brackets and then I'm going to cut this expression and

63
00:04:32,350 --> 00:04:33,970
‫paste it into those brackets.

64
00:04:33,970 --> 00:04:40,330
‫So it's saying if the distance moved is greater than the move distance, then essentially we want to

65
00:04:40,330 --> 00:04:41,830
‫reverse the direction.

66
00:04:41,830 --> 00:04:44,500
‫So that's what we're doing this comment for.

67
00:04:44,530 --> 00:04:48,310
‫We want to reverse the direction of motion and to reverse the direction of motion.

68
00:04:48,310 --> 00:04:50,350
‫We've got a platform velocity.

69
00:04:50,350 --> 00:04:57,070
‫So let's set the platform velocity to the negative of itself, basically reversing its direction.

70
00:04:57,070 --> 00:04:59,860
‫So platform velocity equals.

71
00:05:00,660 --> 00:05:02,810
‫Negative platform velocity.

72
00:05:02,820 --> 00:05:07,350
‫So that's going to basically flip the arrow of the velocity.

73
00:05:07,590 --> 00:05:11,670
‫I'm going to put a semicolon on the end of that line and let's see if this if statement actually works

74
00:05:11,670 --> 00:05:12,420
‫as we intend it to.

75
00:05:12,450 --> 00:05:14,610
‫Let's go over into the editor and hit compile.

76
00:05:14,610 --> 00:05:19,860
‫And now that that's completed successfully, we can go ahead and hit play and see what happens.

77
00:05:20,130 --> 00:05:27,000
‫So I'm expecting it to go forward and backwards pretty much as as it's doing, and that's pretty good.

78
00:05:27,030 --> 00:05:31,710
‫Now, what's actually going on is it's going a little bit past its starting point.

79
00:05:32,040 --> 00:05:33,390
‫The reason for that.

80
00:05:33,390 --> 00:05:35,130
‫So if we have a look, that's the starting point.

81
00:05:35,130 --> 00:05:40,200
‫And if I go and hit play and look to the side from this same angle where my player is standing, you

82
00:05:40,200 --> 00:05:44,790
‫can see it's going the same distance from the starting point in each direction.

83
00:05:44,790 --> 00:05:51,000
‫And that's because when we look at what's being calculated here, the distance moved is from a start

84
00:05:51,000 --> 00:05:53,160
‫location that never changes.

85
00:05:53,160 --> 00:05:58,860
‫We never update that start location, so we're going in the negative direction.

86
00:05:58,860 --> 00:06:03,210
‫We're then going to travel past it the same amount as we traveled the other way.

87
00:06:03,360 --> 00:06:06,300
‫So that's not how I want my platform to work.

88
00:06:06,300 --> 00:06:09,960
‫So I'm going to challenge you to update your start location.

89
00:06:09,960 --> 00:06:12,240
‫We're going to be doing that inside the if statement.

90
00:06:12,240 --> 00:06:17,910
‫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

91
00:06:17,910 --> 00:06:23,820
‫location so that basically we reset our starting point to the other side and then we can be traveling

92
00:06:23,820 --> 00:06:24,870
‫in the opposite direction.

93
00:06:24,870 --> 00:06:28,370
‫So test the results and see if it works as you expect.

94
00:06:28,380 --> 00:06:29,880
‫Pause video and have a go.

95
00:06:32,930 --> 00:06:33,440
‫Okay.

96
00:06:33,440 --> 00:06:34,160
‫Welcome back.

97
00:06:34,160 --> 00:06:39,290
‫So we're going to after we've reversed the velocity here, it doesn't actually matter whether it's after

98
00:06:39,290 --> 00:06:42,470
‫or before because they don't have an impact on each other.

99
00:06:42,890 --> 00:06:52,040
‫We're going to set the start location equal to the current location, which essentially means that in

100
00:06:52,040 --> 00:06:57,290
‫the next frame the distance moved is going to be zero because we know that the distance between the

101
00:06:57,290 --> 00:06:59,710
‫start location and itself will be zero.

102
00:06:59,720 --> 00:07:02,480
‫Let's go back into Unreal, hit the compile button.

103
00:07:02,480 --> 00:07:05,840
‫And when that's done, let's hit play and see what happens now.

104
00:07:06,260 --> 00:07:09,080
‫So I expect this to go forwards and backwards.

105
00:07:09,080 --> 00:07:09,860
‫There you go.

106
00:07:09,860 --> 00:07:15,290
‫About the same amount of units always going back to its starting location, not going back any further,

107
00:07:15,290 --> 00:07:16,670
‫which is fantastic.

108
00:07:16,670 --> 00:07:18,260
‫Just what I wanted to happen.

109
00:07:18,260 --> 00:07:25,010
‫So that's it for this lecture we've learnt how to use our if statements and Boolean expressions in order

110
00:07:25,010 --> 00:07:29,360
‫to do that and we've allowed that to send our platform back and forth.

111
00:07:29,360 --> 00:07:30,290
‫Great stuff.

112
00:07:30,380 --> 00:07:31,970
‫I'll see you in the next lecture.

