1
00:00:03,190 --> 00:00:06,430
In this lecture, we are creating some scene persistence.

2
00:00:06,430 --> 00:00:10,780
So when I kill some enemies and grab some coins, when I die, those things are remembered and gone.

3
00:00:10,780 --> 00:00:14,800
But when I die, another couple of times they come back because we've restarted our game.

4
00:00:14,800 --> 00:00:16,000
So let's get started.

5
00:00:16,030 --> 00:00:19,600
Let's start the process in our hierarchy by right clicking Create Empty.

6
00:00:19,600 --> 00:00:25,900
I'm going to call this scene per persist and we'll reset that on the transform right click Reset.

7
00:00:25,900 --> 00:00:28,450
Now I need to create a script in my scripts folder.

8
00:00:28,480 --> 00:00:33,820
Right click Create C-sharp script will also call this scene per assist.

9
00:00:33,970 --> 00:00:39,880
Now, similar to the logic of when we were having our game session and we said, because the game session

10
00:00:39,880 --> 00:00:44,830
has a singleton on it, anything underneath that is also going to have that singleton logic applied.

11
00:00:44,830 --> 00:00:48,760
So in our case, the canvas with the lives and the score is going to persist.

12
00:00:48,790 --> 00:00:50,770
I'm going to do the same thing with scene persist.

13
00:00:50,770 --> 00:00:55,690
However, underneath that I'm going to be placing my coins and my enemies and anything else that I want

14
00:00:55,690 --> 00:01:02,050
to remember that if I collect one coin when the player dies, that coin is still collected until we

15
00:01:02,050 --> 00:01:07,180
restart that level or restart the game and need everything to kind of reset back to where it was.

16
00:01:07,180 --> 00:01:09,790
So click back on our scene, persist game object.

17
00:01:09,790 --> 00:01:12,220
I'll drag my scene persist script over in there.

18
00:01:12,220 --> 00:01:18,100
Now that I've got those two key elements, I can go to prefabs and prefab my scene persist so that as

19
00:01:18,100 --> 00:01:20,080
we change our code everything is cool.

20
00:01:20,080 --> 00:01:26,440
It'll update now prefab and I can click on same persist and drag underneath here my coins although I

21
00:01:26,440 --> 00:01:33,040
do want to group them first so right click create empty and we'll call this pick ups and then underneath

22
00:01:33,040 --> 00:01:36,190
pick ups we'll grab our coins, drop them underneath pick ups.

23
00:01:36,190 --> 00:01:36,700
That's great.

24
00:01:36,700 --> 00:01:39,220
And then pick ups we'll put underneath scene persist.

25
00:01:39,220 --> 00:01:44,350
We don't want to go and update our or override our prefab because we don't want these things.

26
00:01:44,350 --> 00:01:47,020
These this is content that's specific for our level.

27
00:01:47,020 --> 00:01:52,360
While we're here for good measure, I'm going to right click create empty and create some enemies once

28
00:01:52,360 --> 00:01:58,990
again reset the transform on that and we'll drag in a goober or two underneath my enemies.

29
00:01:58,990 --> 00:01:59,800
There we go.

30
00:01:59,800 --> 00:02:00,760
So click on that.

31
00:02:00,760 --> 00:02:02,320
Where are you hiding, Mr. Goober?

32
00:02:02,320 --> 00:02:04,180
You're way over there, Prop.

33
00:02:04,600 --> 00:02:09,880
Drop you into the scene and we'll grab the other goober drop you into the scene as well, just so that

34
00:02:09,880 --> 00:02:13,180
we can be testing these things out and we know that it's actually working.

35
00:02:13,180 --> 00:02:18,460
So we've got these things enemies and we want to drag enemies underneath scene persist.

36
00:02:18,460 --> 00:02:22,480
So those things should all now persist as we implement our logic in the script.

37
00:02:22,480 --> 00:02:23,800
So let's start that process.

38
00:02:23,800 --> 00:02:28,840
We'll jump over into our scripts, fire up, scene, persist and we go nice and fresh.

39
00:02:28,840 --> 00:02:34,630
I'm going to well, I'm here top left click on Explorer open up my game session double click on that

40
00:02:34,630 --> 00:02:35,740
closed scene Explorer again.

41
00:02:35,740 --> 00:02:36,880
So we've got plenty of space.

42
00:02:36,880 --> 00:02:42,100
I'm going to highlight my awake code block in here that's got our singleton on it and copy that so we

43
00:02:42,100 --> 00:02:43,320
can reuse our code a little bit.

44
00:02:43,320 --> 00:02:47,320
And you don't have to sit there watching me type everything and I highlight my start and update and

45
00:02:47,320 --> 00:02:47,890
remove that.

46
00:02:47,890 --> 00:02:50,140
We might put it back if we need it, but don't for now.

47
00:02:50,140 --> 00:02:53,590
Paste in the week we'll go through and just change some of the details in here.

48
00:02:53,590 --> 00:02:58,480
So some game sessions we will double click F2 that will allow us to rename.

49
00:02:58,480 --> 00:03:02,800
So instead of num game sessions can be number of seen persists.

50
00:03:03,220 --> 00:03:04,510
That's what this is called.

51
00:03:04,510 --> 00:03:09,880
Same persists instead of finding of type game session we're finding of type seen persist.

52
00:03:09,880 --> 00:03:13,630
Okay, so that's a singleton basically saying if you've got one already, you don't need another one.

53
00:03:13,630 --> 00:03:18,490
That's going to be great when the player dies and then respawns and we restart the level, there'll

54
00:03:18,520 --> 00:03:20,470
be a seen persist there already.

55
00:03:20,470 --> 00:03:25,050
However, it's not going to be so good if the player exits the level.

56
00:03:25,090 --> 00:03:26,890
So what happens if you exit the level?

57
00:03:26,890 --> 00:03:32,590
We've already got a scene persist that has all its information about this is what I believe should be

58
00:03:32,590 --> 00:03:37,960
contained within my scene persist and it's going to destroy any scene persist that we create for level

59
00:03:37,960 --> 00:03:39,520
two and level three and level four and so on.

60
00:03:39,520 --> 00:03:43,090
So what we need to do is have some logic in here, very simple.

61
00:03:43,090 --> 00:03:46,240
I'm going to keep this whole script, this whole class, very simple.

62
00:03:46,240 --> 00:03:49,120
So it's easy to understand we need one more thing in here.

63
00:03:49,120 --> 00:03:52,300
But before we do that one thing, let's go and create ourselves a little bit of content.

64
00:03:52,300 --> 00:03:57,070
So I'm going to save up level one, jump over into level two and we'll pop in here.

65
00:03:57,070 --> 00:03:58,720
Our prefab scene persist.

66
00:03:58,720 --> 00:03:59,260
Where are you seen?

67
00:03:59,260 --> 00:04:00,160
Persist There you go.

68
00:04:00,160 --> 00:04:05,800
Prefab grab our enemies and drag them underneath scene persist grab our pick ups and drag them underneath

69
00:04:05,800 --> 00:04:06,700
scene persist as well.

70
00:04:06,700 --> 00:04:12,130
So these should now be seen persisting if we've done this correctly and I save that and just go to level

71
00:04:12,130 --> 00:04:15,190
three while I'm here so we can do proper play testing on this.

72
00:04:15,220 --> 00:04:19,870
All my goobers just mushed around wherever he is, I'm going to right click create empty and tidy this

73
00:04:19,870 --> 00:04:24,520
up by creating enemies right click to reset our enemies game objects.

74
00:04:24,520 --> 00:04:27,730
I will grab all of the goobers except not the player.

75
00:04:27,730 --> 00:04:28,600
You're not a goober.

76
00:04:28,600 --> 00:04:30,760
You stay out of here, drag them under enemies.

77
00:04:30,760 --> 00:04:31,330
That's good.

78
00:04:31,330 --> 00:04:33,820
And then enemies we can put underneath game session.

79
00:04:33,820 --> 00:04:35,110
Do we have any coins in here?

80
00:04:35,110 --> 00:04:39,070
I just quickly chuck a couple of coins in here and our players should probably go back to where the

81
00:04:39,070 --> 00:04:40,960
player needs to start off, which was where?

82
00:04:40,960 --> 00:04:42,040
Up the top here.

83
00:04:42,040 --> 00:04:42,550
Down the bottom.

84
00:04:42,550 --> 00:04:45,040
Down the bottom here before the bouncy bounce just there.

85
00:04:45,040 --> 00:04:49,660
And we'll put in a couple of coins at the start so that we can make sure this all works properly.

86
00:04:49,660 --> 00:04:53,380
Prefabs, coin, drag a coin just in front of the player there.

87
00:04:53,380 --> 00:04:57,400
Do a big jumping to the coin and one more so we can test that all this works properly.

88
00:04:58,030 --> 00:04:58,870
So at two coins.

89
00:04:58,870 --> 00:05:02,410
So once again, right click create empty call this pick ups.

90
00:05:03,060 --> 00:05:08,040
Reset the transform supporting to keep all these things organized, mostly because your hierarchy doesn't

91
00:05:08,040 --> 00:05:12,540
then get filled up with a whole bunch of gunk, but also so we can do these exact same things like grabbing

92
00:05:12,540 --> 00:05:14,560
the pickups and easily putting them underneath.

93
00:05:14,580 --> 00:05:16,950
Our scene persists, which I haven't put in there yet.

94
00:05:16,950 --> 00:05:19,230
Oh, sorry, I wouldn't put enemies under game session.

95
00:05:19,230 --> 00:05:19,980
What are you doing, Rick?

96
00:05:19,980 --> 00:05:20,730
Something crazy.

97
00:05:20,730 --> 00:05:22,440
So scene persist prefab.

98
00:05:22,440 --> 00:05:23,550
We drag that into the scene.

99
00:05:23,550 --> 00:05:24,870
Sorry, you probably picked that up.

100
00:05:24,870 --> 00:05:25,470
I missed it.

101
00:05:25,470 --> 00:05:28,290
I'm going to grab my enemies and put them where they belong on the scene.

102
00:05:28,290 --> 00:05:30,090
Persist in my pickups under scene.

103
00:05:30,090 --> 00:05:30,780
Persist as well.

104
00:05:30,810 --> 00:05:33,210
Okay sanity check to make sure Rick isn't insane.

105
00:05:33,240 --> 00:05:34,470
Game session canvas.

106
00:05:34,470 --> 00:05:35,190
Yes, that's right.

107
00:05:35,190 --> 00:05:35,850
Same persist.

108
00:05:35,850 --> 00:05:36,570
Enemies and pickups.

109
00:05:36,570 --> 00:05:37,620
Yes, that looks right.

110
00:05:37,620 --> 00:05:38,400
Okay, good one.

111
00:05:38,400 --> 00:05:39,990
Now I'll go all the way back to level one.

112
00:05:39,990 --> 00:05:43,590
So click on levels level one so we can test this from the get go.

113
00:05:43,620 --> 00:05:46,260
I'm just going to move this coin a little bit higher so it's not the way the enemy.

114
00:05:46,260 --> 00:05:51,090
So now that we've got all that structure set up, what I want to do is say when the player reaches the

115
00:05:51,090 --> 00:05:58,350
end of a level or loses all of their lives, I want to have my scene persist, be destroyed, so that

116
00:05:58,350 --> 00:06:03,570
when you're on a new level, that new level scene persist is going to be the boss.

117
00:06:03,570 --> 00:06:05,280
It's going to be controlling things.

118
00:06:05,280 --> 00:06:08,460
So I'm going to give this to you is a little bit more of an abstract challenge.

119
00:06:08,460 --> 00:06:10,050
The code is pretty simple.

120
00:06:10,050 --> 00:06:15,570
We've done this before and we'll be continuing to practice our basic concepts to reinforce them.

121
00:06:15,570 --> 00:06:20,550
And in this case, we're going to create a public method that we can call wherever we need to in order

122
00:06:20,550 --> 00:06:25,680
to reset our scene, persist and then make sure you play test so that you can be sure there are no edge

123
00:06:25,680 --> 00:06:26,880
cases, no weird things.

124
00:06:26,880 --> 00:06:31,080
Like if you pick up three coins when you've got one life and then you die and you go back to that level,

125
00:06:31,080 --> 00:06:33,090
then you go into this level without picking up any coins.

126
00:06:33,090 --> 00:06:36,000
So do a little bit of quality assurance testing on it to make sure you've got it.

127
00:06:36,000 --> 00:06:36,480
So there you go.

128
00:06:36,480 --> 00:06:37,890
There's a challenge plus video.

129
00:06:37,890 --> 00:06:39,000
See you back here when you're done.

130
00:06:41,610 --> 00:06:42,780
Insane persists.

131
00:06:42,780 --> 00:06:43,770
Yes, I mean, sane persists.

132
00:06:43,770 --> 00:06:47,490
What I need to do, first of all, is create public void.

133
00:06:47,640 --> 00:06:53,760
And this I'll call reset scene persist just to be really clear what it is parentheses.

134
00:06:53,880 --> 00:07:00,630
Kelly braces and here's really simply just destroy me destroy game object not magazine Rick but me is

135
00:07:00,630 --> 00:07:03,480
in this particular game object and then save all of that.

136
00:07:03,480 --> 00:07:05,220
So this is really simple code.

137
00:07:05,220 --> 00:07:10,200
There's a lot more nifty ways that you can create this as you become more capable and confident as a

138
00:07:10,200 --> 00:07:10,830
programmer.

139
00:07:10,830 --> 00:07:16,230
But I want to keep practicing these core concepts of if you have something as public within here, we

140
00:07:16,230 --> 00:07:19,710
can call it from somewhere else, and it affects this particular class.

141
00:07:19,710 --> 00:07:22,830
And so where do we need to be calling this public method?

142
00:07:22,830 --> 00:07:28,950
Well, if I go into my game session, this is one place where let's see, let's see.

143
00:07:28,950 --> 00:07:29,880
Not take life.

144
00:07:29,880 --> 00:07:32,730
We don't need to call it there, but in reset game session.

145
00:07:32,730 --> 00:07:37,620
So in other words, when the entire game ends, we absolutely want to be resetting our scene persist.

146
00:07:37,620 --> 00:07:43,230
So just before we load the new scene, I'm going to find object of type.

147
00:07:43,230 --> 00:07:48,960
It will be seen persist parentheses and then we're going to be calling what we'll be calling.

148
00:07:48,960 --> 00:07:50,270
The reset scene persists.

149
00:07:50,280 --> 00:07:52,530
We just created parentheses semicolon.

150
00:07:52,530 --> 00:07:54,630
I'm going to copy that because I'll need it one more time.

151
00:07:54,630 --> 00:07:56,760
Save this script into my explorer.

152
00:07:56,760 --> 00:07:59,250
The other spot I think I need to do this is level exit.

153
00:07:59,250 --> 00:08:00,420
When we leave a level.

154
00:08:00,420 --> 00:08:04,320
So open that up and I'm going to look for where we would do this.

155
00:08:04,320 --> 00:08:08,340
So on trigger, if it's the player, start the routine to load the next level.

156
00:08:08,340 --> 00:08:09,720
We want to wait our little bit of time.

157
00:08:09,720 --> 00:08:14,400
We're waiting in here, but prefer if to do it just before we actually load the new thing.

158
00:08:14,400 --> 00:08:16,830
Let's destroy our scene.

159
00:08:16,830 --> 00:08:17,340
Persist.

160
00:08:17,340 --> 00:08:20,580
So I'll paste that in there under load next level.

161
00:08:20,580 --> 00:08:25,410
After we've done all of our scrutiny and yielding and all that good stuff, we want to find object to

162
00:08:25,410 --> 00:08:26,820
type, scene persist and reset.

163
00:08:26,850 --> 00:08:27,600
Same persist.

164
00:08:27,600 --> 00:08:31,140
So save that up and I'm pretty sure they're the only spots for now.

165
00:08:31,140 --> 00:08:36,809
You might have something in the future where the player can, for example, push a button to exit,

166
00:08:36,809 --> 00:08:41,190
to to reload or a main menu or some other element.

167
00:08:41,190 --> 00:08:42,510
And you need to be thinking of this.

168
00:08:42,510 --> 00:08:47,910
And this is one of the downsides of using this kind of public method where we know in one of our scripts

169
00:08:47,910 --> 00:08:52,020
in here scene persist, we've got this public thing and then go and put it in all of the other places.

170
00:08:52,020 --> 00:08:53,130
Put it here, put it there.

171
00:08:53,130 --> 00:08:56,070
It's really great for us at this beginner plus stage.

172
00:08:56,070 --> 00:09:00,450
It's a nice, simple way of doing things, but there are more detailed ways that you'll learn to do

173
00:09:00,450 --> 00:09:01,740
these things in the future.

174
00:09:01,740 --> 00:09:03,240
That's a little bit more scalable.

175
00:09:03,240 --> 00:09:06,660
But for now, this is a perfectly fine way of doing things for where we're at.

176
00:09:06,660 --> 00:09:09,450
So let's jump in before we congratulate ourselves too much.

177
00:09:09,450 --> 00:09:10,650
Let's make sure it works.

178
00:09:10,650 --> 00:09:15,750
I should have my coins persisting in here when I jump on them, and even when I shoot a goober, they

179
00:09:15,750 --> 00:09:18,750
will go shop one goober, grab one coin, fell down into the hole.

180
00:09:18,750 --> 00:09:19,170
Great.

181
00:09:19,170 --> 00:09:22,170
So those things no longer appear exactly what I wanted.

182
00:09:22,170 --> 00:09:23,250
Now, let's see.

183
00:09:23,250 --> 00:09:24,000
We've got one coin.

184
00:09:24,000 --> 00:09:26,190
One goober will go through the level exit.

185
00:09:26,190 --> 00:09:27,240
That coin is there.

186
00:09:27,240 --> 00:09:27,420
Good.

187
00:09:27,420 --> 00:09:32,250
Grab a few of these coins and then let's just see what happens when we die at this stage.

188
00:09:32,250 --> 00:09:32,850
Great.

189
00:09:32,850 --> 00:09:34,110
Those coins are no longer there.

190
00:09:34,110 --> 00:09:35,160
Let's see what happens when we die.

191
00:09:35,160 --> 00:09:36,630
Now, back to the first one, okay?

192
00:09:36,630 --> 00:09:37,890
And these coins have reappeared.

193
00:09:37,890 --> 00:09:38,970
The group is reappeared.

194
00:09:38,970 --> 00:09:44,460
That means we should have this successfully set up where we have our seen persistence, but only when

195
00:09:44,460 --> 00:09:45,000
we need it.

196
00:09:45,000 --> 00:09:48,810
And just a reminder of why we needed to have that final public method in there.

197
00:09:48,810 --> 00:09:53,190
It's because if this scene persists, is told, you need to stay around forever.

198
00:09:53,210 --> 00:09:58,290
We go into next level and we get to the next level and it says, okay, there's already a scene persist

199
00:09:58,290 --> 00:10:01,740
from the previous level, so I won't bring in my scene persistence than these enemies.

200
00:10:01,740 --> 00:10:04,620
And these pickups might actually not appear in the scene.

201
00:10:04,620 --> 00:10:06,690
So that's why we needed to do that public method.

202
00:10:06,690 --> 00:10:11,640
But I'm feeling pretty good now about our flow and about our features and our functionality, so I'll

203
00:10:11,640 --> 00:10:12,540
see you in the next lecture.

