﻿1
00:00:04,750 --> 00:00:05,980
‫Hello and welcome.

2
00:00:05,980 --> 00:00:12,010
‫In this lecture, we're going to be learning about how we can get outputs out of functions in C++.

3
00:00:12,010 --> 00:00:18,970
‫And we're going to use this to vastly simplify our code and to start implementing some of that pseudocode

4
00:00:18,970 --> 00:00:19,870
‫we've written.

5
00:00:19,870 --> 00:00:20,800
‫Let's dive in.

6
00:00:20,980 --> 00:00:26,230
‫So before we try to understand return values from functions, let's do a brief detour and understand

7
00:00:26,230 --> 00:00:26,350
‫it.

8
00:00:26,350 --> 00:00:29,290
‫Two concepts here add two more concepts to our glossary.

9
00:00:29,590 --> 00:00:37,060
‫So the first one is the expression and expression is a fragment of code that produces a value.

10
00:00:37,060 --> 00:00:43,180
‫And there's the statement which is a fragment of code that is an action to be performed.

11
00:00:43,570 --> 00:00:51,130
‫Now these two actually have a relationship in that an expression almost always is a statement in that

12
00:00:51,130 --> 00:00:57,100
‫to calculate the value of a fragment of code, you need to it needs to be an action to be performed.

13
00:00:57,100 --> 00:01:01,030
‫But the same isn't necessarily true the other way around.

14
00:01:01,030 --> 00:01:04,780
‫A statement isn't always going to give you a value.

15
00:01:04,840 --> 00:01:09,910
‫So let's have a look at some examples of expressions and some things that are not from the code that

16
00:01:09,910 --> 00:01:10,960
‫we've already written.

17
00:01:11,170 --> 00:01:20,410
‫So when we type my vector into C++, this is an expression because it has the value of whatever is stored

18
00:01:20,410 --> 00:01:22,510
‫inside the my vector variable.

19
00:01:22,510 --> 00:01:24,730
‫Same is true for local vector.

20
00:01:24,730 --> 00:01:32,950
‫And in fact if you have local vector plus z, that itself is an expression and local vector dot z plus

21
00:01:32,950 --> 00:01:33,850
‫100.

22
00:01:33,850 --> 00:01:41,620
‫That itself is an expression too, because the local vector z gave you the value inside of the Z member.

23
00:01:41,620 --> 00:01:46,960
‫And when you add 100, that gives you a new value which has the addition of 100.

24
00:01:46,960 --> 00:01:48,700
‫So these are all expressions.

25
00:01:48,940 --> 00:01:55,360
‫However, something that is not an expression is when we declared our local vector and we said F vector,

26
00:01:55,360 --> 00:01:58,870
‫local vector equals my vector and put a semicolon at the end of the line.

27
00:01:58,870 --> 00:02:01,000
‫We can't use that to get a value.

28
00:02:01,000 --> 00:02:02,590
‫There's no value there.

29
00:02:02,590 --> 00:02:06,370
‫It's just telling C++ that Here's an action for you to perform.

30
00:02:06,370 --> 00:02:09,310
‫I want you to create a new local vector.

31
00:02:09,310 --> 00:02:11,980
‫So that is not an expression.

32
00:02:12,310 --> 00:02:17,020
‫However, something that is an expression is get actor location.

33
00:02:17,020 --> 00:02:20,950
‫It's a function that we are calling because there's parentheses on the end here.

34
00:02:20,950 --> 00:02:22,720
‫We actually don't have any arguments here.

35
00:02:22,720 --> 00:02:26,290
‫This is an empty list of arguments, but it is calling something.

36
00:02:26,290 --> 00:02:34,780
‫And because this function has a return value, the actual piece of code here is an expression that takes

37
00:02:34,780 --> 00:02:37,810
‫on the value of whatever's returned from the function.

38
00:02:37,810 --> 00:02:44,350
‫And this is essentially the same as the output pins that we have previously seen in Blueprint.

39
00:02:44,350 --> 00:02:50,800
‫Except unlike with Blueprint, you can only have one output value from a function.

40
00:02:50,800 --> 00:02:52,600
‫There's just one return value.

41
00:02:52,600 --> 00:02:59,830
‫So that is what we can use over here where we've written our pseudo code that says Get current location.

42
00:02:59,830 --> 00:03:02,440
‫That is where we're going to do that.

43
00:03:02,440 --> 00:03:08,410
‫We're going to get the location using our get active location.

44
00:03:08,410 --> 00:03:11,320
‫So let's create a new local variable here.

45
00:03:11,320 --> 00:03:14,650
‫We're going to have a new local variable called F of type F vector.

46
00:03:14,650 --> 00:03:20,350
‫It has to be a type F vector because that is the type returned from get active location.

47
00:03:20,440 --> 00:03:29,470
‫And we're going to call this current location and we'll set that equal to the result or the expression

48
00:03:29,470 --> 00:03:36,970
‫get actor location and then parentheses a set of matching parentheses and then a semicolon to end the

49
00:03:36,970 --> 00:03:37,540
‫line.

50
00:03:37,540 --> 00:03:44,020
‫So if we dissect this line a little bit, we can see that this bit here, the get actor location and

51
00:03:44,020 --> 00:03:49,990
‫the parentheses is the expression it's getting us the value, which is the current location of the actor.

52
00:03:50,290 --> 00:03:57,040
‫Then we are doing an assignment to the current location, which is also being declared right here.

53
00:03:57,040 --> 00:04:00,550
‫So on this line, this whole line is not an expression.

54
00:04:00,550 --> 00:04:03,220
‫There's no value out of it, it is a statement.

55
00:04:03,220 --> 00:04:09,880
‫And usually you can tell a statement because in C++ it ends in a semicolon.

56
00:04:09,880 --> 00:04:16,780
‫Okay, so this is essentially going to allow us to do away with the my vector and local vectors that

57
00:04:16,780 --> 00:04:23,650
‫we've been using previously because we should just be able to update our local variable here, this

58
00:04:23,650 --> 00:04:29,470
‫current location that we've got from the location of our actor.

59
00:04:29,470 --> 00:04:34,390
‫So what we're going to do is further down here, we've got a comment that says, set the location.

60
00:04:34,390 --> 00:04:45,430
‫So after that comment line, I'm going to do a set actor location using the expression current location,

61
00:04:45,430 --> 00:04:50,110
‫which is just using that variable value and then put a semicolon at the end of the line to say that

62
00:04:50,110 --> 00:04:51,460
‫this whole thing is a statement.

63
00:04:51,460 --> 00:04:58,990
‫We want it to execute this action set at a location and notice that I while my comments are indented,

64
00:04:58,990 --> 00:05:04,120
‫I have decided not to indent the code to match because usually we want.

65
00:05:04,630 --> 00:05:09,130
‫Codes to be at one level of indentation within the function.

66
00:05:09,670 --> 00:05:15,490
‫Now what I can do is I can remove all of the code we had previously, everything involving local vector.

67
00:05:15,490 --> 00:05:18,430
‫My vector set a location using the local vector.

68
00:05:18,430 --> 00:05:23,830
‫I can remove all of that so that all we've got now is the stuff around our comments.

69
00:05:24,040 --> 00:05:27,100
‫And also I'm going to go over into the header file and clean things up a bit here.

70
00:05:27,100 --> 00:05:31,420
‫We no longer need the my vector, we no longer need the my x new properties.

71
00:05:31,420 --> 00:05:34,440
‫So I'm going to delete these ones as well.

72
00:05:34,450 --> 00:05:40,000
‫Now obviously the moving platform CP doesn't actually do very much because we get the current active

73
00:05:40,000 --> 00:05:44,680
‫location, then we set it again, which basically has no effect.

74
00:05:44,680 --> 00:05:52,870
‫So I'd like you to have a go at updating the current location of the moving platform in that tick function.

75
00:05:52,990 --> 00:05:56,530
‫So use the X or Y members as you see fit.

76
00:05:56,530 --> 00:06:03,820
‫One X will move you forward, Y will move you to the right, then you can add to it and assign back

77
00:06:03,820 --> 00:06:08,770
‫into the member, as we've been doing previously, to update our moving platforms location.

78
00:06:08,770 --> 00:06:15,940
‫So add one or two depending on how fast you want the platform to move and then the move the platform

79
00:06:15,940 --> 00:06:20,860
‫to its start location because now we don't have that member variable to tell it the start location,

80
00:06:20,860 --> 00:06:23,920
‫we actually using the current location.

81
00:06:23,920 --> 00:06:28,960
‫So wherever you place that platform, that is where the platform is going to start from.

82
00:06:29,200 --> 00:06:35,380
‫And then I'd like you to compile and test your code and see if it's moving, as you'd expect.

83
00:06:35,410 --> 00:06:37,240
‫Pause the video and have a go.

84
00:06:40,030 --> 00:06:40,360
‫Okay.

85
00:06:40,360 --> 00:06:41,240
‫Welcome back.

86
00:06:41,260 --> 00:06:47,230
‫So we're going to be placing this line of code just underneath the comment that talks about it, add

87
00:06:47,230 --> 00:06:49,210
‫vector to that location.

88
00:06:49,600 --> 00:06:52,900
‫And the reason we're putting it here is because of the order.

89
00:06:52,900 --> 00:06:56,830
‫We want to get the current location, update it and then set it.

90
00:06:56,860 --> 00:06:59,710
‫We don't want anything out of order to happen there.

91
00:07:00,070 --> 00:07:04,790
‫If we were to update it before we created the variable, well, that variable wouldn't exist.

92
00:07:04,810 --> 00:07:06,490
‫Remember, it's a local variable.

93
00:07:06,520 --> 00:07:11,110
‫It only exists once we create it within this particular call of the function.

94
00:07:11,110 --> 00:07:16,750
‫And if we'd done it after set actor location again, that modification would just be chucked away when

95
00:07:16,750 --> 00:07:20,210
‫we got to the closing brace in the function.

96
00:07:20,260 --> 00:07:29,410
‫So we're going to set the current location dot x, I'm going to use equal to the current location,

97
00:07:29,410 --> 00:07:34,180
‫dot x plus one and then put a semicolon at the end of the line.

98
00:07:34,180 --> 00:07:40,180
‫And we're going to go back in to Unreal and we're going to hit the compile button.

99
00:07:40,180 --> 00:07:45,640
‫And as I mentioned, we now need to go and move this moving platform to its starting location.

100
00:07:45,640 --> 00:07:51,490
‫So I'm going to click on our third person character and I'm going to copy his location by right clicking

101
00:07:51,490 --> 00:07:56,050
‫on the location, hitting copy, then going to our moving platform, right clicking on its location,

102
00:07:56,050 --> 00:07:56,770
‫hitting paste.

103
00:07:56,770 --> 00:08:01,510
‫So that puts the platform right inside our player, which is not actually what we want.

104
00:08:01,510 --> 00:08:09,250
‫So I'm going to move it slightly off to the right of the player like so and I can see if it's at the

105
00:08:09,250 --> 00:08:09,790
‫right height.

106
00:08:09,790 --> 00:08:15,400
‫I think I'll make it a little bit lower down, which is one of the benefits of using the current location

107
00:08:15,400 --> 00:08:19,960
‫rather than a location variable, is that I can actually see where this platform is going to be in the

108
00:08:19,960 --> 00:08:20,560
‫world.

109
00:08:20,800 --> 00:08:27,130
‫So now if I go ahead and hit play, then you can see that the platform is moving along nicely in the

110
00:08:27,130 --> 00:08:31,510
‫X direction and I can still jump on it and be moved along with it.

111
00:08:31,510 --> 00:08:32,450
‫Great stuff.

112
00:08:32,470 --> 00:08:36,490
‫Now we've learned how return values work out of functions.

113
00:08:36,490 --> 00:08:43,160
‫We are really starting to understand a lot of the concepts from blueprints in the C++ world.

114
00:08:43,180 --> 00:08:44,890
‫I'll see you in the next lecture.

