﻿1
00:00:04,420 --> 00:00:06,220
‫Hello and welcome back.

2
00:00:06,250 --> 00:00:13,210
‫Now, we've already created the ability to handle death for the tank and tower, and we saw that this

3
00:00:13,210 --> 00:00:20,200
‫involved some extra steps with our tank since we accessed the player controller to disable input and

4
00:00:20,200 --> 00:00:22,120
‫disable showing the mouse cursor.

5
00:00:22,150 --> 00:00:28,270
‫Now we'd like to have our own custom player controller class so that we can add functionality to it

6
00:00:28,300 --> 00:00:30,210
‫that will handle this for us.

7
00:00:30,220 --> 00:00:36,010
‫And then we'd like to create a blueprint based on this class so that we can access some of the exposed

8
00:00:36,010 --> 00:00:42,580
‫variables on the player controller such as which mouse cursor to show once we actually start showing

9
00:00:42,580 --> 00:00:43,630
‫the mouse cursor.

10
00:00:43,660 --> 00:00:50,020
‫So we're going to create a player controller class and add enabling and disabling input functionality.

11
00:00:50,020 --> 00:00:54,490
‫And then we're going to create a blueprint and set the mouse cursor type in blueprints.

12
00:00:54,520 --> 00:00:58,030
‫Now creating this class is something you know how to do.

13
00:00:58,030 --> 00:01:04,480
‫So you're going to create a player controller class for your challenge, create a new C++ class, and

14
00:01:04,480 --> 00:01:07,300
‫choose player controller as the parent class.

15
00:01:07,300 --> 00:01:11,800
‫Call this tune tanks, player controller or something appropriate like that.

16
00:01:11,800 --> 00:01:15,760
‫And once you're done, create a blueprint based on this class.

17
00:01:15,760 --> 00:01:21,340
‫Now, once you have the blueprint, you can assign this as the default player controller in the game

18
00:01:21,340 --> 00:01:22,330
‫mode blueprint.

19
00:01:22,360 --> 00:01:25,180
‫Do this in the game mode, blueprint itself.

20
00:01:25,360 --> 00:01:30,280
‫Pause the video and create the player controller class for the tank now.

21
00:01:33,230 --> 00:01:33,680
‫Okay.

22
00:01:33,680 --> 00:01:34,790
‫So I'm back in the editor.

23
00:01:34,790 --> 00:01:41,210
‫I'm going to right click click new C++ class and scroll down and I'll see that player controller is

24
00:01:41,210 --> 00:01:42,170
‫an option here.

25
00:01:42,170 --> 00:01:48,320
‫So I'm going to select player controller and name my new player controller tune tanks.

26
00:01:49,110 --> 00:01:53,190
‫Player, controller and click create class.

27
00:01:53,190 --> 00:01:56,790
‫And here I have my new player controller class.

28
00:01:56,820 --> 00:02:00,030
‫Now the next step is to create a blueprint based on this.

29
00:02:00,030 --> 00:02:06,060
‫So I'm back in the editor and I didn't tell you to do this, but I'm going to create a new folder here

30
00:02:06,060 --> 00:02:09,360
‫in my blueprints folder called Player Controller.

31
00:02:10,440 --> 00:02:15,480
‫And if you'd like the same folder structure, you can create this folder now if you haven't already

32
00:02:15,480 --> 00:02:19,170
‫done so and then move your player controller blueprint into this.

33
00:02:19,170 --> 00:02:23,610
‫If you don't know how to move a blueprint from one folder to another, it's quite simple.

34
00:02:23,640 --> 00:02:30,810
‫You go to the folder that contains the blueprint, click and drag into the correct folder and click

35
00:02:30,810 --> 00:02:31,740
‫move here.

36
00:02:31,890 --> 00:02:36,900
‫Now, I don't want to move my projectile to this folder, so I'm just going to click here in the content

37
00:02:36,900 --> 00:02:38,760
‫browser to cancel that action.

38
00:02:38,760 --> 00:02:41,370
‫So now I have a player controller folder.

39
00:02:41,370 --> 00:02:47,760
‫I'm going to right click and click Blueprint Class and in this search I'm going to look for toon tanks,

40
00:02:47,760 --> 00:02:51,460
‫player controller and select that class and click Select.

41
00:02:51,480 --> 00:02:57,180
‫Now I can call this beep underscore toon tanks player controller.

42
00:02:57,390 --> 00:02:59,070
‫So I now have a player controller.

43
00:02:59,070 --> 00:03:02,100
‫Now I need to set this as the default player controller.

44
00:03:02,100 --> 00:03:08,220
‫So I'm going to go to the game mode folder, open beep, tune tanks, game mode and open the full blueprint.

45
00:03:08,220 --> 00:03:11,670
‫Editor And here I see player controller class.

46
00:03:11,940 --> 00:03:18,690
‫I'm going to click the dropdown and select BP tune tanks, player controller and compile and save and

47
00:03:18,690 --> 00:03:19,950
‫close out of that.

48
00:03:19,950 --> 00:03:23,250
‫So now we have the custom player controller class.

49
00:03:23,250 --> 00:03:25,680
‫We're ready to add some functionality to this.

50
00:03:25,680 --> 00:03:31,110
‫So what I'd like is a function here that can set the player enabled state, whether or not the player

51
00:03:31,110 --> 00:03:34,890
‫is enabled to add input that will move the tank.

52
00:03:34,890 --> 00:03:39,690
‫Now I plan on calling this from the game mode when the actor dies.

53
00:03:39,690 --> 00:03:44,940
‫So I'm going to make a public section here and the function is going to be void.

54
00:03:44,940 --> 00:03:55,020
‫And I'm simply going to call this set player enabled state, which can take a boolean called B player

55
00:03:55,020 --> 00:03:55,950
‫enabled.

56
00:03:56,550 --> 00:04:01,830
‫This way, if we call this function, we can either pass in true if we'd like the player to be enabled

57
00:04:01,830 --> 00:04:04,620
‫or false if we'd like it to be disabled.

58
00:04:04,620 --> 00:04:09,090
‫So let's implement this function in tune tanks, player, controller.

59
00:04:09,360 --> 00:04:14,100
‫CP So here's my function and of course I need the class name here.

60
00:04:14,100 --> 00:04:20,370
‫So it's going to be a tune tanks, player, controller, and there's the scope resolution operator.

61
00:04:20,370 --> 00:04:22,860
‫Now, this function is going to be quite simple.

62
00:04:22,860 --> 00:04:28,320
‫It's going to check the value of B player enabled, and we can do that with a simple if check.

63
00:04:28,320 --> 00:04:35,460
‫So I'm going to say if B player enabled and I'd like to access the pawn and enable or disable input

64
00:04:35,460 --> 00:04:41,790
‫and the way to access the pawn that this player controller is possessing is to use the function, get

65
00:04:41,790 --> 00:04:42,390
‫pawn.

66
00:04:42,390 --> 00:04:47,520
‫This returns and a pawn and the pawn class has the function.

67
00:04:47,520 --> 00:04:55,860
‫Enable input and enable input as we've seen requires a player controller so we can pass in this.

68
00:04:56,190 --> 00:05:02,490
‫Now I'm getting the red squiggles because we need to include that pawn class type and we can easily

69
00:05:02,490 --> 00:05:09,090
‫see the include for the pawn by either going to the unreal engine documentation for a pawn, or we could

70
00:05:09,090 --> 00:05:13,890
‫simply go to our base pawn class which inherits from a pawn.

71
00:05:14,070 --> 00:05:19,890
‫And because it inherits from a pawn, we've seen that it needs to include the pawn header there so I

72
00:05:19,890 --> 00:05:23,430
‫can copy that include it's game framework, a slash pawn.

73
00:05:24,150 --> 00:05:26,850
‫So back in tune tanks player controller.

74
00:05:26,850 --> 00:05:30,900
‫I can paste that include here at the top to get rid of those red squiggles.

75
00:05:30,900 --> 00:05:37,470
‫So this takes care of the case when B player enabled is true, but we also need an LS case here.

76
00:05:38,160 --> 00:05:45,210
‫In the LS case we're going to call get pawn and there's a function just like enable input that is designed

77
00:05:45,210 --> 00:05:50,580
‫to disable input and it's called, as you might guess, disable input.

78
00:05:50,580 --> 00:05:55,020
‫And this also requires a player controller for which we will pass in this.

79
00:05:55,020 --> 00:06:00,480
‫Now the player controller has that be show mouse cursor and we're going to set that as well.

80
00:06:00,480 --> 00:06:04,500
‫And we'd like to set it based on whether or not the player should be enabled.

81
00:06:04,500 --> 00:06:10,950
‫So whatever the Boolean value of B player enabled, we're going to set B show mouse cursor to that value.

82
00:06:10,950 --> 00:06:18,090
‫So we're going to say B sho mouse cursor equals B player enabled.

83
00:06:18,360 --> 00:06:20,730
‫Now we haven't shown the mouse cursor yet.

84
00:06:20,730 --> 00:06:21,990
‫We're going to get to that part.

85
00:06:21,990 --> 00:06:27,210
‫We'd actually like to see the mouse cursor while we're playing the game so we know where our tank is

86
00:06:27,210 --> 00:06:27,960
‫aiming.

87
00:06:27,960 --> 00:06:29,490
‫So we'll get to that soon.

88
00:06:29,490 --> 00:06:35,490
‫But for now we have this nice function in the player controller and we'd like to call this function

89
00:06:35,490 --> 00:06:38,550
‫from the game mode now to show you what I mean.

90
00:06:38,550 --> 00:06:44,610
‫I'm going back to game mode and here's the actor died function and you'll see that we're disabling input

91
00:06:44,610 --> 00:06:47,070
‫and we're setting b show mouse cursor here.

92
00:06:47,070 --> 00:06:48,450
‫So the reason I'd like to have a.

93
00:06:48,540 --> 00:06:54,420
‫Function on the player controller so that we can simply call that function passing in a boolean value.

94
00:06:54,450 --> 00:07:00,600
‫In this case, we'd pass in false so that we'd get that disable input function called and B so mouse

95
00:07:00,600 --> 00:07:02,570
‫cursor would be set to false.

96
00:07:02,580 --> 00:07:08,880
‫So we can simply call that function here now rather than calling tank get player controller which returns

97
00:07:08,880 --> 00:07:09,960
‫a player controller.

98
00:07:10,050 --> 00:07:15,590
‫I'd like to have a pointer variable that allows us to access that player controller.

99
00:07:15,600 --> 00:07:20,560
‫So here in tune tanks game modes, I'm going to add a private variable.

100
00:07:20,580 --> 00:07:27,990
‫I'm going to forward declare it and it's going to be of type A tune tanks, player, controller, that's

101
00:07:27,990 --> 00:07:28,800
‫a pointer.

102
00:07:28,800 --> 00:07:35,640
‫And I'm going to call this tune Tank's Player Controller and I can set this here in tune tanks game

103
00:07:35,640 --> 00:07:39,500
‫mode and begin play using gameplay statics.

104
00:07:39,510 --> 00:07:45,360
‫Now if you are wondering if game play statics had a get player controller function, you were right.

105
00:07:45,570 --> 00:07:51,630
‫This is one of those handy static functions that requires a world context object and a player index

106
00:07:51,630 --> 00:07:58,020
‫so we can go ahead and copy the include for gameplay statics and call this here in Begin Play.

107
00:07:58,020 --> 00:08:02,970
‫So I see at the top here, I already have gameplay statics, that's because we're using it already here

108
00:08:02,970 --> 00:08:05,310
‫and begin play to call get player --.

109
00:08:05,310 --> 00:08:09,350
‫We're going to do the same thing here to set the player controller pointer.

110
00:08:09,360 --> 00:08:11,400
‫Now this is something you should know how to do.

111
00:08:11,400 --> 00:08:16,680
‫So why don't you pause the video and do a little mini challenge here and just set the value of tune

112
00:08:16,680 --> 00:08:18,060
‫tanks, player controller.

113
00:08:20,990 --> 00:08:21,470
‫Okay.

114
00:08:21,470 --> 00:08:28,760
‫So we're going to say tune tanks, player, controller, and we're setting it equal to the result of

115
00:08:28,760 --> 00:08:37,550
‫you gameplay statics get player controller and we know the world context object can be this and the

116
00:08:37,550 --> 00:08:40,160
‫player index is not nine but zero.

117
00:08:40,160 --> 00:08:45,920
‫And of course we know that this returns a player controller and not a tune tanks player controller.

118
00:08:45,920 --> 00:08:49,460
‫So just like we did with Tank here, we're going to cast.

119
00:08:49,460 --> 00:08:58,970
‫So I'm going to say cast to a tune tanks player controller and we're casting the result of get player

120
00:08:58,970 --> 00:08:59,720
‫controller.

121
00:08:59,930 --> 00:09:05,270
‫So now after Begin Play, we will have the tune tanks, player controller.

122
00:09:05,270 --> 00:09:11,870
‫And now we can come up here and instead of getting the player controller using get player controller

123
00:09:11,870 --> 00:09:19,430
‫directly from the tank, we can check to make sure our variable tune tanks player controller is valid

124
00:09:19,430 --> 00:09:21,830
‫and then we can call that function we made.

125
00:09:21,830 --> 00:09:23,090
‫Let's go back and look at it.

126
00:09:23,090 --> 00:09:30,620
‫It's called set player enabled state and here in the game mode we'd like to disable input and stop showing

127
00:09:30,620 --> 00:09:33,650
‫the cursor, which means we're going to pass in false for this.

128
00:09:33,650 --> 00:09:43,490
‫So we're going to say tune tanks, player, controller, set player enabled state and we're passing

129
00:09:43,490 --> 00:09:49,340
‫in false for this and we're using tune tanks, player controller, which means we need to include tune

130
00:09:49,340 --> 00:09:50,300
‫tanks, player controller.

131
00:09:50,300 --> 00:09:56,570
‫So here at the top we're going to include tune tanks, player controller.

132
00:09:57,620 --> 00:10:00,890
‫And now we have this type defined OC.

133
00:10:00,890 --> 00:10:02,630
‫Let's go ahead and compile this.

134
00:10:02,780 --> 00:10:08,630
‫And the last thing we'll do before we wrap this up is go into our player controller blueprint and I'd

135
00:10:08,630 --> 00:10:15,320
‫like you to scroll down to the mouse interface section and you'll see that it says Default Mouse Cursor.

136
00:10:15,350 --> 00:10:19,730
‫Now we can click the dropdown and there are a number of different cursors to choose from.

137
00:10:19,730 --> 00:10:21,770
‫And I'm going to choose Crosshairs.

138
00:10:21,950 --> 00:10:27,830
‫Now, we're not showing the mouse cursor yet, but once we start the game we're going to show the cursor.

139
00:10:27,830 --> 00:10:33,920
‫And when we do, I like it to be crosshairs instead of the default white arrow that we always see when

140
00:10:33,920 --> 00:10:35,060
‫we look at the mouse cursor.

141
00:10:35,060 --> 00:10:41,330
‫So set default mouse cursor to crosshairs and we'll be able to see those crosshairs once we actually

142
00:10:41,330 --> 00:10:43,040
‫enable the mouse cursor.

143
00:10:43,040 --> 00:10:50,540
‫So in summary, we created a custom player controller class and we're using that to enable or disable

144
00:10:50,540 --> 00:10:53,420
‫input with a function that we placed on that class.

145
00:10:53,420 --> 00:10:59,420
‫And we're also using that function to show the mouse cursor or hide the mouse cursor based on the Boolean

146
00:10:59,420 --> 00:11:00,290
‫passed in.

147
00:11:00,290 --> 00:11:06,350
‫And in the blueprint that we created, we set the mouse cursor type and we're going to see very soon

148
00:11:06,350 --> 00:11:12,140
‫what that crosshairs mouse cursor looks like when we start showing the mouse cursor when the game starts.

149
00:11:12,140 --> 00:11:12,980
‫So great job.

150
00:11:12,980 --> 00:11:18,620
‫We now have this player controller and we're ready to continue implementing game functionality.

151
00:11:18,620 --> 00:11:20,810
‫So I'll see you in the next video.

