1
00:00:03,080 --> 00:00:07,340
In this lecture, we're implementing our game sessions so that we can keep track of how many lives the

2
00:00:07,340 --> 00:00:08,090
player has.

3
00:00:08,090 --> 00:00:11,000
But and when we drop into some spikes, drops down to one.

4
00:00:11,000 --> 00:00:16,070
If we go all the way down to zero lives, then it will reset us back to the very first level.

5
00:00:16,070 --> 00:00:18,290
Let's look at what we're trying to achieve here with our goal.

6
00:00:18,290 --> 00:00:21,410
I want to have the player have a certain number of lives, probably three for me.

7
00:00:21,410 --> 00:00:25,940
I want to have the scene restart when the player dies so that you go back to the start of the scene

8
00:00:25,940 --> 00:00:28,100
or start the level and try to do it all over again.

9
00:00:28,100 --> 00:00:33,470
And when all the players lives are lost, I want the game to be over and reset all the way back to the

10
00:00:33,470 --> 00:00:38,990
first screen, or if we've got a main menu or the very first in our build index, most likely.

11
00:00:38,990 --> 00:00:39,770
So that's our goal.

12
00:00:39,770 --> 00:00:40,460
There's a problem.

13
00:00:40,460 --> 00:00:46,010
However, after death, reloading the scene re instantiating the player and all other objects.

14
00:00:46,010 --> 00:00:47,450
So it loads everything again.

15
00:00:47,450 --> 00:00:51,140
And any progress we have with our player or our scene is going to be lost.

16
00:00:51,140 --> 00:00:54,470
So we don't want to have that if the player has a certain score, if we've picked up a certain number

17
00:00:54,470 --> 00:00:58,340
of coins, if the player has a certain number of lives, then we want to retain that.

18
00:00:58,340 --> 00:01:01,100
We don't want to have all that loss when we restart the scene.

19
00:01:01,100 --> 00:01:06,290
So what I'm going to create is a game session object which will be responsible for all these things.

20
00:01:06,290 --> 00:01:07,400
It'll be game session.

21
00:01:07,700 --> 00:01:13,880
CSS is our script and it will be responsible for our lives and our dying, for our score, for level

22
00:01:13,880 --> 00:01:14,570
progress.

23
00:01:14,570 --> 00:01:20,630
So what level we're on, as well as restarting the game when we have run out of lives and anything else

24
00:01:20,630 --> 00:01:23,450
that we want to persist throughout our game session.

25
00:01:23,450 --> 00:01:26,210
So let's scootch back over into Unity.

26
00:01:26,210 --> 00:01:28,370
I'm going to create myself a new game object.

27
00:01:28,370 --> 00:01:33,380
I'll do this within level one and then prefab it and pop it into the other levels as we go.

28
00:01:33,380 --> 00:01:35,660
So right click Create Empty.

29
00:01:35,660 --> 00:01:41,810
I will call this game session, game session and I'll reset that back to the zeros.

30
00:01:41,810 --> 00:01:46,280
Now in my scripts folder, I'm going to right click create a C sharp script.

31
00:01:46,280 --> 00:01:49,760
I will call also game session and clicking on game session.

32
00:01:49,760 --> 00:01:52,190
I will drag game session onto game session.

33
00:01:52,190 --> 00:01:54,770
How many times can we say game session and what we're here for?

34
00:01:54,770 --> 00:01:55,250
A bit of fun.

35
00:01:55,250 --> 00:01:59,930
I'm going to click on my Prefabs folder and drag the game session down into the Prefabs folder.

36
00:01:59,930 --> 00:02:04,070
So we've started that particular process or double click on our game session script.

37
00:02:04,070 --> 00:02:04,850
Open that up.

38
00:02:04,850 --> 00:02:06,860
I'm going to get rid of the bits and pieces.

39
00:02:06,860 --> 00:02:08,930
We don't need a bit of tidy up here.

40
00:02:08,960 --> 00:02:12,860
Now the first I'm going to do, instead of using Start, I'm going to use a wake.

41
00:02:12,860 --> 00:02:18,920
So if you remember our unity order of execution, awake is the very first thing that is going to happen

42
00:02:18,920 --> 00:02:20,900
when we push, play, push our play button.

43
00:02:20,900 --> 00:02:27,650
So within a week, what I want to do is find out how many of the game sessions we currently have.

44
00:02:27,650 --> 00:02:29,870
What I'm doing here, a little bit of a spoiler.

45
00:02:29,900 --> 00:02:32,480
I'm going to implement a singleton pattern.

46
00:02:32,480 --> 00:02:37,940
And the singleton pattern is a way for us to say we only want to have one particular thing in a single

47
00:02:37,940 --> 00:02:38,810
a single thing.

48
00:02:38,810 --> 00:02:42,380
And when we click on play, we'll have this one game session object.

49
00:02:42,380 --> 00:02:48,110
We only want to have one because if we then reset the level or restart the level, we don't want to

50
00:02:48,110 --> 00:02:49,370
have a new one appear.

51
00:02:49,370 --> 00:02:50,780
We only want to have one of these.

52
00:02:50,780 --> 00:02:55,640
So right at the very start, we're going to find out how many game sessions we have.

53
00:02:55,640 --> 00:03:02,900
I'll store this in an integer, which I'll call NUM as a number of game sessions, right equals.

54
00:03:02,900 --> 00:03:06,740
And this will be find objects of type with an s.

55
00:03:06,740 --> 00:03:10,850
I sometimes get this messed up, so I'm going to make a really big deal out of the SFU so you don't

56
00:03:10,850 --> 00:03:11,540
mess up as well.

57
00:03:11,540 --> 00:03:12,980
Objects of type.

58
00:03:12,980 --> 00:03:16,700
This is going to create an array because there's many things.

59
00:03:16,700 --> 00:03:22,190
There's possibly more than one thing find objective type finds the first thing, but objects of type

60
00:03:22,190 --> 00:03:27,230
finds all the things I'm going to be looking for game session and that goes in our angle brackets and

61
00:03:27,230 --> 00:03:28,220
then parentheses.

62
00:03:28,220 --> 00:03:33,530
Dot we want to know how many game sessions we have because this is an array.

63
00:03:33,560 --> 00:03:38,240
Can you have a think of how we would go about finding how many items are in the array?

64
00:03:39,280 --> 00:03:43,960
We use length that will tell us how many things are in the array.

65
00:03:43,990 --> 00:03:51,430
Now we need a little if statement in here if our number of game sessions is greater than one, so we've

66
00:03:51,430 --> 00:03:52,090
got more than one.

67
00:03:52,090 --> 00:03:53,260
What do we want to do?

68
00:03:53,260 --> 00:03:56,020
Well, we destroy whatever appears.

69
00:03:56,020 --> 00:04:01,450
We destroy the game, object, semicolon.

70
00:04:01,450 --> 00:04:08,920
And so a wake is going to be called whenever this class wakes up or comes alive or is brought to life.

71
00:04:08,950 --> 00:04:11,830
The first time that happens is when we push the play button.

72
00:04:11,830 --> 00:04:14,920
This game session with its script on it will blink, come to life.

73
00:04:14,950 --> 00:04:22,690
It also happens when we die and we restart this scene so that we freshly go back to the start of it.

74
00:04:22,690 --> 00:04:26,620
When we start that, it's also going to awaken a new script.

75
00:04:26,620 --> 00:04:32,170
So at that stage or a new class at that stage, if something already exists, if there's already one

76
00:04:32,170 --> 00:04:37,000
in there, so this new one makes the second one, then we destroy this new one.

77
00:04:37,000 --> 00:04:40,390
However otherwise or else is how we write otherwise.

78
00:04:40,390 --> 00:04:43,840
Then don't destroy on load.

79
00:04:43,840 --> 00:04:45,760
And once again it's the game object.

80
00:04:45,760 --> 00:04:51,670
So that's our way of saying each time you load a scene, don't destroy it, have it, stick around,

81
00:04:51,670 --> 00:04:52,810
have it stay there.

82
00:04:52,840 --> 00:04:54,550
There's another couple of things I'm going to do in here.

83
00:04:54,550 --> 00:04:55,810
I'm going to remove update.

84
00:04:55,810 --> 00:04:57,910
I don't think we're going to need that next stop.

85
00:04:57,910 --> 00:05:02,290
I'm going to create a public method that I can call from somewhere else.

86
00:05:02,290 --> 00:05:05,140
Call from the player script, I think is where we'll call it.

87
00:05:05,140 --> 00:05:11,350
So public void process, player death is what I'll call this death.

88
00:05:11,440 --> 00:05:16,360
And within here I'm going to add two methods, one of which I'll do with you now, and the other I'll

89
00:05:16,360 --> 00:05:18,640
set as a challenge for you.

90
00:05:18,670 --> 00:05:25,030
Start by adding an if statement, if the player lives and we need to create a variable for this.

91
00:05:25,030 --> 00:05:27,700
So if player lives is greater than one.

92
00:05:27,700 --> 00:05:29,770
So in other words, you still have enough to keep going.

93
00:05:29,770 --> 00:05:31,450
Let's jump up and create this now.

94
00:05:31,450 --> 00:05:34,030
So I will create a serialized field.

95
00:05:34,030 --> 00:05:35,350
It will be of type int.

96
00:05:35,380 --> 00:05:37,840
We don't want to have a fraction of a life.

97
00:05:37,870 --> 00:05:40,090
We'll call this player lives.

98
00:05:40,300 --> 00:05:41,050
Player lives.

99
00:05:41,050 --> 00:05:45,370
It looks like the player leaves now its player lives will initialize that at three, give the player

100
00:05:45,370 --> 00:05:46,090
three lives.

101
00:05:46,090 --> 00:05:47,320
So back down into here.

102
00:05:47,320 --> 00:05:49,480
If player lives, what have I done wrong?

103
00:05:49,870 --> 00:05:50,590
Play lives.

104
00:05:50,590 --> 00:05:51,630
Play lives.

105
00:05:51,790 --> 00:05:53,500
Well, I've had an extra se down there.

106
00:05:53,500 --> 00:05:55,900
I'm in the habit of the find objects of type.

107
00:05:55,900 --> 00:05:58,570
I'm find everything's of type players of lives.

108
00:05:58,570 --> 00:06:02,650
So okay, just player lives greater than one as per the variable we've just made.

109
00:06:02,650 --> 00:06:04,030
Then we want to do something.

110
00:06:04,030 --> 00:06:05,200
Well, we'll call this for now.

111
00:06:05,230 --> 00:06:06,490
Take life.

112
00:06:06,490 --> 00:06:08,620
So take a life of the player.

113
00:06:09,250 --> 00:06:09,730
It's a method.

114
00:06:09,730 --> 00:06:12,790
We'll create that in a moment, else or otherwise.

115
00:06:12,790 --> 00:06:14,140
We want to do what?

116
00:06:14,140 --> 00:06:15,460
Well, I'll make this as a method as well.

117
00:06:15,460 --> 00:06:17,440
There's only a couple of lines, but it's nice.

118
00:06:17,440 --> 00:06:18,520
It reads more nicely.

119
00:06:18,520 --> 00:06:23,320
If we've got separate methods for these, we'll call this reset game session.

120
00:06:23,320 --> 00:06:28,240
So basically, if you're at the point where you've lost all your lives, we want to reset that.

121
00:06:28,240 --> 00:06:31,840
So I want to say the single turn, just reset, start a new session.

122
00:06:31,840 --> 00:06:33,910
So let's create reset game session together.

123
00:06:33,910 --> 00:06:39,640
Then I'll get you to do take life as a challenge so I can click on game session control period or control

124
00:06:39,640 --> 00:06:40,270
full stop.

125
00:06:40,270 --> 00:06:43,000
Generate this method that'll generate down below.

126
00:06:43,000 --> 00:06:44,560
Just clean it up a little bit void.

127
00:06:44,560 --> 00:06:46,030
And then we don't want to throw an exception.

128
00:06:46,030 --> 00:06:47,590
We want to put some content in here.

129
00:06:47,590 --> 00:06:53,410
So when we reset the game session, we want to first of all, access scene manager and because it's

130
00:06:53,410 --> 00:06:55,390
not showing up, it means we haven't used that.

131
00:06:55,390 --> 00:07:01,090
Once again, I can hit control period and then add the using unity scene management that will plop that

132
00:07:01,090 --> 00:07:01,570
up at the top.

133
00:07:01,570 --> 00:07:01,870
Yes.

134
00:07:01,870 --> 00:07:02,230
Thank you.

135
00:07:02,230 --> 00:07:02,860
Scene management.

136
00:07:02,860 --> 00:07:03,190
Good.

137
00:07:03,190 --> 00:07:06,820
Now we should know scene manager something we want to do.

138
00:07:06,820 --> 00:07:06,940
What?

139
00:07:06,940 --> 00:07:08,530
We want to load the very first scene.

140
00:07:08,530 --> 00:07:14,350
So I'm going to hard code this in load scene zero assuming that the very first thing we come across

141
00:07:14,350 --> 00:07:18,040
is going to be the main menu or the first level for now, it's just the first level.

142
00:07:18,070 --> 00:07:22,270
Later on, we might need to come back and change this, but I think this is okay for the moment.

143
00:07:22,270 --> 00:07:28,270
Just saying go back to the first level or first scene we have in our entire build at this point in time.

144
00:07:28,270 --> 00:07:33,100
And then when we do that, it's really, really important that we destroy this thing, destroy this

145
00:07:33,100 --> 00:07:34,000
game object.

146
00:07:34,000 --> 00:07:40,390
So we're destroying this instance of the game session so that if someone's playing this and they push

147
00:07:40,390 --> 00:07:43,450
play again, we don't have all this stuff hanging around.

148
00:07:43,450 --> 00:07:46,780
We get a totally fresh new instance of the game session.

149
00:07:46,780 --> 00:07:50,980
That can be the first one that when other ones come along and says, Oh, there's already one here,

150
00:07:50,980 --> 00:07:54,130
but at some point we need to reset it back to having none of them.

151
00:07:54,130 --> 00:07:54,880
That's what we're doing here.

152
00:07:54,880 --> 00:07:57,700
When we destroy the game object, that's most of what we need in here.

153
00:07:57,700 --> 00:07:59,250
The last thing I'm going to give to you is a challenge.

154
00:07:59,270 --> 00:08:01,750
Take life, so finish processing the player.

155
00:08:01,750 --> 00:08:06,550
Death reduced the number of lives when the player has some sort of death incident.

156
00:08:06,550 --> 00:08:11,560
And then to load our current scene that we're currently on when that happens, we've done this a couple

157
00:08:11,560 --> 00:08:16,000
of times in this section, so there's some code you might want to go look at and grab, but there's

158
00:08:16,000 --> 00:08:17,080
your challenge, plus video.

159
00:08:17,080 --> 00:08:18,220
See you back in when you're done.

160
00:08:20,590 --> 00:08:21,550
So take life.

161
00:08:21,550 --> 00:08:27,280
I'm going to control period to generate that as a method, clean it up a little bit, get rid of our

162
00:08:27,280 --> 00:08:28,480
exception.

163
00:08:28,570 --> 00:08:31,570
First thing I want to do in here is player lives.

164
00:08:31,570 --> 00:08:37,270
Now I'll write it the long way then the short way equals player lives minus one.

165
00:08:37,390 --> 00:08:42,909
Now the quicker way we can write this, it's exact same thing is to say player lives minus minus.

166
00:08:42,909 --> 00:08:46,030
That will decrease whatever the current value is by one.

167
00:08:46,030 --> 00:08:49,600
Now we need to load where we currently are and we've done this before.

168
00:08:49,600 --> 00:08:53,290
I'm going to use it because our build index is an integer.

169
00:08:53,320 --> 00:09:00,730
Current scene index equals scene manager dot get active scene.

170
00:09:01,090 --> 00:09:02,650
What's the active scene we're looking for?

171
00:09:02,650 --> 00:09:07,330
Well, we need to know from the build index what its number is and then we're going to load that.

172
00:09:07,330 --> 00:09:15,160
So scene manager, dot load scene and we're loading the current scene index semicolon.

173
00:09:15,160 --> 00:09:20,920
Now the last step we need to do because we created a public method process player death that goes through

174
00:09:20,920 --> 00:09:22,900
all of this kind of stuff on the game session.

175
00:09:22,900 --> 00:09:28,360
We need to find a place to call that, so I need to open up my player movement script.

176
00:09:28,360 --> 00:09:31,180
It's a little bit more than player movement, but I'm okay.

177
00:09:31,180 --> 00:09:35,350
We could rename it to player, we could break it out into a number of different scripts.

178
00:09:35,350 --> 00:09:37,720
But I'm okay for now because there's no confusion.

179
00:09:37,720 --> 00:09:42,070
There really is only one thing in here when we move in a way that causes us to die.

180
00:09:42,070 --> 00:09:43,900
Well, we want to be reloading certain things.

181
00:09:43,900 --> 00:09:45,880
Let's have a look at where we've got die in here.

182
00:09:45,880 --> 00:09:47,250
This is a big class now, isn't it?

183
00:09:47,260 --> 00:09:48,370
Die if this happens.

184
00:09:48,370 --> 00:09:50,020
Bump, bump, bump down at the very bottom.

185
00:09:50,020 --> 00:09:53,920
We're going to look for our game session.

186
00:09:53,920 --> 00:09:56,320
So find object of type.

187
00:09:56,320 --> 00:09:57,610
Object of type.

188
00:09:57,610 --> 00:10:01,450
In this instance, we want to find the one that exists for an object of type.

189
00:10:01,450 --> 00:10:05,950
We're looking for game session parentheses, full stop.

190
00:10:05,950 --> 00:10:07,630
And we're going to call which method?

191
00:10:07,630 --> 00:10:09,010
Hopefully the one we just created.

192
00:10:09,010 --> 00:10:14,380
Process player, death, parentheses, semicolon and we'll say we'll see how this works.

193
00:10:14,380 --> 00:10:19,450
Probably need to first of all, go and throw this into all of our other levels.

194
00:10:19,450 --> 00:10:20,830
So I'll just do that quickly.

195
00:10:20,860 --> 00:10:22,060
Jump into levels.

196
00:10:22,060 --> 00:10:24,190
Level two, save level one.

197
00:10:24,190 --> 00:10:25,450
Yes, level two.

198
00:10:25,480 --> 00:10:32,140
We need to grab our prefabs and put in game session into the level.

199
00:10:32,140 --> 00:10:34,510
Save that up over to our levels.

200
00:10:34,510 --> 00:10:39,280
Fine level three prefabs game session save.

201
00:10:39,280 --> 00:10:43,630
So I'm going to go back to level one just because it's nice and simple and we can go through this process

202
00:10:43,630 --> 00:10:44,470
quite quickly.

203
00:10:44,470 --> 00:10:47,650
If you click on play, you'll see it don't destroy on load.

204
00:10:47,680 --> 00:10:51,850
If we expand the don't destroy on load, you can see the game session in here.

205
00:10:51,850 --> 00:10:53,920
Player lives run, run, run, drop.

206
00:10:53,920 --> 00:10:56,470
It's down to two, down to one, down to zero.

207
00:10:56,710 --> 00:10:57,850
And then it should reset.

208
00:10:57,850 --> 00:11:01,480
Once again, we get a new game session and it's back up to three.

209
00:11:01,480 --> 00:11:04,840
I just noticed one bug that I just found just now.

210
00:11:04,870 --> 00:11:07,390
I click on play if I shoot bump.

211
00:11:08,440 --> 00:11:09,760
It takes us to the next scene.

212
00:11:09,790 --> 00:11:12,400
Okay, let's fix that bug really quickly now.

213
00:11:12,580 --> 00:11:14,800
I don't want to take up too much time in this particular lecture.

214
00:11:14,800 --> 00:11:19,930
If we click on the play off, just going to tag the player as player and then jump over into one of

215
00:11:19,930 --> 00:11:20,440
my scripts.

216
00:11:20,440 --> 00:11:20,860
Which one?

217
00:11:20,860 --> 00:11:22,420
This is going to be the level exit.

218
00:11:22,420 --> 00:11:23,160
Open that up.

219
00:11:23,170 --> 00:11:24,040
What do we need to do in here?

220
00:11:24,050 --> 00:11:25,230
Little micro challenge for you.

221
00:11:25,240 --> 00:11:27,190
Pause the video and see if you can have it.

222
00:11:27,190 --> 00:11:29,620
So only the player starts the code routine.

223
00:11:31,660 --> 00:11:38,320
Well, it should be reasonably straightforward if other tag equals equals parentheses player, make

224
00:11:38,320 --> 00:11:39,970
sure we spell it exactly right.

225
00:11:40,150 --> 00:11:44,710
Wrap our curly braces around the stock code routine.

226
00:11:44,710 --> 00:11:45,880
Pop that in there.

227
00:11:46,240 --> 00:11:47,920
Get our formatting figured out.

228
00:11:48,100 --> 00:11:49,150
We'll save that.

229
00:11:49,150 --> 00:11:50,230
Hopefully, that's going to work.

230
00:11:50,230 --> 00:11:52,000
Just do a little bit of a test on this.

231
00:11:52,210 --> 00:11:55,930
I totally didn't realize we had that bug until I saw it accidentally just now.

232
00:11:55,930 --> 00:12:02,380
I was trying to click on one thing and I shot Let's see if I shoot now at the level exit editor I'm

233
00:12:02,380 --> 00:12:02,910
shooting.

234
00:12:02,920 --> 00:12:03,780
Nothing's happening.

235
00:12:03,790 --> 00:12:06,180
If I run down and run through it, I'm the player.

236
00:12:06,190 --> 00:12:06,670
Yes.

237
00:12:06,670 --> 00:12:11,510
And the player is recognize game session is still telling us we've got three player lives.

238
00:12:11,530 --> 00:12:14,830
Let me just run into the spikes to make sure I haven't broken anything with that.

239
00:12:14,830 --> 00:12:15,630
And that's all working.

240
00:12:15,640 --> 00:12:15,850
Cool.

241
00:12:15,850 --> 00:12:16,420
There we go.

242
00:12:16,450 --> 00:12:18,220
We fixed the bug as a bit of a bonus.

243
00:12:18,220 --> 00:12:19,240
That's very exciting.

244
00:12:19,240 --> 00:12:21,010
That's what game development is all about.

245
00:12:21,010 --> 00:12:22,330
Making bugs and fixing bugs.

246
00:12:22,330 --> 00:12:23,200
So great stuff.

247
00:12:23,200 --> 00:12:24,430
I'll see you in the next lecture.

