﻿1
00:00:04,410 --> 00:00:05,370
‫Hello and welcome.

2
00:00:05,370 --> 00:00:10,350
‫In this lecture, we're going to be learning about a strange phenomenon called an out parameter.

3
00:00:10,350 --> 00:00:17,460
‫This is a parameter that instead of passing the value in in C++, we tend to pass the value out of a

4
00:00:17,460 --> 00:00:18,240
‫function.

5
00:00:18,330 --> 00:00:23,430
‫And we're also going to see about something called a constant reference and how these two concepts are

6
00:00:23,430 --> 00:00:25,620
‫kind of intertwined with one another.

7
00:00:25,650 --> 00:00:27,900
‫Let's dive in and figure this out.

8
00:00:28,850 --> 00:00:29,180
‫Now.

9
00:00:29,180 --> 00:00:36,980
‫The real place that references shine is in parameters to functions, because every time we create a

10
00:00:36,980 --> 00:00:45,200
‫float delta time like this in our tech component, what we're doing is we are taking a copy of that

11
00:00:45,200 --> 00:00:49,580
‫original float and we are passing it in to the function.

12
00:00:49,580 --> 00:00:56,000
‫So instead of doing that, what we might want to do is take a reference to it and pass that in instead.

13
00:00:56,090 --> 00:01:03,020
‫So let's imagine that we were to create a function in gravity called print damage.

14
00:01:03,020 --> 00:01:09,360
‫And as a argument to this function, we want to pass in a float damage.

15
00:01:09,380 --> 00:01:14,780
‫Now, this, as I said, is going to make a copy of whatever we pass in and copy it in to that variable.

16
00:01:14,780 --> 00:01:18,980
‫Create this parameter variable inside the print damage function.

17
00:01:19,930 --> 00:01:24,610
‫And we do need to have a return value of void here as well to complete it in a semicolon at the end.

18
00:01:24,640 --> 00:01:28,630
‫Let's go over into the C++ and continue this conversation.

19
00:01:28,630 --> 00:01:31,030
‫Let's create the print damage in here.

20
00:01:31,300 --> 00:01:39,490
‫Let's put the you grab a class in front of it and let's put some braces instead of the semicolon.

21
00:01:39,670 --> 00:01:48,340
‫So we've got a copy in to the damage parameter variable and it's safe because if I were to do damage

22
00:01:48,340 --> 00:01:53,980
‫equals five, I'm only changing this variable here within print damage.

23
00:01:53,980 --> 00:01:55,600
‫I've got a copy of it to my own.

24
00:01:55,600 --> 00:01:58,160
‫I can't mess it up by accident.

25
00:01:58,180 --> 00:02:01,460
‫Now, what if I didn't want to make a copy?

26
00:02:01,480 --> 00:02:04,530
‫My alternative would be to take a reference.

27
00:02:04,540 --> 00:02:10,840
‫So what I can do here is I can put a float reference going into print damage, and I need to update

28
00:02:10,840 --> 00:02:14,500
‫that over in the C++ file as well.

29
00:02:14,650 --> 00:02:20,440
‫And then I might have this log message that we're using the reference for, let's go and take this down

30
00:02:20,440 --> 00:02:22,690
‫and put that into print damage.

31
00:02:22,690 --> 00:02:25,360
‫So now we take our damage and we're printing it.

32
00:02:25,360 --> 00:02:33,490
‫Outlets update the log statement to say damage is this so damages this damage and in our tick component

33
00:02:33,490 --> 00:02:41,470
‫what we can do is we can do print damage and pass in our damage variable and put semicolon at the end

34
00:02:41,470 --> 00:02:48,730
‫and we can get rid of our damage reference for now, because what we've got going on now is a damage

35
00:02:48,730 --> 00:02:49,360
‫float.

36
00:02:49,480 --> 00:02:51,040
‫It's created in TTIC.

37
00:02:51,070 --> 00:03:00,100
‫The memory for this has been created here and tick and then we go and give it to this print damage function.

38
00:03:00,100 --> 00:03:06,670
‫But because the parameter is a reference, we aren't actually copying this damage.

39
00:03:06,670 --> 00:03:13,330
‫We are taking its address and storing that in the reference instead so that when we use this reference

40
00:03:13,330 --> 00:03:18,160
‫down the line, we're actually accessing the original piece of memory.

41
00:03:18,160 --> 00:03:21,970
‫So no copy has had to happen for this to print the damage.

42
00:03:21,970 --> 00:03:25,270
‫Now let's just check that this is actually doing what we would expect it to do.

43
00:03:25,270 --> 00:03:34,060
‫If I change the damage value to five, for example, and then recompile and hit play, then go and have

44
00:03:34,060 --> 00:03:35,500
‫a look at that output log.

45
00:03:35,500 --> 00:03:38,590
‫Indeed, get the printing that the damage is five.

46
00:03:38,590 --> 00:03:42,910
‫But because we've got a reference, we know that we can update that value.

47
00:03:42,910 --> 00:03:49,000
‫So what I can do is I could say within this function I could do, hey, damage should be equal to two

48
00:03:49,000 --> 00:03:49,990
‫like so.

49
00:03:50,230 --> 00:03:55,510
‫And if I wasn't careful, if I thought I had my own copy, I might think that's okay.

50
00:03:55,510 --> 00:04:01,510
‫But actually in the tick component, what we've done is we've changed the original variable.

51
00:04:01,520 --> 00:04:09,640
‫So if I put a new log in here to inspect that original damage variable and we put a percent F and pass

52
00:04:09,640 --> 00:04:16,420
‫in the damage here, let's live coding, compile that and we're going to hit play and have a look at

53
00:04:16,420 --> 00:04:17,230
‫our output log.

54
00:04:17,230 --> 00:04:22,990
‫And you can see that now the damage and the original damage have both been modified.

55
00:04:22,990 --> 00:04:27,490
‫They're both been changed to the number two, which isn't great.

56
00:04:27,490 --> 00:04:33,130
‫You know, you could accidentally do a lot of damage, if you'll excuse the pun, by changing the variables

57
00:04:33,130 --> 00:04:35,770
‫that you weren't expecting to change.

58
00:04:35,920 --> 00:04:41,380
‫And here comes another use of that constant keyword that we've seen in the past.

59
00:04:41,530 --> 00:04:49,690
‫If I stick const in front of my float reference, then what I'm saying here is that this should be a

60
00:04:49,690 --> 00:04:52,570
‫reference that you cannot change.

61
00:04:52,570 --> 00:04:56,440
‫You cannot call any non-contact functions on it, you can't set it.

62
00:04:56,440 --> 00:05:00,430
‫You can't in any way change the contents of this variable.

63
00:05:00,430 --> 00:05:04,420
‫You can only use it, you can only read it, you can only see what is in there.

64
00:05:04,420 --> 00:05:10,570
‫So if we go back into our grab of CPP, we need to make sure that we put Const here in the parameters

65
00:05:10,570 --> 00:05:17,230
‫as well and we can go ahead and compile and you see we get a build failure and the error is saying that

66
00:05:17,230 --> 00:05:22,090
‫damage cannot be assigned to because it's a constant variable.

67
00:05:22,090 --> 00:05:25,240
‫So that gives us the protection we need.

68
00:05:25,240 --> 00:05:32,950
‫So generally speaking, if we are trying to pass a variable in, in an efficient way where we don't

69
00:05:32,950 --> 00:05:38,950
‫want to take a copy, then we will always, always use the contact keyword as well as using a reference

70
00:05:38,950 --> 00:05:43,900
‫because this prevents us from accidentally changing that contents.

71
00:05:44,200 --> 00:05:53,380
‫Now however, sometimes you do want to use it to change that value and what we might have as an example,

72
00:05:53,380 --> 00:06:03,190
‫we might have a Boolean return value and we might want to say has damage, but we might also, as we've

73
00:06:03,190 --> 00:06:06,610
‫done in Blueprint in the past, want to have a second return value.

74
00:06:06,610 --> 00:06:10,810
‫So we might say, how have we got any damage and what is that damage?

75
00:06:10,810 --> 00:06:19,090
‫Well, in this case it actually makes sense to use a simple non const reference, a float reference

76
00:06:19,090 --> 00:06:19,240
‫to.

77
00:06:19,620 --> 00:06:26,640
‫The damage, and typically we would actually call this out damage just to make it really visible that

78
00:06:26,640 --> 00:06:29,330
‫this is an what's known as an out parameter.

79
00:06:29,340 --> 00:06:31,150
‫It's not a parameter for passing things in.

80
00:06:31,170 --> 00:06:33,520
‫It's a parameter for passing things out.

81
00:06:33,540 --> 00:06:38,400
‫Now, it can be a little bit of a weird quirk of C++ and it can be hard to spot.

82
00:06:38,400 --> 00:06:44,460
‫But generally speaking, if you see any parameters that are a reference but aren't marked as const,

83
00:06:44,460 --> 00:06:50,890
‫then you can pretty much assume that somebody intended to use that as an out parameter.

84
00:06:50,910 --> 00:06:52,560
‫So that's what we've got here.

85
00:06:52,560 --> 00:06:53,820
‫We've got it has damage.

86
00:06:53,820 --> 00:07:01,440
‫And if we were to go and implement this over in the CPP, then we can put the you grabber class in front

87
00:07:01,440 --> 00:07:05,950
‫here and we can put our braces parentheses here.

88
00:07:05,970 --> 00:07:10,680
‫We're going to comment out the line in print damage where it's not happy about because we're trying

89
00:07:10,680 --> 00:07:12,390
‫to set a cost reference.

90
00:07:12,720 --> 00:07:19,710
‫And essentially what we could do in here is we could do some code, we could see if we've got some damage

91
00:07:19,710 --> 00:07:22,230
‫and then we would print out, yes, okay.

92
00:07:22,230 --> 00:07:25,470
‫Out damage equals five.

93
00:07:25,470 --> 00:07:29,760
‫So that's the damage that's been done and we're going to return.

94
00:07:29,760 --> 00:07:36,570
‫True, because we've got some some damage that's been done and this we can now use in somewhere like

95
00:07:36,570 --> 00:07:40,110
‫tick components to get hold of the damage.

96
00:07:40,410 --> 00:07:46,080
‫So here instead of setting the damage five, we might get the damage and print it if there is some damage

97
00:07:46,080 --> 00:07:54,090
‫so we can say something along these lines if has damage and then we pass in the variable that we want

98
00:07:54,090 --> 00:07:56,700
‫to be populated with the damage.

99
00:07:56,700 --> 00:08:05,040
‫So damage, for example, and then we can have the body of our statement and we could only call print

100
00:08:05,040 --> 00:08:11,990
‫damage, for example, if there is any damage to be displayed and we can remove this u log entirely.

101
00:08:12,000 --> 00:08:19,740
‫So this is the way that we use non const references as out parameters, a way of getting multiple return

102
00:08:19,740 --> 00:08:21,420
‫values out of a function.

103
00:08:21,420 --> 00:08:27,510
‫And typically when we do this, when we are calling to this function, we will create the variable,

104
00:08:27,510 --> 00:08:29,820
‫but we won't give it an initial value.

105
00:08:29,820 --> 00:08:37,050
‫So on line 39, basically I've got a float damage defined, I pass it in straight away in the next line.

106
00:08:37,050 --> 00:08:41,820
‫And this is another telltale sign that you are using an out parameter.

107
00:08:42,150 --> 00:08:48,450
‫I haven't given it any value right before I've passed it into a function means that I'm expecting that

108
00:08:48,450 --> 00:08:51,570
‫function to put the value in for me.

109
00:08:51,570 --> 00:08:55,500
‫So this can be hard to spot at first and it can be a little bit confusing.

110
00:08:55,500 --> 00:08:58,260
‫But these are the two rules that I would like you to remember.

111
00:08:58,260 --> 00:09:04,380
‫If you see a variable declared and not given a value and instantly passed into a function, ask yourself

112
00:09:04,380 --> 00:09:05,910
‫whether that's an out parameter.

113
00:09:05,910 --> 00:09:13,170
‫And the other rule is if you see a reference as a parameter, but it isn't const, then that might also

114
00:09:13,200 --> 00:09:15,600
‫be a hint that this is an out parameter.

115
00:09:15,600 --> 00:09:21,840
‫And of course if the variable or parameter is named out something, then that's a dead giveaway that

116
00:09:21,840 --> 00:09:24,060
‫this thing is meant to be an out parameter.

117
00:09:24,300 --> 00:09:24,960
‫Great stuff.

118
00:09:24,960 --> 00:09:28,890
‫I think that's set us up very nicely for being able to do a sweep.

119
00:09:28,890 --> 00:09:30,990
‫So I'll see you in the next lecture.

