﻿1
00:00:04,380 --> 00:00:06,150
‫Hello and welcome back.

2
00:00:06,240 --> 00:00:12,150
‫Now we have movement for the --, but we're not rotating that top part of the --, which we're calling

3
00:00:12,150 --> 00:00:12,920
‫the turret.

4
00:00:12,930 --> 00:00:16,290
‫So we'd like to be able to do that using our mouse.

5
00:00:16,320 --> 00:00:18,800
‫Now, take a look at what's going on here.

6
00:00:18,810 --> 00:00:23,300
‫As I'm moving my mouse cursor around in the world, I'm getting this red sphere.

7
00:00:23,310 --> 00:00:27,060
‫This is a debug sphere that I'm drawing in 3D space.

8
00:00:27,300 --> 00:00:33,600
‫Now notice as I move up against a wall, that sphere moves against the surface of the wall.

9
00:00:33,600 --> 00:00:40,410
‫So wherever my mouse cursor is moving, I'm getting this fear that corresponds to whatever object I'm

10
00:00:40,410 --> 00:00:43,620
‫hitting based on my cursor location.

11
00:00:43,620 --> 00:00:50,250
‫And this is going to be useful for us so we can have a location in space that we can rotate our turret

12
00:00:50,250 --> 00:00:51,200
‫towards.

13
00:00:51,210 --> 00:00:57,570
‫Now, in order to do this, there are some functions we need on the controller, so we have to access

14
00:00:57,570 --> 00:00:59,250
‫those functions on the controller.

15
00:00:59,250 --> 00:01:04,140
‫And in this video we're going to work on setting that up and we're also going to discuss how this is

16
00:01:04,140 --> 00:01:07,080
‫going to involve a concept known as casting.

17
00:01:07,080 --> 00:01:12,330
‫Now, the reason we want the controller is because there are functions on the controller that allow

18
00:01:12,330 --> 00:01:14,820
‫us to get the hit result under the cursor.

19
00:01:14,820 --> 00:01:16,860
‫Now we're talking about the mouse cursor.

20
00:01:16,860 --> 00:01:20,310
‫So say this is our screen and here's our mouse cursor.

21
00:01:20,310 --> 00:01:25,530
‫And the point at the tip of the cursor corresponds to a location on the screen.

22
00:01:25,530 --> 00:01:31,740
‫Now if we move the cursor around, our goal is that the turret part of our tank, that top part of our

23
00:01:31,740 --> 00:01:37,290
‫mesh is going to rotate so that it's facing the direction of that cursor.

24
00:01:37,440 --> 00:01:44,370
‫Now if we move the cursor around, there's a function on the controller that we can use to get the location

25
00:01:44,370 --> 00:01:47,190
‫corresponding to that cursor.

26
00:01:47,190 --> 00:01:52,830
‫If we were to draw a line straight out from the camera towards the cursor and hit some object in the

27
00:01:52,830 --> 00:01:59,700
‫world, we can get that location and use that location to set the rotation for the tank turret.

28
00:01:59,730 --> 00:02:00,960
‫Let me show you what I mean.

29
00:02:00,960 --> 00:02:06,420
‫Now, in order to get this hit location corresponding to our cursor, we need to use the controller.

30
00:02:06,420 --> 00:02:09,480
‫Now, let's talk about the controller for a brief moment.

31
00:02:09,780 --> 00:02:11,880
‫Let's say we have our pawn in the world.

32
00:02:11,880 --> 00:02:13,950
‫A pawn will have a controller.

33
00:02:14,130 --> 00:02:18,210
‫This is an invisible object that's associated with the pawn.

34
00:02:18,210 --> 00:02:24,180
‫Now, for our pawn, our controller is going to have the type A player controller and at the beginning

35
00:02:24,180 --> 00:02:27,720
‫of the game, the controller will possess the pawn.

36
00:02:27,750 --> 00:02:32,790
‫This is why we can see from the point of view of the camera that belongs to our pawn tank.

37
00:02:32,790 --> 00:02:34,890
‫Now we can access this controller.

38
00:02:34,920 --> 00:02:38,970
‫Our tank class inherits a function called Get Controller.

39
00:02:38,970 --> 00:02:41,100
‫It's inherited from the -- class.

40
00:02:41,100 --> 00:02:46,950
‫So what we'd like to do is get a hold of that controller and store that in a variable so that we can

41
00:02:46,950 --> 00:02:51,270
‫use it to access the function we need to get the hit result under the cursor.

42
00:02:51,270 --> 00:02:53,010
‫So let's set ourselves up for that.

43
00:02:53,010 --> 00:02:58,650
‫Now I'm here in the tank class and the place we need to access our controller is begin play.

44
00:02:58,680 --> 00:03:05,190
‫If we do it in the constructor, that's too early and we may not have a valid controller just yet.

45
00:03:05,190 --> 00:03:08,910
‫So we're going to need the begin play function here in the tank class.

46
00:03:08,910 --> 00:03:14,250
‫Now we have it here in the base pong class and so far we're not really using begin play in the base

47
00:03:14,250 --> 00:03:15,090
‫pong class.

48
00:03:15,270 --> 00:03:18,450
‫So what I'd like to do is move it over to the tank class.

49
00:03:18,450 --> 00:03:24,150
‫So I'm going to highlight begin play, including the comment and the protected section here and I'm

50
00:03:24,150 --> 00:03:25,500
‫simply going to cut it.

51
00:03:25,500 --> 00:03:28,710
‫And then back in tank stage, I'm going to paste it.

52
00:03:28,710 --> 00:03:33,960
‫Let's add this protected section here just above the private section with begin play inside.

53
00:03:33,960 --> 00:03:36,270
‫Now we've removed Begin Play from base pawn.

54
00:03:36,840 --> 00:03:41,430
‫We no longer have it here, but we also need to remove it from base pond cpp.

55
00:03:41,460 --> 00:03:42,240
‫Here it is.

56
00:03:42,240 --> 00:03:48,960
‫We can go ahead and delete it here and now we have it declared here in Tank H, but we also need to

57
00:03:48,960 --> 00:03:51,330
‫define it in tank CPP.

58
00:03:51,360 --> 00:03:55,830
‫So I'm going to define the function just under setup player input component.

59
00:03:55,830 --> 00:04:00,660
‫So it's going to be void begin play with no input parameters.

60
00:04:00,870 --> 00:04:06,840
‫There's the function body and we also need a tank pre fixing the function name.

61
00:04:06,840 --> 00:04:09,930
‫Now we know it's important to call super big in play.

62
00:04:09,930 --> 00:04:16,320
‫So that way any functionality in the begin play function and any of the parent classes will be still

63
00:04:16,320 --> 00:04:17,010
‫called.

64
00:04:17,340 --> 00:04:18,960
‫So here we have begin play.

65
00:04:18,990 --> 00:04:22,560
‫This is where we can get our controller and store it in a variable.

66
00:04:22,560 --> 00:04:25,920
‫So let's go back to Tank H and create a variable.

67
00:04:25,920 --> 00:04:30,750
‫Now this is going to be private and I'm going to stick it down here at the bottom of the private section

68
00:04:30,750 --> 00:04:38,460
‫and it's going to be a player controller pointer type and we can call this player controller ref.

69
00:04:38,460 --> 00:04:40,920
‫This will be our reference to the player controller.

70
00:04:40,950 --> 00:04:43,590
‫Now we're not talking about a C++ reference.

71
00:04:43,590 --> 00:04:48,750
‫This is still just a pointer, but we're going to use this to refer to the player controller.

72
00:04:48,750 --> 00:04:55,560
‫So now we have a player controller ref variable of type A player controller and we're ready to store

73
00:04:55,590 --> 00:04:57,600
‫the controller in this pointer.

74
00:04:57,600 --> 00:05:02,730
‫So let's go back to Tank Dot CP here and begin play and attempt to do this.

75
00:05:02,730 --> 00:05:03,330
‫So.

76
00:05:03,400 --> 00:05:12,100
‫What I'm going to do is say player, controller, ref equals and I'm going to attempt to call get controller.

77
00:05:12,880 --> 00:05:17,440
‫Now get controller, as I mentioned is on the -- class.

78
00:05:17,470 --> 00:05:24,070
‫Lets right click and go to definition and notice that this function returns an A controller.

79
00:05:24,100 --> 00:05:29,020
‫Now our variable is of type A player controller, not a controller.

80
00:05:29,020 --> 00:05:35,260
‫So what we're attempting to do here is take the return value from get controller, which is of a controller

81
00:05:35,260 --> 00:05:40,270
‫pointer type and we're trying to store it in an A player controller pointer.

82
00:05:40,300 --> 00:05:42,330
‫Notice we see the red squiggly here.

83
00:05:42,340 --> 00:05:43,940
‫It says a value of type.

84
00:05:43,960 --> 00:05:48,080
‫A controller pointer cannot be assigned to an entity of type.

85
00:05:48,100 --> 00:05:49,630
‫A player controller.

86
00:05:49,660 --> 00:05:51,210
‫We're getting an error.

87
00:05:51,220 --> 00:05:55,390
‫Let's take a look here at the documentation for a controller.

88
00:05:55,420 --> 00:05:56,400
‫Take a look.

89
00:05:56,410 --> 00:05:58,030
‫Here's a controller.

90
00:05:58,030 --> 00:06:00,460
‫And it says inheritance, hierarchy.

91
00:06:00,460 --> 00:06:05,690
‫And below a controller we see a a controller and a player controller.

92
00:06:05,710 --> 00:06:10,210
‫This means a player controller is a child class of a controller.

93
00:06:10,240 --> 00:06:14,500
‫Now, in C++, we cannot have a pointer of type.

94
00:06:14,530 --> 00:06:20,200
‫A player controller store the value of the address of an A controller.

95
00:06:20,530 --> 00:06:22,600
‫A controller is the parent type.

96
00:06:22,780 --> 00:06:25,450
‫A player controller is the child type.

97
00:06:25,450 --> 00:06:30,960
‫You cannot take an object of parent type and store it in a pointer of child type.

98
00:06:30,970 --> 00:06:32,680
‫So how do we get around this?

99
00:06:32,680 --> 00:06:34,000
‫So here's our problem.

100
00:06:34,000 --> 00:06:38,630
‫The get controller function returns the address of an A controller.

101
00:06:38,650 --> 00:06:44,410
‫Now, this is something we could store in an A controller pointer, but we can't store it in an A player

102
00:06:44,410 --> 00:06:45,410
‫controller pointer.

103
00:06:45,430 --> 00:06:52,000
‫The funny thing is, though, that the object returned by get controller is in fact a player controller.

104
00:06:52,000 --> 00:06:58,630
‫So we have this address in the form of an A controller pointer and we know it's stores a player controller,

105
00:06:58,630 --> 00:07:03,910
‫but if we have it in the form of an A controller, we cannot access the function that we need because

106
00:07:03,910 --> 00:07:06,210
‫it exists on a player controller.

107
00:07:06,220 --> 00:07:08,810
‫The function is called get hit result under cursor.

108
00:07:08,830 --> 00:07:15,250
‫Now if we have a value of type A controller pointer and we attempt to get that function that exists

109
00:07:15,250 --> 00:07:18,430
‫on a player controller, we will fail to do so.

110
00:07:18,460 --> 00:07:20,020
‫We'll get a compilation error.

111
00:07:20,020 --> 00:07:25,960
‫So since we know the object itself is a player controller, it's just stored in an A controller pointer.

112
00:07:25,960 --> 00:07:27,610
‫How do we get around this problem?

113
00:07:27,610 --> 00:07:29,980
‫The answer is we have to cast.

114
00:07:30,010 --> 00:07:36,100
‫We know that a controller is the parent class and a player controller is the child class.

115
00:07:36,100 --> 00:07:41,320
‫And we know that we cannot have an A controller stored in an A player controller pointer.

116
00:07:41,320 --> 00:07:46,750
‫We know we can go the other way around and have an A controller pointer store, an object of a player

117
00:07:46,750 --> 00:07:47,530
‫controller.

118
00:07:47,620 --> 00:07:53,290
‫That's in fact what we're seeing when we call get controller, it returns an A controller pointer that

119
00:07:53,290 --> 00:07:55,120
‫points to an A player controller.

120
00:07:55,120 --> 00:08:01,810
‫So if we have a value of type A controller pointer, we can use a special function called a cast to

121
00:08:01,810 --> 00:08:05,920
‫convert the type of the pointer to an A player controller pointer.

122
00:08:05,920 --> 00:08:13,720
‫So casting changes the type of one pointer to another pointer type as long as the object itself is the

123
00:08:13,720 --> 00:08:15,640
‫type that we're trying to cast to.

124
00:08:15,670 --> 00:08:21,430
‫Now, if we attempted to cast to an invalid type, for example, we have an A controller pointer pointing

125
00:08:21,430 --> 00:08:23,140
‫to a player controller object.

126
00:08:23,140 --> 00:08:30,070
‫If we attempt to cast to say, a character, the cast function returns null and we say that we fail

127
00:08:30,070 --> 00:08:30,820
‫the cast.

128
00:08:30,820 --> 00:08:33,160
‫So the cast function looks like this.

129
00:08:33,190 --> 00:08:36,790
‫It's a template just like create default sub object.

130
00:08:36,790 --> 00:08:42,850
‫This template function will adapt to the type chosen and in this function, the type we pass in is the

131
00:08:42,850 --> 00:08:44,230
‫type we're casting too.

132
00:08:44,230 --> 00:08:45,400
‫So we pass that type.

133
00:08:45,400 --> 00:08:51,700
‫We'd like to cast two inside the angle brackets and we pass the pointer that we'd like to cast into

134
00:08:51,700 --> 00:08:52,570
‫the parentheses.

135
00:08:52,570 --> 00:08:53,710
‫Here's an example.

136
00:08:53,710 --> 00:08:59,410
‫Let's say we'd like to cast to a player controller and a pointer of type A controller type.

137
00:08:59,410 --> 00:09:04,240
‫We pass the pointer into the parentheses and the type we're casting to in the angled brackets.

138
00:09:04,240 --> 00:09:08,590
‫And what gets returned is a value of type A player controller pointer.

139
00:09:08,590 --> 00:09:13,720
‫So here's the function call where we're passing the type in the angle brackets and the A controller

140
00:09:13,720 --> 00:09:18,550
‫pointer value that we get returned from get controller can go in the parentheses.

141
00:09:18,550 --> 00:09:22,630
‫So we need to cast this value that we're getting returned from get controller.

142
00:09:22,630 --> 00:09:24,250
‫That's going to be your challenge.

143
00:09:24,250 --> 00:09:31,840
‫So in Begin Play, use the get controller function and pass that value in to the cast function and cast

144
00:09:31,840 --> 00:09:33,550
‫to a player controller.

145
00:09:33,550 --> 00:09:39,490
‫And then you can store the result in our player controller ref pointer, which is of type A player controller

146
00:09:39,490 --> 00:09:40,120
‫pointer.

147
00:09:40,120 --> 00:09:42,580
‫Pause the video and perform the cast.

148
00:09:45,530 --> 00:09:45,860
‫Okay.

149
00:09:45,860 --> 00:09:49,570
‫So we've almost got all the work done here we just need to cast.

150
00:09:49,580 --> 00:09:56,000
‫So what we're going to do is call the cast function, which has angle brackets as well as parentheses.

151
00:09:56,000 --> 00:09:59,990
‫And what goes in the parentheses is the value that we'd like to cast.

152
00:09:59,990 --> 00:10:05,060
‫Now get controller returns a pointer of Type A controller.

153
00:10:05,060 --> 00:10:07,850
‫I'm going to move that right here into the parentheses.

154
00:10:07,850 --> 00:10:12,110
‫And the type we're casting to is a player controller.

155
00:10:14,050 --> 00:10:17,620
‫So again we're taking the value returned by get comptroller.

156
00:10:17,620 --> 00:10:21,160
‫This returns an a comptroller pointer or an address.

157
00:10:21,160 --> 00:10:25,000
‫Then we're using cast to cast it to an A player controller.

158
00:10:25,000 --> 00:10:31,330
‫We know that the object returned by gate controller is an A player controller object stored in an A

159
00:10:31,330 --> 00:10:32,320
‫controller pointer.

160
00:10:32,320 --> 00:10:38,500
‫So after we've cast this, what gets returned is a pointer of type A player controller.

161
00:10:38,500 --> 00:10:41,410
‫So we're storing that in player controller ref.

162
00:10:41,410 --> 00:10:46,990
‫Now that we have access to the controller in the form of an A player controller pointer, we can access

163
00:10:46,990 --> 00:10:50,830
‫the function we need to get the trace hit result under the cursor.

164
00:10:50,830 --> 00:10:55,570
‫So in summary, we used get controller to gain access to the controller.

165
00:10:55,930 --> 00:11:03,040
‫We also use the cast function to cast from the type A controller pointer to type A player controller

166
00:11:03,040 --> 00:11:03,670
‫pointer.

167
00:11:03,670 --> 00:11:10,000
‫Now we can access the function we need, which is get hit results under cursor which exists on the A

168
00:11:10,000 --> 00:11:11,290
‫player controller class.

169
00:11:11,290 --> 00:11:14,500
‫So we'll start using this function in the next video.

170
00:11:14,860 --> 00:11:15,850
‫I'll see you soon.

