Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:04,150 --> 00:00:09,760
In this video we're going to be refactoring our code will be importing the previous number Wizards script
2
00:00:09,820 --> 00:00:14,290
that we created in the number was a console section and doing a little bit of analysis of what we need
3
00:00:14,290 --> 00:00:21,640
to change and then doing our first refactor of this code so it's ready to be used with our user interface.
4
00:00:21,670 --> 00:00:24,800
So let's jump in and do some refactoring.
5
00:00:25,100 --> 00:00:28,540
Okeydokey first thing we need to do is grab the previous code.
6
00:00:28,540 --> 00:00:33,040
Our number is a code that we worked on from the no wizard console section of the Course.
7
00:00:33,040 --> 00:00:38,470
If you don't have access to that then you can download the file that I've attached to this lecture number
8
00:00:38,470 --> 00:00:41,680
was a dot C S drag that into your.
9
00:00:42,510 --> 00:00:50,560
Scripts folder X or want a next thing to do is to create a game object so we can attach our script to
10
00:00:50,620 --> 00:00:57,300
that game object right click in the hierarchy create empty we'll rename this to game play.
11
00:00:57,700 --> 00:01:00,370
And as good practice we reset the transform.
12
00:01:00,490 --> 00:01:03,130
Now we can drag our number was a script.
13
00:01:03,730 --> 00:01:09,460
As a component on to our game play game object that means that when we run the game the script the script
14
00:01:09,490 --> 00:01:10,970
will be run.
15
00:01:11,850 --> 00:01:17,550
Let's open up number wizard and have a look and see refamiliarize ourselves with the old familiar Coale
16
00:01:17,580 --> 00:01:19,740
code from a couple of sections ago.
17
00:01:20,160 --> 00:01:23,310
Okay we've got some variables we're declaring up at the top here.
18
00:01:23,310 --> 00:01:28,200
We created a stack game method where we're giving the player a whole bunch of here's how you play the
19
00:01:28,200 --> 00:01:34,740
game information and then in our update method we've got our player input where we're using our if and
20
00:01:34,800 --> 00:01:40,980
else if statements to see whether the player has pushed up or down or return and get key down was the
21
00:01:41,520 --> 00:01:44,440
mechanism we're using in there for the players input.
22
00:01:44,550 --> 00:01:49,590
Let's have a little look at what we need to do refactor what I'd like to do is serialise our variables
23
00:01:49,590 --> 00:01:54,670
at the top so that we can change them in the inspector and not have them hard coded into our code here.
24
00:01:54,810 --> 00:01:59,370
Need a removal of our debug information and then change the the player input.
25
00:01:59,370 --> 00:02:05,640
So instead of using our get keyed down our keyboard input we need to create public methods similar to
26
00:02:05,640 --> 00:02:11,400
that which we did in Scene lodo where we had public void load next scene so that we can create public
27
00:02:11,400 --> 00:02:17,760
methods in here that we call from our button presses so those are the three main chunks serializing
28
00:02:18,000 --> 00:02:22,550
removing the debug and changing the player input that we need to do.
29
00:02:22,560 --> 00:02:28,140
And when I say we of course I mean you know what I'd like for you to do as a challenge is to start refactoring
30
00:02:28,140 --> 00:02:34,500
your code serialise field for the minimax variables to remove the debug dialogue text that we don't
31
00:02:34,500 --> 00:02:35,380
require.
32
00:02:35,430 --> 00:02:41,820
And instead of IF statements in Update create two public methods and we'll call them on press higher
33
00:02:42,060 --> 00:02:43,290
and on press lower.
34
00:02:43,290 --> 00:02:44,160
So those are the steps.
35
00:02:44,170 --> 00:02:48,960
The Challenge You should have everything you need to jump in and give that your best shot and when you're
36
00:02:48,960 --> 00:02:52,970
ready I'll see you back here and we'll go through the solution together.
37
00:02:55,220 --> 00:03:03,170
Radio first step is to serialize our variables so serialise field for Max and.
38
00:03:04,180 --> 00:03:06,590
Serialise field for me.
39
00:03:06,610 --> 00:03:09,610
I'll save that just check that that has worked.
40
00:03:09,640 --> 00:03:10,510
Jim back over in here.
41
00:03:10,510 --> 00:03:12,320
Click on game play.
42
00:03:12,850 --> 00:03:14,550
After it compiles click on game.
43
00:03:14,590 --> 00:03:15,130
That we go.
44
00:03:15,250 --> 00:03:21,830
And you can see there is a max an image in here that we could set to say one thousand and one.
45
00:03:22,960 --> 00:03:27,120
Next steps I'll just delete my debug text book.
46
00:03:27,280 --> 00:03:28,030
Get rid of all that.
47
00:03:28,030 --> 00:03:31,100
Anywhere we see debug just gone.
48
00:03:31,480 --> 00:03:34,190
And also within the next guest's method.
49
00:03:34,200 --> 00:03:36,920
So all of that has now been gone.
50
00:03:36,940 --> 00:03:37,810
Excellent.
51
00:03:37,810 --> 00:03:43,000
Next it was the next part of the challenge was to create the public methods on press higher and on press
52
00:03:43,000 --> 00:03:43,620
lower.
53
00:03:43,720 --> 00:03:46,540
So we'll do that right above where we currently have.
54
00:03:46,560 --> 00:03:53,030
Update we'll be delaying all of our update the moment but for now we will start by creating public void
55
00:03:53,380 --> 00:03:54,590
on Press.
56
00:03:54,620 --> 00:03:55,460
Hi.
57
00:03:57,630 --> 00:04:01,940
Parentheses and then our curly braces.
58
00:04:02,070 --> 00:04:07,200
We need to do exactly the same logic that we're doing when we push up arrow so I will grab the mean
59
00:04:07,230 --> 00:04:09,960
equals guess and next guess method.
60
00:04:09,960 --> 00:04:15,960
Copy that pasted inside my own press higher method here.
61
00:04:15,960 --> 00:04:16,420
Great.
62
00:04:16,430 --> 00:04:24,340
Second we need to go public void on press low so be our other method again.
63
00:04:24,350 --> 00:04:25,650
Kelly bracers.
64
00:04:26,540 --> 00:04:28,690
Where is their logic it Max equals guess.
65
00:04:28,760 --> 00:04:29,350
Next guest.
66
00:04:29,350 --> 00:04:30,590
Copy that.
67
00:04:30,590 --> 00:04:32,340
Paste it within here.
68
00:04:32,400 --> 00:04:33,700
That looks pretty good.
69
00:04:33,710 --> 00:04:36,170
We don't need to have anything for our.
70
00:04:36,170 --> 00:04:40,470
Did you get it correct because we already have connected to our success button here.
71
00:04:40,610 --> 00:04:43,430
It calls the load next scenes that's taken care of.
72
00:04:43,580 --> 00:04:49,460
Which means we can delete all of our update method and everything that is contained within it.
73
00:04:49,610 --> 00:04:56,570
So now we just have all variables being declared at the top level of logic we need to tidy up in start
74
00:04:56,570 --> 00:04:59,780
game and our next guess ops.
75
00:04:59,810 --> 00:05:02,940
I accidentally deleted an cares to do that don't delete.
76
00:05:02,940 --> 00:05:03,860
Next guess again.
77
00:05:03,920 --> 00:05:06,250
So just delete what is an update.
78
00:05:06,260 --> 00:05:06,760
There we go.
79
00:05:06,800 --> 00:05:09,950
Thank you Red squiggly line for letting me know I had done something foolish.
80
00:05:10,150 --> 00:05:15,160
OK so on press higher on press lower and next guess we still have they will save that.
81
00:05:15,260 --> 00:05:17,520
Now let's have a look at what's going on in start game.
82
00:05:17,540 --> 00:05:20,420
Just here we've got our Max equals Max plus one.
83
00:05:20,420 --> 00:05:27,360
That was our mechanism for making sure that we absolutely can get to a guess of 1000 because of rounding.
84
00:05:27,380 --> 00:05:30,320
We say Macs Plus mean divided by two.
85
00:05:30,370 --> 00:05:34,610
Then we didn't end up getting to a thousand or was 999 as high as we could get to.
86
00:05:34,630 --> 00:05:36,290
I'm going to move that up to the top.
87
00:05:36,290 --> 00:05:42,290
They are Macs equals Macs plus 1 so we can establish them straight away and then we don't need to be
88
00:05:42,290 --> 00:05:45,370
re assigning max and min.
89
00:05:45,410 --> 00:05:48,980
We also don't need to have Guess haeckel's 500 that's hard coded in there.
90
00:05:48,980 --> 00:05:54,470
Got a whole ton of lines in here for some reason now what we do need to do next is have some sort of
91
00:05:54,470 --> 00:05:57,100
guess so what is our guest going to be.
92
00:05:57,260 --> 00:06:01,800
Well guess as we have done here is Max plus min divided by two.
93
00:06:01,820 --> 00:06:09,380
Copy that line within our next guess and put it in start game so that as we did before were establishing
94
00:06:09,380 --> 00:06:11,520
Let's have a guess right at the start.
95
00:06:11,630 --> 00:06:17,890
But we don't hardcoded it because up here we might say that the max E is out ten thousand or twenty
96
00:06:17,890 --> 00:06:24,830
three whatever we want to sit now inspector now is vector over here whatever we are assigning as the
97
00:06:24,830 --> 00:06:25,850
max and the main.
98
00:06:25,850 --> 00:06:31,390
So then we need to have our calculation based on the variable Max and the variable min right here in
99
00:06:31,380 --> 00:06:36,230
our start game method I get so on start with Call start game.
100
00:06:36,230 --> 00:06:37,790
Excellent with tidy that up.
101
00:06:37,970 --> 00:06:41,190
We have on press higher on press lower and next guess.
102
00:06:41,210 --> 00:06:44,510
Excellent so we've tidied up all our code in here is looking pretty good.
103
00:06:44,540 --> 00:06:51,530
The one last step that we have to do is to display onto the screen what our guess actually is and that's
104
00:06:51,530 --> 00:06:56,750
going to be the topic for the next lecture where we're looking at the two string function and hooking
105
00:06:56,750 --> 00:07:01,770
up all of our text field so great work with your Reflektor great work in this video.
106
00:07:01,940 --> 00:07:05,930
We're getting very close to finishing up our functionality I think in the very next lecture will have
107
00:07:05,930 --> 00:07:10,890
it so that we can play our game the whole way through so great work and see you again very shortly.
10774
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.