﻿1
00:00:04,680 --> 00:00:05,640
‫Hello and welcome.

2
00:00:05,640 --> 00:00:10,950
‫In this lecture, we are going to be making the win condition work so I can kill all of the enemies

3
00:00:10,950 --> 00:00:11,910
‫in my level.

4
00:00:11,910 --> 00:00:14,220
‫And it will show me this windscreen.

5
00:00:14,220 --> 00:00:14,780
‫Hurrah!

6
00:00:14,790 --> 00:00:16,560
‫Let's dive in and see how it's done.

7
00:00:17,800 --> 00:00:22,210
‫So we'd like it to be possible for our player to actually win this game.

8
00:00:22,210 --> 00:00:23,740
‫At the moment they cannot.

9
00:00:23,950 --> 00:00:25,090
‫So how are we going to do that?

10
00:00:25,120 --> 00:00:27,070
‫Well, you could do it in a number of ways.

11
00:00:27,070 --> 00:00:33,370
‫One of them would be when Pawn Killed is called and the pawn is not the player controller but an AI.

12
00:00:33,400 --> 00:00:39,610
‫We could subtract from a variable which says how many AI there are in the world, how many we have to

13
00:00:39,610 --> 00:00:40,090
‫kill.

14
00:00:40,090 --> 00:00:43,830
‫And then when that goes down to zero, we can end the game in favor of the player.

15
00:00:43,840 --> 00:00:49,510
‫However, I think that's a little bit fragile because it requires us to put how many enemies there are

16
00:00:49,510 --> 00:00:51,070
‫in the world in two places.

17
00:00:51,070 --> 00:00:55,450
‫Essentially, it needs to be in the game mode, but also it needs to be in the world when you're placing

18
00:00:55,450 --> 00:00:59,530
‫the enemies down and those two can get out of sync, and that's going to cause some problems.

19
00:00:59,740 --> 00:01:05,740
‫So the way I would prefer to do this is to simply say, let's count all of the enemies in the world

20
00:01:05,740 --> 00:01:11,800
‫every time a pawn is killed, let's do a recount, a head count of all of the AI and see which ones

21
00:01:11,800 --> 00:01:13,060
‫are still alive.

22
00:01:13,060 --> 00:01:16,600
‫If any of them are still alive, then we're not going to end the game yet.

23
00:01:16,600 --> 00:01:21,460
‫Basically, the game's not over, but if they are all dead and the play controller is not, then the

24
00:01:21,460 --> 00:01:23,110
‫player has won.

25
00:01:23,530 --> 00:01:29,230
‫So in order for us to do this, we need to have a way to query a player controller as to whether it's

26
00:01:29,230 --> 00:01:30,070
‫dead or not.

27
00:01:30,070 --> 00:01:32,140
‫Sorry, I mean, the AI controller.

28
00:01:32,290 --> 00:01:34,030
‫So let's look for our shooter.

29
00:01:34,030 --> 00:01:35,260
‫AI controller.

30
00:01:36,370 --> 00:01:41,800
‫And what we want to do here is create a new public method that allows us to query whether it is dead

31
00:01:41,800 --> 00:01:42,400
‫or not.

32
00:01:42,400 --> 00:01:49,330
‫So we're going to do a ball is basically is dead and it's going to be a contest because it shouldn't

33
00:01:49,330 --> 00:01:52,930
‫change what's actually happening in the controller when it's called.

34
00:01:53,440 --> 00:01:55,780
‫And we can go ahead and create an implementation.

35
00:01:56,510 --> 00:02:01,780
‫Now we should be controlling a -- that can tell us whether or not it is dead.

36
00:02:01,790 --> 00:02:07,760
‫If we go and have a look at that again, we have a look at the shooter character, which is a --.

37
00:02:07,760 --> 00:02:12,160
‫Remember, we have already this is dead cannot function there.

38
00:02:12,170 --> 00:02:17,390
‫So essentially what we want to do is just pass on the request to the shooter I control now.

39
00:02:17,390 --> 00:02:17,990
‫Yes.

40
00:02:17,990 --> 00:02:23,780
‫One thing is you could say in the KML game mode, you could get hold of the controller, get hold of

41
00:02:23,780 --> 00:02:30,380
‫the -- that the that that's controlling, do a cast and then check whether that -- is dead.

42
00:02:30,380 --> 00:02:36,410
‫However, that's generally not best practice because it means that if suddenly something changes about

43
00:02:36,410 --> 00:02:43,520
‫how we calculate death and or the AI controller is going to control different types of --, suddenly

44
00:02:43,520 --> 00:02:46,880
‫we need to go and change the game mode, which doesn't make sense.

45
00:02:46,880 --> 00:02:52,580
‫So it's better to do this and do the passing on inside the AI control is more robust.

46
00:02:52,580 --> 00:02:58,790
‫It stops us from fiddling around inside the internals of somebody else's class, which generally means

47
00:02:58,790 --> 00:03:04,340
‫that we can restrict when things change, we can restrict how many classes we have to go and touch and

48
00:03:04,340 --> 00:03:06,920
‫update and change, which is always a good thing.

49
00:03:07,400 --> 00:03:13,160
‫So what we're going to want to do is get the -- and cast the -- to make sure it is a shooter character.

50
00:03:13,160 --> 00:03:14,450
‫So let's do a cast.

51
00:03:14,690 --> 00:03:17,270
‫It's going to be to a shooter.

52
00:03:18,040 --> 00:03:18,970
‫Character.

53
00:03:19,360 --> 00:03:24,010
‫And I think we're going to need to do a hash include at the top of our eye control to make sure we know

54
00:03:24,010 --> 00:03:25,960
‫about the shooter character like so.

55
00:03:26,110 --> 00:03:34,360
‫And then we're going to do the cast doing a get -- and we're going to want to store this in a variable

56
00:03:34,360 --> 00:03:37,000
‫so it's going to be a shooter.

57
00:03:37,000 --> 00:03:42,670
‫Character pointer called let's call it controlled.

58
00:03:43,830 --> 00:03:44,730
‫Character.

59
00:03:46,030 --> 00:03:52,390
‫I can't just call it character because that's already a variable on the AI controller and put a semicolon

60
00:03:52,390 --> 00:03:56,740
‫at the end of the line and we're going to check whether this is null or not.

61
00:03:56,980 --> 00:04:03,790
‫So control character is not equal to null pointer, then we should be able to query it for its status

62
00:04:03,790 --> 00:04:04,980
‫as to whether it's dead or not.

63
00:04:04,990 --> 00:04:10,180
‫So we should be able to return that the controlled character.

64
00:04:11,100 --> 00:04:12,480
‫Is dead.

65
00:04:12,810 --> 00:04:18,870
‫Basically, we forward on the request to control character if that control character is a shooter character.

66
00:04:18,990 --> 00:04:22,350
‫However, notice here we don't have a return statement.

67
00:04:22,350 --> 00:04:27,630
‫In the other case, we do need to have a return statement that always needs to be a return if the return

68
00:04:27,630 --> 00:04:29,130
‫value isn't void.

69
00:04:29,130 --> 00:04:35,430
‫So we need to put a return statement out here outside of the if statement to catch the case where we

70
00:04:35,430 --> 00:04:37,950
‫haven't already returned inside the if statement.

71
00:04:38,040 --> 00:04:40,950
‫And here we're just going to say, is this character dead?

72
00:04:40,950 --> 00:04:46,500
‫Well, yes, essentially, if it isn't so true, basically it is dead.

73
00:04:46,590 --> 00:04:52,680
‫If we don't have a pawn, if we're not controlling a pawn, then essentially this controller is as good

74
00:04:52,680 --> 00:04:53,850
‫as dead to us.

75
00:04:54,030 --> 00:05:00,360
‫And that could happen because remember that the pawn detaches from its AI controller when it dies.

76
00:05:01,020 --> 00:05:06,630
‫So we're going to go back over in to the game mode and what are we going to want to do here?

77
00:05:07,110 --> 00:05:16,650
‫Essentially what we want to do is a for loop over all of the I the shooter A.I..

78
00:05:17,760 --> 00:05:19,380
‫In world.

79
00:05:20,200 --> 00:05:22,450
‫And with that for loop.

80
00:05:22,570 --> 00:05:23,740
‫What do we want to do?

81
00:05:23,770 --> 00:05:25,990
‫We want to see is dead.

82
00:05:27,460 --> 00:05:32,380
‫Basically and or basically is not dead.

83
00:05:33,040 --> 00:05:39,390
‫And if any of them are not dead, then we instantly know we don't have to check all of the eye at all.

84
00:05:39,400 --> 00:05:44,110
‫If even just one A.I. remains alive, then the game is not over.

85
00:05:44,110 --> 00:05:49,180
‫So we can do what's known as an early return, which you've probably seen before, which is going to

86
00:05:49,180 --> 00:05:54,220
‫just exit from this function without executing any more of the code in it.

87
00:05:54,220 --> 00:05:58,570
‫So everything after this early return will stop executing the for loop, will stop executing anything

88
00:05:58,570 --> 00:06:00,010
‫after it will stop executing.

89
00:06:00,550 --> 00:06:07,240
‫But then we can say if we manage to successfully get through this full loop without it early returning,

90
00:06:07,240 --> 00:06:14,500
‫meaning that there are no living A.I. in the world, then we need to go ahead and end the game.

91
00:06:15,130 --> 00:06:16,060
‫Simple as that.

92
00:06:16,090 --> 00:06:22,060
‫Now, one part of the puzzle that we need to look at here is the end game.

93
00:06:22,060 --> 00:06:25,860
‫Cause our game has ended on the player controller.

94
00:06:25,870 --> 00:06:29,230
‫Let's have a look at how the shooter player controller currently deals with this.

95
00:06:29,230 --> 00:06:35,740
‫If we go over to its CP, what we currently see is that it's currently totally ignoring the B is winner

96
00:06:35,740 --> 00:06:38,560
‫is currently ignoring whether or not it is the winner.

97
00:06:39,190 --> 00:06:44,140
‫So what we need to do is say if B is winner.

98
00:06:45,000 --> 00:06:48,600
‫Then we're going to do something otherwise else.

99
00:06:48,960 --> 00:06:52,090
‫We are going to display our lose screen.

100
00:06:52,110 --> 00:06:55,930
‫So cut these lines here that are responsible for displaying the lose screen.

101
00:06:55,950 --> 00:07:00,210
‫Put that in the alt statement and we'll restart in both cases anyway.

102
00:07:00,690 --> 00:07:02,630
‫So currently the B is winner.

103
00:07:02,640 --> 00:07:03,540
‫What are we going to do?

104
00:07:03,540 --> 00:07:11,610
‫Well, we're just going to go ahead and probably create a wind screen as well, which is going to be

105
00:07:11,610 --> 00:07:12,440
‫fairly straightforward.

106
00:07:12,460 --> 00:07:16,410
‫We go over to our header file for the shooter player controller.

107
00:07:16,410 --> 00:07:22,440
‫We can duplicate our lose screen class and make a win screen class in here.

108
00:07:22,860 --> 00:07:24,750
‫We can also go ahead.

109
00:07:24,750 --> 00:07:26,160
‫And in fact, this is a mini challenge for you.

110
00:07:26,160 --> 00:07:29,650
‫Maybe go ahead and set it up so that it can do a windscreen.

111
00:07:29,970 --> 00:07:30,130
‫Okay.

112
00:07:30,240 --> 00:07:30,690
‫Welcome back.

113
00:07:30,690 --> 00:07:33,150
‫So hopefully you've given that a shot.

114
00:07:33,150 --> 00:07:40,890
‫We're going to just go in and put windscreen here instead of all of the loses and copy essentially everything

115
00:07:40,890 --> 00:07:41,550
‫else.

116
00:07:42,960 --> 00:07:43,800
‫Like so.

117
00:07:44,860 --> 00:07:51,880
‫We're going to go ahead and compile this, then head on over into the editor and find ourselves the

118
00:07:51,880 --> 00:07:53,650
‫player controller class.

119
00:07:53,650 --> 00:07:58,540
‫And we should now see there is an empty windscreen class and what we're going to do is go over into

120
00:07:58,540 --> 00:08:04,540
‫the UI and I'm going to duplicate the lose screen to call the windscreen, and you could create yours

121
00:08:04,540 --> 00:08:05,800
‫from scratch if you wanted to.

122
00:08:05,800 --> 00:08:11,980
‫But I'm just going to be using it very similarly, except I'm going to change out the colour for green.

123
00:08:12,650 --> 00:08:17,180
‫And make it into a you win instead of a you lose.

124
00:08:18,030 --> 00:08:21,870
‫Because the restart logic is effectively just going to be the same.

125
00:08:21,870 --> 00:08:23,400
‫Some reason it reset my color there.

126
00:08:23,400 --> 00:08:25,670
‫I'm just going to go and set the color to green again.

127
00:08:25,680 --> 00:08:26,460
‫Okay, so that's great.

128
00:08:26,460 --> 00:08:31,740
‫But we still have this lump of pseudo code over in the KML game mode that has not been filled out.

129
00:08:31,740 --> 00:08:34,840
‫And that's because it is your challenge to calculate the win.

130
00:08:34,860 --> 00:08:36,480
‫Fill out the pseudocode.

131
00:08:36,600 --> 00:08:40,680
‫Use the T to range from below in that class to help you out.

132
00:08:40,710 --> 00:08:45,390
‫Use the new is dead function that we created on the controller and test it out.

133
00:08:45,420 --> 00:08:49,500
‫See if your windscreen shows up when you kill all the AI in the world.

134
00:08:49,500 --> 00:08:53,670
‫So just going to leave it with the pseudo code on your screen for you to have a look at.

135
00:08:54,090 --> 00:08:55,020
‫K Welcome back.

136
00:08:55,020 --> 00:08:56,490
‫So let's start with the for loop.

137
00:08:56,610 --> 00:08:57,720
‫So we're going to do a four.

138
00:08:57,720 --> 00:08:59,100
‫What do we for looping over?

139
00:08:59,100 --> 00:09:00,690
‫It's not a controller at this time.

140
00:09:00,690 --> 00:09:03,660
‫It's going to be the shooter AI controller.

141
00:09:03,660 --> 00:09:05,340
‫So we need to hash include that.

142
00:09:06,210 --> 00:09:09,570
‫It's going to be the shooter.

143
00:09:10,360 --> 00:09:12,340
‫I controller h.

144
00:09:13,050 --> 00:09:19,620
‫And that means that we're going to be looping over a shooter, A.I. controller, star.

145
00:09:20,190 --> 00:09:29,460
‫Just call the controller for shorts and then we're going to do a t actor range over again that a shooter

146
00:09:29,460 --> 00:09:30,120
‫A.I. controller.

147
00:09:30,120 --> 00:09:34,140
‫We need to get these two types to match then get world.

148
00:09:34,230 --> 00:09:36,540
‫So we're doing it in this level.

149
00:09:37,740 --> 00:09:42,480
‫And then we've got the body of the for loop here where we're going to be asking if it's not dead.

150
00:09:42,480 --> 00:09:48,120
‫So if not, controller arrow is dead.

151
00:09:49,720 --> 00:09:52,510
‫Basically we're saying make sure that we aren't dead.

152
00:09:52,600 --> 00:09:58,360
‫And if it's not dead, then we just go ahead and return to this early return because we know that the

153
00:09:58,360 --> 00:10:00,180
‫game can't be over just yet.

154
00:10:00,190 --> 00:10:06,910
‫And otherwise, if we got through the hole for loop without returning, we do need to end the game because

155
00:10:06,910 --> 00:10:10,330
‫the player must have one killed all of the AI in the level.

156
00:10:10,480 --> 00:10:13,180
‫So in this case we could do an end game.

157
00:10:13,180 --> 00:10:13,900
‫True.

158
00:10:13,900 --> 00:10:15,310
‫Because the player has one.

159
00:10:15,310 --> 00:10:20,440
‫And that should notify all of the player controllers and controllers around.

160
00:10:20,830 --> 00:10:21,430
‫Cool.

161
00:10:21,430 --> 00:10:24,040
‫So let's go over and compile that.

162
00:10:24,040 --> 00:10:27,850
‫First of all, then I'm going to go over into Unreal and have a play.

163
00:10:27,850 --> 00:10:32,410
‫Some reason my editor thinks that I'm in this edit specified color operation, so I'm just going to

164
00:10:32,410 --> 00:10:33,940
‫close it down and restart it.

165
00:10:33,940 --> 00:10:38,680
‫Now that it's rebooted, I'm going to go ahead and hit play and I'm going to get killed.

166
00:10:38,680 --> 00:10:39,190
‫First of all.

167
00:10:39,190 --> 00:10:40,450
‫Yes, I still lose.

168
00:10:40,450 --> 00:10:41,140
‫That's great.

169
00:10:41,140 --> 00:10:44,020
‫Now let's see if I can kill this guy before he kills me.

170
00:10:44,730 --> 00:10:45,270
‫Let's go.

171
00:10:45,270 --> 00:10:46,980
‫Boom, boom, boom, boom, boom, boom, boom.

172
00:10:47,250 --> 00:10:48,180
‫I lose too quickly.

173
00:10:48,180 --> 00:10:52,170
‫Let's go and find the gun again and set.

174
00:10:52,170 --> 00:10:56,490
‫We have that damage from the gun really high for testing purposes.

175
00:10:56,490 --> 00:11:00,720
‫I'm going to go and set the damage down back to maybe 20 this time.

176
00:11:00,720 --> 00:11:03,240
‫So it's still fast, but not as fast.

177
00:11:03,270 --> 00:11:06,420
‫Go and play and hit this guy a few times.

178
00:11:06,420 --> 00:11:12,210
‫And what we saw there was that actually I did win because the level restarted.

179
00:11:12,210 --> 00:11:15,660
‫So if I go and kill this guy again, you see nothing's happening.

180
00:11:15,660 --> 00:11:19,860
‫It's saying in the message log that create which it was called with a null class.

181
00:11:19,860 --> 00:11:25,890
‫And the reason for that, if we go over to our BP shooter player controller again, you can see we haven't

182
00:11:25,890 --> 00:11:27,360
‫hooked up our windscreen.

183
00:11:27,450 --> 00:11:31,350
‫One thing that I forgot to do, so you can go and compile, save and play.

184
00:11:31,350 --> 00:11:37,980
‫And now hopefully if I kill this guy to New Doom, I win and I'm going to restart in 5 seconds.

185
00:11:37,980 --> 00:11:40,950
‫And I want to show you that the same is true.

186
00:11:41,100 --> 00:11:46,290
‫If I put more player controllers in the world, it's going to require me to kill both of them.

187
00:11:46,290 --> 00:11:50,160
‫So let's go and bring in the shooter character over here.

188
00:11:50,610 --> 00:11:52,950
‫And this is going to be my second enemy to kill.

189
00:11:52,950 --> 00:11:54,420
‫So kill this guy, boom.

190
00:11:54,750 --> 00:11:56,850
‫And it hasn't won.

191
00:11:56,850 --> 00:11:59,310
‫Then I go over to the second guy, boom.

192
00:11:59,310 --> 00:12:04,440
‫And now I have one because I've killed all of the AI and we didn't have to go and change any variables

193
00:12:04,440 --> 00:12:09,420
‫to do that because our for loop counts all of the living A.I. every time from scratch.

194
00:12:09,420 --> 00:12:12,270
‫So currently we're now living in quite a silent world.

195
00:12:12,270 --> 00:12:18,330
‫So in the next lecture, I want to start looking a little bit at how we can do some audio.

196
00:12:18,360 --> 00:12:19,290
‫I'll see you there.

