﻿1
00:00:04,290 --> 00:00:05,650
‫Hey, welcome back.

2
00:00:05,670 --> 00:00:09,900
‫In this video, we're concerned with winning and losing conditions.

3
00:00:10,320 --> 00:00:14,520
‫This is one of the last things we're going to handle in this game project.

4
00:00:14,550 --> 00:00:16,860
‫Now let's take a look at our section plan.

5
00:00:16,860 --> 00:00:23,430
‫Now, we've already checked off the first five items on our section plan, and the last two are the

6
00:00:23,430 --> 00:00:26,580
‫special effects and winning and losing conditions.

7
00:00:26,580 --> 00:00:32,250
‫And I'd like to switch these last two around and handle winning and losing conditions just because this

8
00:00:32,250 --> 00:00:39,300
‫is the last of our gameplay programming logic and the special effects are cosmetic and more like finishing

9
00:00:39,300 --> 00:00:42,420
‫touches so we can handle those at the very end.

10
00:00:42,420 --> 00:00:48,300
‫So in this video we're going to create a game over function so that we know when the game has been either

11
00:00:48,300 --> 00:00:49,950
‫won or lost.

12
00:00:49,950 --> 00:00:55,410
‫And in order to win the game, we're going to need to count the number of towers in the world, because

13
00:00:55,410 --> 00:00:59,730
‫once we've destroyed all of the towers, then we know we have won the game.

14
00:00:59,730 --> 00:01:05,670
‫And if the tank itself gets destroyed, then we can consider that to be the lose condition.

15
00:01:05,670 --> 00:01:11,760
‫So I'm back here in the game mode and the first thing I'd like to do is create another blueprint implementable

16
00:01:11,760 --> 00:01:12,540
‫event.

17
00:01:12,570 --> 00:01:19,980
‫We have this start game function, which we can call from C++, but we're implementing its logic in

18
00:01:19,980 --> 00:01:20,520
‫the game mode.

19
00:01:20,520 --> 00:01:27,120
‫Blueprint Thanks to Blueprint Implementable Event, I'd like to create another one of these for losing

20
00:01:27,120 --> 00:01:27,720
‫the game.

21
00:01:27,720 --> 00:01:31,530
‫So I'm going to create a void function called Game Over.

22
00:01:31,530 --> 00:01:37,530
‫And for this one, I'd like to take a boolean input parameter called B one game.

23
00:01:37,530 --> 00:01:43,020
‫And of course this is going to be a blueprint implementable event because the only thing I'd like to

24
00:01:43,020 --> 00:01:50,130
‫handle with this function is displaying that we've lost or won the game and that we can do from blueprints.

25
00:01:50,130 --> 00:01:52,410
‫So we have a game over function.

26
00:01:52,410 --> 00:01:55,350
‫Now we need to know when we should call this function.

27
00:01:55,350 --> 00:02:00,450
‫Well, first of all, we know we can call this function when we've lost the game, and that's going

28
00:02:00,450 --> 00:02:02,550
‫to be if the tank has died.

29
00:02:02,550 --> 00:02:10,050
‫So let's go into our actor died function here in tune tanks game mode CP And we see that if dead actor

30
00:02:10,050 --> 00:02:15,840
‫is the tank, then we're handling the destruction of the tank and we're setting the enabled state to

31
00:02:15,840 --> 00:02:16,680
‫false.

32
00:02:16,710 --> 00:02:25,020
‫Another thing we can do here is end the game by simply calling game over and this boolean be one game

33
00:02:25,020 --> 00:02:27,240
‫we can pass in false for this.

34
00:02:27,240 --> 00:02:31,770
‫So that takes care of losing the game, at least from the C++ side.

35
00:02:31,800 --> 00:02:34,770
‫Now we need to know when we should win the game.

36
00:02:34,800 --> 00:02:36,540
‫Now here an actor died.

37
00:02:36,540 --> 00:02:43,050
‫If the dead actor can be cast to a tower, we know that we've just destroyed a tower.

38
00:02:43,050 --> 00:02:49,740
‫Now, this would be the perfect place to update a tower count or a variable of integer type that keeps

39
00:02:49,740 --> 00:02:53,220
‫track of how many towers in the world there currently are.

40
00:02:53,250 --> 00:02:53,580
‫Now.

41
00:02:53,580 --> 00:02:59,310
‫In order to do that, we need a variable to keep track of the number of towers so we can add a private

42
00:02:59,310 --> 00:03:02,250
‫variable here in the H file.

43
00:03:02,250 --> 00:03:03,960
‫And I'd like to do that down here.

44
00:03:03,960 --> 00:03:08,880
‫So I'm going to create an INT 32 and I'm going to call this target towers.

45
00:03:09,120 --> 00:03:15,840
‫Now we can initialize this right off the bat to a value of zero, but we need to update this as the

46
00:03:15,840 --> 00:03:17,760
‫number of towers decreases.

47
00:03:17,760 --> 00:03:22,350
‫And we also need to set this to the number of towers there are in the world.

48
00:03:22,470 --> 00:03:28,170
‫Now we could hard code this to four, but what happens when we start adding more towers to our levels?

49
00:03:28,170 --> 00:03:30,690
‫Well, then it gets a little bit more complicated.

50
00:03:30,690 --> 00:03:35,580
‫So what I'd like to do instead is see how many towers in the world there are.

51
00:03:35,610 --> 00:03:43,050
‫Now, I could create a function for this that returns 32 and this function can find out how many towers

52
00:03:43,050 --> 00:03:45,810
‫there are in the world and return the number.

53
00:03:45,810 --> 00:03:51,450
‫So I'm going to call this function, get a target tower count.

54
00:03:51,600 --> 00:03:55,650
‫So let's go ahead and give this function a body in the CP file.

55
00:03:55,680 --> 00:04:02,130
‫I'm going to go all the way down to the bottom and create the function body and I can't forget to put

56
00:04:02,130 --> 00:04:05,040
‫the class name before the function name.

57
00:04:05,040 --> 00:04:12,270
‫Now this is going to return an end 32 and we need to decide how we're going to find out how many towers

58
00:04:12,270 --> 00:04:13,170
‫there are.

59
00:04:13,200 --> 00:04:19,800
‫Now there's a function that exists in gameplay statics called Get All Actors of class.

60
00:04:19,800 --> 00:04:21,300
‫Here's the documentation.

61
00:04:21,300 --> 00:04:25,770
‫It says Find all actors in the world of the specified class.

62
00:04:25,770 --> 00:04:28,890
‫So we need to specify a particular class.

63
00:04:28,920 --> 00:04:31,530
‫Now this function has three inputs.

64
00:04:31,530 --> 00:04:33,720
‫First is a world context object.

65
00:04:33,720 --> 00:04:34,770
‫That one's easy.

66
00:04:34,770 --> 00:04:36,180
‫We know how to handle that.

67
00:04:36,180 --> 00:04:41,610
‫But next we have a PT subclass of a actor and this is called Actor Class.

68
00:04:41,610 --> 00:04:48,000
‫Now we've seen that functions that require a PT sub class of can accept a u class as inputs and the

69
00:04:48,000 --> 00:04:49,740
‫third parameter is a T array.

70
00:04:49,770 --> 00:04:56,940
‫It requires a t array of actor pointers and this is passed in as a reference and it's not a const reference

71
00:04:56,940 --> 00:05:03,680
‫which seems to suggest that this function just might change this input parameter called out act.

72
00:05:03,730 --> 00:05:08,530
‫Here's another hint that this is the case, is that it's called out actors.

73
00:05:08,530 --> 00:05:15,820
‫It's an out parameter, meaning we pass it in and then after the function has been called, it has values

74
00:05:15,820 --> 00:05:18,220
‫that have been put into it by the function.

75
00:05:18,220 --> 00:05:21,340
‫So we can think of it as a sort of output for the function.

76
00:05:21,340 --> 00:05:28,810
‫Now it says down here for parameters, actor class is the class of actor to find must be specified or

77
00:05:28,810 --> 00:05:30,640
‫result array will be empty.

78
00:05:30,640 --> 00:05:37,240
‫We want to find all of the towers and out actors is an output array of actors of the specified class.

79
00:05:37,240 --> 00:05:42,670
‫So once we've called this function and passed in an array, when the function has finished, that array

80
00:05:42,670 --> 00:05:48,160
‫will contain pointers to all of the actors in the world of the specified class.

81
00:05:48,160 --> 00:05:51,880
‫So we know that this function requires a world context object.

82
00:05:51,880 --> 00:05:56,500
‫Now, if we're going to be calling this from within the game mode, we can pass in this next.

83
00:05:56,500 --> 00:06:03,490
‫We need the PT sub class of specifying the A actor and we know that this can be a new class pointer.

84
00:06:03,490 --> 00:06:06,070
‫Now we need to specify the class to find.

85
00:06:06,070 --> 00:06:12,010
‫If we specify a actor, then we'll get a big array filled with pointers to each actor in the world.

86
00:06:12,010 --> 00:06:13,810
‫But we don't need all of the actors.

87
00:06:13,810 --> 00:06:21,070
‫We just need the towers so we can use a tower static class which will return us a new class that represents

88
00:06:21,070 --> 00:06:23,290
‫the A Tower C++ class.

89
00:06:23,290 --> 00:06:26,770
‫Now the third parameter is a T array of a actor pointers.

90
00:06:26,770 --> 00:06:33,760
‫So if we create a local variable of type T array specifying the a actor pointer in the angle brackets,

91
00:06:33,760 --> 00:06:35,980
‫then we can call you game play statics.

92
00:06:36,010 --> 00:06:41,620
‫Get all actors of class passing in those three inputs, and the result of this action will be that our

93
00:06:41,620 --> 00:06:45,970
‫towers array should contain pointers to all towers in our world.

94
00:06:45,970 --> 00:06:52,840
‫Now the tray is a nice container class which can contain a number of elements as long as they all share

95
00:06:52,840 --> 00:06:53,830
‫the same type.

96
00:06:53,830 --> 00:07:01,690
‫And this class has quite a great deal of functions that we can use when storing items in a tray.

97
00:07:01,720 --> 00:07:08,710
‫Now I'm going to scroll all the way down and show you this function here called Num Short four number

98
00:07:08,830 --> 00:07:12,310
‫and it says Returns the number of elements in array.

99
00:07:12,310 --> 00:07:19,690
‫So we can use this function to get the number of elements in our array once we have all of the towers

100
00:07:19,690 --> 00:07:20,380
‫in the world.

101
00:07:20,380 --> 00:07:26,080
‫So if we call Tower's num, then we can use this as the return value for our function.

102
00:07:26,080 --> 00:07:31,930
‫And as we saw, we set our return value for this function which we call get target tower count as an

103
00:07:31,930 --> 00:07:35,980
‫n 32, we can return the number of elements in our tower's array.

104
00:07:35,980 --> 00:07:41,230
‫So I'd like to challenge you to implement this get target tower count function.

105
00:07:41,230 --> 00:07:44,710
‫So create a local t array of type A actor.

106
00:07:44,710 --> 00:07:47,110
‫You can call it towers or anything you like.

107
00:07:47,110 --> 00:07:52,930
‫Now use the you game, play statics function, get all actors of class and make sure you pass in the

108
00:07:52,930 --> 00:07:54,220
‫correct input parameters.

109
00:07:54,220 --> 00:08:00,310
‫Now, after you call this function, your tray is going to contain pointers to all of the towers in

110
00:08:00,310 --> 00:08:06,850
‫our world, so you can simply return the number of towers using that num function that belongs to the

111
00:08:06,850 --> 00:08:07,930
‫tray class.

112
00:08:07,930 --> 00:08:10,660
‫So go ahead and pause the video and implement.

113
00:08:10,660 --> 00:08:12,640
‫Get target tower count now.

114
00:08:15,670 --> 00:08:16,000
‫Okay.

115
00:08:16,000 --> 00:08:17,980
‫So here's get target tower count.

116
00:08:18,010 --> 00:08:24,250
‫Now, the first thing I'm going to do is create a local t array specifying a actor pointer as the type

117
00:08:24,250 --> 00:08:26,590
‫of element that this array should store.

118
00:08:26,590 --> 00:08:31,720
‫And I'm going to call this towers now I need to call you gameplay statics, so I need to make sure that

119
00:08:31,720 --> 00:08:33,100
‫that header is included.

120
00:08:33,130 --> 00:08:38,830
‫Now I see that it is since we're using it in some of our other functions, but it's good to make that

121
00:08:38,830 --> 00:08:39,340
‫check.

122
00:08:39,370 --> 00:08:47,380
‫Next I can use you gameplay statics and I'm going to call get all actors of class.

123
00:08:47,410 --> 00:08:54,130
‫Now the first input is a world context object I'm going to pass in this and next is the sub class of

124
00:08:54,130 --> 00:08:55,990
‫representing the actor class.

125
00:08:55,990 --> 00:09:03,430
‫And I mentioned that we can use a tower static class which will return a U class that represents the

126
00:09:03,430 --> 00:09:04,270
‫tower class.

127
00:09:04,300 --> 00:09:09,190
‫Now again, it's a good idea to make sure that we're including towers, which we are.

128
00:09:09,220 --> 00:09:14,740
‫Now, the third and final parameter is going to be the tray and we can pass in towers.

129
00:09:14,740 --> 00:09:21,340
‫So once this function has been called, towers will now contain pointers to all of the towers in the

130
00:09:21,340 --> 00:09:21,880
‫world.

131
00:09:21,880 --> 00:09:28,000
‫In fact, if there exist any objects of a class that derives from towers, we would also get those.

132
00:09:28,000 --> 00:09:33,780
‫So since we have a towers array, we can simply return the number of elements in that array.

133
00:09:33,790 --> 00:09:40,660
‫So I'm going to use a return statement and use towers dot num and now we have a get target tower count.

134
00:09:40,660 --> 00:09:41,740
‫So this is great.

135
00:09:41,740 --> 00:09:48,220
‫We can use this to initialize our tower count, which we called target towers and we can do that when

136
00:09:48,220 --> 00:09:49,090
‫we start the game.

137
00:09:49,090 --> 00:09:51,780
‫So I'm going to place that here and handle game start.

138
00:09:51,790 --> 00:09:54,400
‫In fact, I'd like it to be the first thing we do.

139
00:09:54,400 --> 00:10:02,530
‫So I'm going to take target towers and set it equal to the return value from get target tower count.

140
00:10:02,560 --> 00:10:07,240
‫Now target towers should be initialized to the number of towers in our world.

141
00:10:07,240 --> 00:10:13,120
‫So this is great, but we need to update this count every time a tower dies, which means we need to

142
00:10:13,120 --> 00:10:16,000
‫go into our actor died function.

143
00:10:16,000 --> 00:10:22,660
‫And here, if we've cast dead actor to a destroyed tower, then after we handle destruction, we should

144
00:10:22,660 --> 00:10:30,430
‫update our target towers variable so we can say target towers and we can use the decrement operator.

145
00:10:30,430 --> 00:10:35,560
‫We can use either the post decrement operator or the predicament operator.

146
00:10:35,560 --> 00:10:41,620
‫Both of these will result in the same action which is taking target towers and subtracting one from

147
00:10:41,620 --> 00:10:41,800
‫it.

148
00:10:41,800 --> 00:10:48,220
‫Now, as soon as we've subtracted one from target towers, now we need to check to see if Target Towers

149
00:10:48,220 --> 00:10:49,660
‫has reached zero.

150
00:10:49,660 --> 00:10:55,150
‫So I'm going to place an if check here to see if Target towers has reached zero.

151
00:10:55,780 --> 00:11:02,380
‫Now, as soon as it does, we know that the game should be over so we can call the game over function

152
00:11:02,710 --> 00:11:10,660
‫passing in true 4b1 game since we've destroyed all of the towers and we can consider this a win.

153
00:11:10,660 --> 00:11:13,180
‫Now let's go ahead and compile our code.

154
00:11:13,180 --> 00:11:18,670
‫Now, back here in the editor, what I'd like to do before we wrap this video up is go into our game

155
00:11:18,670 --> 00:11:19,420
‫mode blueprint.

156
00:11:19,420 --> 00:11:21,040
‫So here's beep tune.

157
00:11:21,040 --> 00:11:24,400
‫Thanks game mode and we have this event start game.

158
00:11:24,400 --> 00:11:29,650
‫And what we're doing is creating our start game widget, adding it to the viewport so that we can see

159
00:11:29,650 --> 00:11:31,780
‫that start game countdown timer.

160
00:11:31,780 --> 00:11:39,190
‫So we know that start game is being called, but we also need to implement our game over blueprint implementable

161
00:11:39,190 --> 00:11:39,820
‫event.

162
00:11:39,820 --> 00:11:47,260
‫So I'm going to right click and type in game over and here's our event, game over and check this out.

163
00:11:47,290 --> 00:11:50,860
‫Notice we have this one game and it's a boolean.

164
00:11:50,860 --> 00:11:57,220
‫So when we call this function from C++, we're passing in a boolean value for B one game.

165
00:11:57,220 --> 00:12:04,270
‫The blueprint event node strips out that lowercase B and places a space between one and game as sort

166
00:12:04,270 --> 00:12:05,440
‫of a courtesy for us.

167
00:12:05,440 --> 00:12:10,150
‫Now we're going to create some widgets so that we can see when we've won or lost the game, but we'll

168
00:12:10,150 --> 00:12:11,650
‫do that in a future video.

169
00:12:11,650 --> 00:12:17,770
‫For now, I just want to confirm that this is being called and called correctly so I can do that with

170
00:12:17,770 --> 00:12:22,180
‫a print string node and we can print the value based on this boolean.

171
00:12:22,180 --> 00:12:29,080
‫If I click and drag this boolean straight into the in string input, we'll see that there's a convert

172
00:12:29,080 --> 00:12:30,280
‫boolean to string.

173
00:12:30,280 --> 00:12:37,420
‫This is another convenient conversion node that will convert a boolean into a string for us, and its

174
00:12:37,420 --> 00:12:43,690
‫functionality is such that it will convert to the word true or false based on the truth value of the

175
00:12:43,690 --> 00:12:44,350
‫boolean.

176
00:12:44,710 --> 00:12:46,120
‫So why don't we test this out?

177
00:12:46,120 --> 00:12:48,040
‫I'm going to compile and hit play.

178
00:12:49,400 --> 00:12:52,570
‫All right, now I'm ready to try to win the game.

179
00:12:52,580 --> 00:12:55,340
‫Hopefully I don't lose before I can do so.

180
00:12:55,430 --> 00:13:01,430
‫And I've lost the game and you can see false printed up at the top left of the screen.

181
00:13:01,430 --> 00:13:08,570
‫So we now know that losing the game results in event game over being called with false being passed

182
00:13:08,570 --> 00:13:12,050
‫in, we need to make sure that true is passed in when we win the game.

183
00:13:12,050 --> 00:13:14,900
‫So let's try to kill all of the turrets.

184
00:13:15,380 --> 00:13:18,020
‫So I need to really try not to get a hit.

185
00:13:18,020 --> 00:13:19,250
‫Oh, I got hit already.

186
00:13:19,910 --> 00:13:21,560
‫This is pretty challenging.

187
00:13:22,840 --> 00:13:24,820
‫And there we go.

188
00:13:24,820 --> 00:13:28,720
‫And true is printed at the top left of the screen.

189
00:13:28,750 --> 00:13:31,600
‫It only shows up for a few seconds, so you might have missed it.

190
00:13:31,630 --> 00:13:32,350
‫Okay, great.

191
00:13:32,350 --> 00:13:35,050
‫So now we have winning and losing conditions.

192
00:13:35,080 --> 00:13:41,380
‫The next step is to actually show this in the form of a widget on the screen so that we're not relying

193
00:13:41,380 --> 00:13:42,850
‫on the print string node.

194
00:13:42,880 --> 00:13:48,010
‫So in summary, we now have winning and losing conditions, and there's just a few things we need to

195
00:13:48,010 --> 00:13:53,890
‫do to polish this, such as creating widgets that inform the player that they've won or lost the game.

196
00:13:53,890 --> 00:13:58,900
‫We also counted the number of towers using a tray and the gameplay statics function.

197
00:13:58,930 --> 00:14:00,600
‫Get all actors of class.

198
00:14:00,610 --> 00:14:01,270
‫Great job.

199
00:14:01,270 --> 00:14:03,370
‫This game is really coming along.

200
00:14:03,370 --> 00:14:09,220
‫In the next video we'll create those widgets and then we're ready to start adding special effects and

201
00:14:09,220 --> 00:14:10,840
‫polishing up our game.

202
00:14:10,870 --> 00:14:12,520
‫I'll see you in the next video.

