﻿1
00:00:04,350 --> 00:00:05,520
‫Hello and welcome.

2
00:00:05,520 --> 00:00:12,210
‫In this lecture we are going to be implementing our own class member functions, move platform and rotate

3
00:00:12,210 --> 00:00:12,630
‫platform.

4
00:00:12,630 --> 00:00:17,190
‫We're going to see how we can set them up so that they can take in arguments.

5
00:00:17,190 --> 00:00:21,030
‫And it's going to help us tidy up our code a little bit.

6
00:00:21,030 --> 00:00:23,370
‫Let's dive in and see how this works.

7
00:00:24,060 --> 00:00:29,550
‫So up until now, we've been using C++ functions that have already been created for us.

8
00:00:29,550 --> 00:00:34,740
‫We've been using TIC V using big in play in terms of writing their functionality.

9
00:00:34,740 --> 00:00:38,130
‫And we've also been using functions that other people have written for us.

10
00:00:38,130 --> 00:00:41,070
‫We've been calling them such as get active location and get name.

11
00:00:41,280 --> 00:00:49,110
‫Now how can we go about creating new own functions to help organize our code and keep these functions

12
00:00:49,110 --> 00:00:51,900
‫small and everything much more legible?

13
00:00:52,320 --> 00:00:57,990
‫Well, we've got the basic building blocks already in place by getting towards the end of the section.

14
00:00:57,990 --> 00:01:00,330
‫We understand a lot more about C++ syntax.

15
00:01:00,330 --> 00:01:08,400
‫So what I want to do is essentially create a new function on the moving platform that allows us to have

16
00:01:08,490 --> 00:01:12,600
‫just intake, say call move platform.

17
00:01:12,870 --> 00:01:14,220
‫Why do I want to do that?

18
00:01:14,220 --> 00:01:20,310
‫Well, because being able to make a function call like this called MOVE Platform, it really speaks

19
00:01:20,310 --> 00:01:22,590
‫to what we're trying to do at the moment.

20
00:01:22,590 --> 00:01:28,440
‫We've got a lot of stuff going on in tech and I'm not entirely clear why we are doing all this stuff

21
00:01:28,440 --> 00:01:34,500
‫if I just come and take a glance at it, if I went into tech and saw a function that says Move platform,

22
00:01:34,590 --> 00:01:39,660
‫well then suddenly I have the context for what all this stuff should be doing, and I don't need a single

23
00:01:39,660 --> 00:01:42,480
‫comment to tell me what's going on.

24
00:01:42,480 --> 00:01:46,950
‫And this is going to be particularly useful when we add into the mix the possibility of rotating the

25
00:01:46,950 --> 00:01:48,240
‫platform as well.

26
00:01:48,810 --> 00:01:52,740
‫So how do we define declare functions?

27
00:01:52,740 --> 00:01:56,670
‫Well, the declaration aspect is done over in the header file.

28
00:01:56,670 --> 00:02:02,190
‫That's where we basically say a heads up to C++, we're going to have a function that's going to look

29
00:02:02,190 --> 00:02:03,270
‫kind of like this.

30
00:02:03,300 --> 00:02:06,930
‫And then over in the C++ is where we actually define the function.

31
00:02:06,930 --> 00:02:09,900
‫We give it the body, we tell it exactly to C++.

32
00:02:09,900 --> 00:02:12,660
‫This is how the function works.

33
00:02:13,140 --> 00:02:16,320
‫And let's have a look at how we can do this.

34
00:02:16,320 --> 00:02:20,070
‫We've got a few examples of functions and they got big in play.

35
00:02:20,070 --> 00:02:27,420
‫We've got tick and it's a good point in time for us to look at the different sections that we've got

36
00:02:27,420 --> 00:02:29,040
‫in our header file.

37
00:02:29,040 --> 00:02:35,190
‫We've got public and protected being mentioned here and public being mentioned again.

38
00:02:35,370 --> 00:02:36,510
‫Now what does this mean?

39
00:02:36,510 --> 00:02:43,410
‫These key words with the colon at the end basically define a section of accessibility.

40
00:02:43,410 --> 00:02:46,470
‫These functions, where can they be accessed from?

41
00:02:46,650 --> 00:02:51,390
‫We've seen over in the get safe normal.

42
00:02:51,390 --> 00:02:57,960
‫This is a function that we're calling on an F vector, but we're calling on it from another class,

43
00:02:57,990 --> 00:02:59,880
‫not from within an F vector.

44
00:03:00,150 --> 00:03:05,070
‫So this means that the accessibility of this function needs to be public.

45
00:03:05,070 --> 00:03:09,900
‫Now we'll leave aside protected just for the minute because it gets a little bit more complicated there.

46
00:03:09,900 --> 00:03:14,400
‫But there is another type of key word that we can use, which is private.

47
00:03:14,700 --> 00:03:19,410
‫So I'm going to add after tick, I'm going to add the private key word.

48
00:03:19,410 --> 00:03:24,360
‫Notice that I don't indent these sections and we're going to put a code on at the end.

49
00:03:24,360 --> 00:03:29,100
‫And that means that everything that comes afterwards until the next section, the next key word that

50
00:03:29,100 --> 00:03:31,710
‫I put in here is going to be private.

51
00:03:31,710 --> 00:03:37,710
‫So that means we've taken all of these variables and said, hey, no other class is allowed to touch

52
00:03:37,710 --> 00:03:38,310
‫these.

53
00:03:38,310 --> 00:03:40,380
‫And that's usually the best practice.

54
00:03:40,380 --> 00:03:44,220
‫We haven't done it up to this point because I didn't want to get things a little bit too confusing for

55
00:03:44,220 --> 00:03:44,640
‫you.

56
00:03:44,640 --> 00:03:49,350
‫But variables in classes should generally be their own business.

57
00:03:49,350 --> 00:03:55,980
‫It's one of the core tenants of object oriented programming that we leave classes to manage their own

58
00:03:55,980 --> 00:04:02,280
‫variables, and we only access them using functions that the class denotes as being public.

59
00:04:03,060 --> 00:04:08,070
‫So what we're going to do here is we're going to add some more private functions in here because we

60
00:04:08,070 --> 00:04:12,300
‫don't want something like Move Platform to be called from outside of the class.

61
00:04:12,300 --> 00:04:16,590
‫We want it to be managed by our own tick function.

62
00:04:16,860 --> 00:04:20,370
‫So how do we define a private function?

63
00:04:20,370 --> 00:04:22,680
‫Well, we put it in a private section.

64
00:04:22,680 --> 00:04:24,780
‫So we've got our private key word up here.

65
00:04:24,780 --> 00:04:31,470
‫I'm going to put it after our variables, however, and we start off with the return type.

66
00:04:31,470 --> 00:04:38,700
‫And if our function doesn't return anything, we just type void to indicate that there's no return coming

67
00:04:38,700 --> 00:04:40,020
‫out of this function.

68
00:04:40,470 --> 00:04:42,570
‫Then we give it a function name.

69
00:04:42,570 --> 00:04:51,180
‫So we had move platform was what we wanted and then we give it some parentheses to indicate that this

70
00:04:51,180 --> 00:04:56,910
‫is a function open and close parentheses and then a semicolon to finish off the line and say, okay,

71
00:04:56,910 --> 00:04:57,330
‫that's it.

72
00:04:57,330 --> 00:05:03,180
‫That is the function that I want to declare here in the header file and all that does.

73
00:05:03,260 --> 00:05:08,990
‫It says this is the shape of the function, this is the return, this is what parameters it takes and

74
00:05:08,990 --> 00:05:11,670
‫the name and which class it belongs to.

75
00:05:11,690 --> 00:05:16,280
‫Now, to actually give it an implementation, we have to define it over in the C++ file.

76
00:05:16,280 --> 00:05:17,540
‫So let's go over there.

77
00:05:17,780 --> 00:05:23,540
‫And at the end of the file, just outside of any curly braces, this is where we do it.

78
00:05:23,540 --> 00:05:25,670
‫We start again with the return type.

79
00:05:25,670 --> 00:05:32,000
‫So that's going to be void then because we're inside a C++ file, we're not inside the curly braces

80
00:05:32,000 --> 00:05:32,750
‫of a class.

81
00:05:32,750 --> 00:05:41,060
‫We have to use the scope resolution operator to say that we're we're trying to define a function from

82
00:05:41,060 --> 00:05:42,590
‫within the moving platform class.

83
00:05:42,590 --> 00:05:49,160
‫So we type the name of the class, which is a moving platform, remembering that a prefix is there to

84
00:05:49,160 --> 00:05:50,630
‫say that it's an actor.

85
00:05:51,320 --> 00:05:55,970
‫Then we use colon, colon, the scope resolution operator, and then we use the name of the function

86
00:05:55,970 --> 00:06:02,870
‫we're defining, which is MOVE platform, and then we use the same set of parameters as we did in the

87
00:06:02,870 --> 00:06:03,440
‫header file.

88
00:06:03,440 --> 00:06:07,970
‫In this case, we had an empty set of parameters, so we just have the parentheses.

89
00:06:08,300 --> 00:06:15,230
‫And then instead of putting a semicolon at the end of this line on a new line, we put an opening curly

90
00:06:15,230 --> 00:06:18,830
‫brace and a corresponding closing curly brace.

91
00:06:18,950 --> 00:06:24,860
‫And between these curly braces where we put the code for our function, fantastic.

92
00:06:24,860 --> 00:06:29,750
‫So what I want to do now is try and say, well, all of this functionality we've got here, and in fact,

93
00:06:29,750 --> 00:06:32,330
‫I'm going to delete the comments because we no longer need them.

94
00:06:32,330 --> 00:06:38,150
‫They were the pseudocode we used to implement this, but now it's kind of served its purpose.

95
00:06:38,150 --> 00:06:40,400
‫We understand what the lines of code are doing.

96
00:06:40,520 --> 00:06:48,950
‫I'm going to take everything but the MOVE platform function out of TTIC and copy it and paste it into

97
00:06:48,950 --> 00:06:51,590
‫our MOVE platform function instead.

98
00:06:52,040 --> 00:06:57,800
‫So this should work pretty much the same because when we go into tick, we then call move platform and

99
00:06:57,800 --> 00:06:59,540
‫it just runs the same piece of code.

100
00:06:59,540 --> 00:07:05,930
‫Except now we've bundled it into a function and it makes it easier for us to read and potentially reuse

101
00:07:05,930 --> 00:07:06,950
‫in the future.

102
00:07:07,160 --> 00:07:11,390
‫But if you do do this, you will notice there is a problem.

103
00:07:11,900 --> 00:07:19,880
‫We are accessing Delta time and that is a parameter we no longer have access to because it was a parameter

104
00:07:19,880 --> 00:07:21,110
‫passed into tick.

105
00:07:21,440 --> 00:07:29,930
‫So if we want to get hold of that, we do need to have a parameter in our function.

106
00:07:29,930 --> 00:07:33,020
‫So when we define parameters, we have to do it in two places.

107
00:07:33,020 --> 00:07:37,760
‫Just like when we define the function, we go back over to the header file and between the parentheses

108
00:07:37,760 --> 00:07:40,190
‫of our MOVE platform function.

109
00:07:40,190 --> 00:07:45,770
‫This is where we say what parameters we want and it's exactly like defining a variable.

110
00:07:45,770 --> 00:07:48,110
‫You need to give it a type and you need to give it a name.

111
00:07:48,110 --> 00:07:53,000
‫So we're going to say it's going to be a type float and we can just call it Delta Time.

112
00:07:53,000 --> 00:07:54,320
‫It doesn't have to be the same name.

113
00:07:54,320 --> 00:07:56,870
‫We could call it D.T., we could call it time.

114
00:07:56,870 --> 00:08:03,500
‫But the it's easier to give it the same name because then it's obvious to anyone reading it what should

115
00:08:03,500 --> 00:08:04,460
‫be passed in here.

116
00:08:04,460 --> 00:08:09,590
‫Then over in the C++ file, we need to match the list of parameters.

117
00:08:09,590 --> 00:08:13,790
‫So again, you can just do this by copying and pasting or by typing it out.

118
00:08:13,790 --> 00:08:18,350
‫If you type it out, make sure you don't make any typos because the compiler will complain.

119
00:08:18,350 --> 00:08:24,740
‫So we're going to have a float called Delta Time passed in as a parameter or defined as a parameter

120
00:08:24,740 --> 00:08:30,800
‫to move platform, but we haven't actually passed it in as an argument to move platform in tick.

121
00:08:30,800 --> 00:08:33,800
‫And you'll see I've got a little red squiggly line here.

122
00:08:33,800 --> 00:08:39,860
‫If I hover over it, it says there's too few arguments in the function call because I have defined it

123
00:08:39,860 --> 00:08:46,400
‫in the header file and now in the C++ as well as requiring one parameter of type float.

124
00:08:46,670 --> 00:08:48,770
‫So I must provide that parameter.

125
00:08:48,770 --> 00:08:54,140
‫So I'm going to make sure that the parameter I'm providing, the value that I'm providing comes from

126
00:08:54,140 --> 00:08:58,760
‫the delta time variable or the Delta time parameter from tick.

127
00:08:58,940 --> 00:09:03,950
‫So with threading the value, the value gets passed into tick, then gets passed into MOVE platform

128
00:09:03,950 --> 00:09:11,360
‫and ends up in our parameter in the MOVE platform function where it can be used to execute our MOVE

129
00:09:11,360 --> 00:09:12,830
‫platform functionality.

130
00:09:12,830 --> 00:09:18,950
‫So I'd like you to create your own rotate platform function that we're going to call from TIC.

131
00:09:18,950 --> 00:09:23,720
‫So you're going to add it to the header file like we've done for our MOVE platform function.

132
00:09:23,720 --> 00:09:26,390
‫We're going to add it to the C++ file in the same way as well.

133
00:09:26,390 --> 00:09:34,190
‫So take your hints from the way we did it for those functions and then call it from the tick function.

134
00:09:34,190 --> 00:09:40,250
‫Make sure you pass in Delta time and you define any parameters to allow you to pass in the Delta time

135
00:09:40,460 --> 00:09:45,980
‫and implement the body of the Rotate platform so that it logs out a message.

136
00:09:45,980 --> 00:09:51,410
‫It's not actually going to rotate the platform yet, but as a placeholder to make sure that that is

137
00:09:51,410 --> 00:09:56,360
‫actually getting called print a log message using the UI log that we've learnt about.

138
00:09:56,360 --> 00:09:58,430
‫Pause the video and have a go.

139
00:10:01,720 --> 00:10:02,110
‫Okay.

140
00:10:02,110 --> 00:10:02,900
‫Welcome back.

141
00:10:02,920 --> 00:10:09,130
‫So we're going to start off in the header file and we're mostly going to be doing the same as we did

142
00:10:09,130 --> 00:10:10,030
‫for our MOVE platform.

143
00:10:10,030 --> 00:10:14,590
‫We're going to put it in a private section so that it's only accessible within our class.

144
00:10:14,910 --> 00:10:19,900
‫I'm going to give it a void return type, we're going to call it rotate platform.

145
00:10:19,900 --> 00:10:21,460
‫We're going to open up the parameter list.

146
00:10:21,460 --> 00:10:24,550
‫And in it we're going to put one parameter which is going to be a type float.

147
00:10:24,580 --> 00:10:30,940
‫We're going to call it Delta time and close the parameters and finish off with a semicolon.

148
00:10:31,150 --> 00:10:36,310
‫Now, most of what we do over in the C++ file is exactly the same as in the header file.

149
00:10:36,310 --> 00:10:41,650
‫So I'm actually going to copy this line, go over to the C++ file and show you what is different.

150
00:10:41,650 --> 00:10:47,050
‫So I'm going to go to a new empty line at the end of the file, paste it in, and then what's different

151
00:10:47,050 --> 00:10:50,470
‫is over here we need the scope resolution operator in the class name.

152
00:10:50,470 --> 00:10:58,120
‫So we're going to have a moving platform, colon, colon rotate platform.

153
00:10:58,210 --> 00:11:01,390
‫Then we have the parameter list and then we don't need the semicolon on the end.

154
00:11:01,390 --> 00:11:07,000
‫So remove the semicolon, add a new line and add our curly braces instead and a new line between those

155
00:11:07,000 --> 00:11:08,140
‫two curly braces.

156
00:11:08,620 --> 00:11:12,070
‫So what we want to do in here is print out a log message.

157
00:11:12,070 --> 00:11:15,190
‫So I'm going to type you log hit enter.

158
00:11:15,490 --> 00:11:17,560
‫It's just going to be a simple display message.

159
00:11:17,560 --> 00:11:24,460
‫And in the text I'm going to put the name of our platform.

160
00:11:24,460 --> 00:11:26,800
‫So it's going to be percents.

161
00:11:26,800 --> 00:11:32,590
‫Then we're going to type rotating dot and then I'm going to pass in the name.

162
00:11:32,590 --> 00:11:37,660
‫Now we could put this into a variable, but because it's quite a short line here, I'm just going to

163
00:11:37,660 --> 00:11:45,040
‫type star, get name and then put the parentheses so that we're calling essentially here calling the

164
00:11:45,040 --> 00:11:45,640
‫get name.

165
00:11:45,640 --> 00:11:48,280
‫That is the expression that gets hold of the F string.

166
00:11:48,280 --> 00:11:53,620
‫And then we are converting that result to put into our log message.

167
00:11:53,890 --> 00:12:00,070
‫So from all of our platforms, I'm expecting to see that rotating is printed, but this will only happen

168
00:12:00,070 --> 00:12:01,390
‫if I actually call it in tech.

169
00:12:01,390 --> 00:12:09,430
‫So let's go up to the tick function after MOVE platform can add a new line and call the rotate platform.

170
00:12:09,670 --> 00:12:15,130
‫And we need to you see, when I open up the parentheses, it gives me a hint that I need to pass in

171
00:12:15,130 --> 00:12:16,870
‫an argument for Delta time.

172
00:12:16,870 --> 00:12:21,490
‫So again, we're just going to pass that same Delta time variable from within here.

173
00:12:21,490 --> 00:12:24,850
‫And like I said, the names here don't have to be the same.

174
00:12:24,850 --> 00:12:28,720
‫So this delta time needs to match with the Delta time within tick.

175
00:12:28,990 --> 00:12:35,680
‫But we could choose to call the variable within rotate platform date instead and anywhere we use it

176
00:12:35,680 --> 00:12:40,720
‫within rotate platform, we'd have to use date the name of the parameter and the argument can basically

177
00:12:40,720 --> 00:12:41,470
‫be different.

178
00:12:41,470 --> 00:12:46,870
‫So let's hop over into unreal and hit compile because we've made some changes to the header file.

179
00:12:46,870 --> 00:12:52,990
‫The compilation will be slightly longer than when we just change the C++ file and once that's completed

180
00:12:52,990 --> 00:12:57,820
‫compiling, we're going to go ahead and hit play and open up the output log.

181
00:12:57,970 --> 00:13:04,030
‫And you can see that we are indeed displaying log messages about each of the platforms rotating.

182
00:13:04,030 --> 00:13:08,890
‫So it looks like our function is effectively getting called in tick.

183
00:13:09,010 --> 00:13:09,640
‫Great stuff.

184
00:13:09,640 --> 00:13:15,160
‫Now we understand how to create our own functions that are members of a class.

185
00:13:15,190 --> 00:13:21,490
‫It's very useful tool for reusing a code, but also for organizing it just like it was in Blueprint.

186
00:13:21,820 --> 00:13:23,710
‫I'll see you in the next lecture.

