Transcripts
1. Course Intro: Are you interested in creating games but don't know
where to start. Do you want to learn how to use the fastest-growing game engine? Got O4, that is also gaining in popularity more and more
as the years go on. And program games with the powerful and
versatile language C-Sharp in this
course is for you. The c-sharp programming
course here, or got O4 is designed
for anyone who wants to learn how to program
using C-sharp and God, oh, for whether you're a
complete beginner or a TD script user looking to transition into
using C-sharp, this course will
provide you with a solid foundation
to get started creating simple games
and applications. This course, you will learn the fundamentals of programming
including datatypes, variables, loops,
conditional statements, functions, classes, and
more than apply all of this knowledge at the end to
create a game of your own. So if you're ready to
learn how to create games using C-sharp and got a four, enroll in the course today and start to journey in development.
2. Download Godot: All right, welcome everyone. In this video we're
going to focus on picking up the engine, but not only that,
but the correct version of the engine we're going to need for this course. Now, normally I would
say that you can pick up the sentence from the
website and go to it. Steam. You scoop. Or I think there's maybe one
or two other places that you can pick it up as well. But considering
we're going to need the version that
supports C-Sharp, I think he can only
get to hear from the website maybe on it. I've never looked at HP, So I don't know if the
C-sharp version is there. What you're gonna do with
this, come over here to their website, Godot engine.org. And you'll see right here on the front page we have
downloaded LTS for 3.5 and download
latest 44.0, 0.1. Now I'm going to need to
get this version as well. So we'll go ahead and click
on that with you guys. Alright, so I'm just
going to go ahead and click on download latest and will be
brought to this page. Now you can see here there are two versions
on the screen. They both look kind of similar, but one from does say dotnet, and if you look under
it, the bottom one here says C Sharp support. So that's the version we're
gonna go ahead and get. So make sure you click
on the bottom one here. Going click that. And if you'd like to
contribute to the engine, you can go ahead and
make a donation there, but it is not necessary if you don't want to or you
can't right now, whatever you all you
have to do is go ahead and click the
X button there. And you'll see in
your downloads, yeah, you'll now have a
zip file that quick. All you have to do
now is go ahead and open that up and unzip that. And you'll have the
executable in there for you. If for some reason this open up on a different platform, e.g. I'm on Windows here,
but if it opened up on Mac or something instead, then should be able to
go ahead and just change that repository for yourself or should automatically
detect you there. Yeah, there it goes. There on the Epic Game
Store now, as well. As you see there, it
says the digital servers do not include C-sharp support. So yeah, the only
way we're gonna get this C-sharp spores by coming directly here to the website. Make sense. And as I was saying earlier, is if some reason came up
with a different platform, here's all your
platforms down here. So because St. macOS, and now we can have the Mac
version if for some reason you're on a Mac and it came up with windows, That's
all you have to do. Just scroll down a bit
and select platform. I see you guys in the next one where we'll get you set
up with an IDE and I'll show you the one I'm gonna be using through
this course and why I would recommend
using an external one.
3. External IDE: Support and I'll show you why
at the end of this video. Now in here, I'm
going to go ahead and I'll be using Visual Studio. If you just go ahead
and Google that, it should be on the first
result here from Microsoft. Just scroll down,
you can go ahead and just download Visual Studio. Now you're using
the free version, You're going to be
using Community here. You can get that
for Windows or Mac. If you're on Linux,
you're going to have to get visual studio code. Now, once the download, you can go ahead and
run the installer. Now, I have already
installed this in the. If I go ahead and
read my installer, I'm going to get
a page like this. I'm just going to go ahead
and modify on one of these. What you're going to
want to do when you install it is make sure that you have the C
sharp sections here selected. Just to make sure that
we have everything, we don't run into any
potential issues. You do not need the option here. Just make sure that you have the desktop development
and you can get the universal Windows Platform development if you would like. I have both selected
just to, again, help prevent any
issues I'm occurring. Just make sure I grab
everything that's sharp there. We're not going to need the
multiplatform PI development because our UI and everything will be built inside of gotto. Once you have that, you can
go ahead and just hit Net. Take the individual components, everything should
already be selected for you. You hit next. Again, we'll go into
the language packs. I use English primarily
on this computer. I'm only going to
install English. But if you want to install
multiple languages, feel free to then of select the location on your drives and hit the install button
that you have here. Just wait for it
to do its thing. Once that is all set up, we can go ahead and we're
going to jump into the engine. Now, quick, if I open this up, let me capture this real quick. For creating your project here. We're just going to
go ahead new project. And you're not able to see
the screen at the moment. But the other thing that's up on here, create new project. It's just asking for the name of the project and a file location. I'm going to name it Course, just for the purpose of this. And hit the Create
Bolder button. The Renderer shouldn't
make any difference. You'll have three options. I'm just going to have
Forward plus selected here, just because that's the default, should not matter when we
go throughout this course. And I'll just hit Create Edit. This way I can just go ahead
and show you real quick. Go ahead and make sure
this is selected. Now, with it open, as
I was saying earlier, you can use the built
in script editor here. When it comes to
doing our C sharp. However, it's not the best
support on the built in one. If you try to use
or search for help, look at the
documentation, you're not going to get any
of that help here. Which unfortunately
what we're going to do is we're going to head
on up to our editor. Not the project editor though. We want to go to
Editor Settings. If we go ahead and
just search in here, external, we go ahead
and look down here. We've got text editor and
we have external here. You can go ahead and
turn that on and select a path directly
to your executable. However, we are using
the Sharp version. So we do have a model section here we want the editor section
that's inside of there. We have external editor, it's set to one
awesome neteditor. Go down to that section and you see you'll have a drive down here of a couple of
the supportive ones such as visual studio
and visual studio code. We have Jet Brains Rider, which is another popular one. And mono develop, which I
have not used personally. But if you're using
visual studio, just as I go ahead
and select that. And then when you
open up your code, it should automatically
launch that program. And the benefits we're going to have in there are
going to be things such as auto completion and
almost like a built in documentation in a sense. Now this is just a
simple project here. Don't worry too much
about the code. I just wanted to show
you that we come down, we get to a point code. We can come in right.
And you can see we can get this auto
complete going on things that we're not going to have in inside
of the built in editor. If you do go with
the built in editor, be aware you're not going to have any of that access there. That's why I would
highly recommend you to use an external one here. You can at least get some of this auto complete
that we're looking at. That way you can
help along the way. That way if you
don't necessarily remember the name of something, you can figure it out, look through, get an idea, and maybe it'll jog
your memory that way. You're not just hitting, blindly copying
something without necessarily understanding
what's going on. But I just wanted to make
sure you guys notice that and can see why I'm recommending you to
use an external one. All right? This one's already rambling on a little
longer than necessary, but I just want to
make that clear. Whichever IDE you want to use, if you want to use
one, is up to you. For this course, I'll
be using visual studio. The benefits that you have
in there are things like the auto complete compared to the built in script editor
that comes with the engine. With this, you can
actually plug ins to help help make your work flow a little easier and customize your development environment
to suit you better. Personalize it a little
more for yourself. All right, I'll see you guys
in the next one and we'll get into some code. Start getting in
with the basics.
4. Printing To Console: Hi, So starting with
our project here, if you haven't created one, then when you wash the execute
all for the first time, go ahead and hit New Project. Go ahead and give it a name. And you'll be greeted
with this scene here. And to get where
we are right now, all you have to do
is just click on the user interface button here with the green circle
on the left hand side. And that'll get you to
the same situation, same scene that sit around here and go ahead
and hit control S, or you can go to
scene and Save Scene As that way we're
already set up to go. Now to run your scene in, simply come up and hit the play button in
the top right here. And if you do that, you'll
have to select a main scene. You can select the current. And when you're
running a project, you can always select this later inside of your project settings. Personally, I select
my main scene later and usually just
hit Run currency in here. At the top. F6. Whichever one you use there
will be completely up to you. But that's when I nine times out of ten, I'm
gonna be clicking on. So let's get started on
our first script here. Hey, we're gonna take
a look at learning how to print things out
to our console. And we will technically have our first program
written in code. It'll be simple and only
printing out to the console. But it will be a program
that you've coded. So I'm just going to click on my control that we created
back on the left-hand side. And you can either right-click
and go to attach script. Or once you've clicked on it, you can just hit this little
script button here with the green plus to
attach the script. And you'll be presented
with this menu here. Make sure you have C-sharp
selected as your language. It's going to inherit from a control because
that's the type. That is. Make sure we have template
checked with no default. From the path. You can come in here and you can rename this to whatever
you would like. Minds can be read as
colon slash slash and I'm just going to name
it, print dot CS. Cs just means that it
is a C-sharp script. And I'll hit Create.
Now we've created this and if you're using an
external IDE like suggested, that we can get the benefits
of things like autocomplete. Then your ID should open
up with your script. If you chose to use
the internal one without auto-complete
and any other benefits, then it should open up
here in the script tab. So if you're using
an external IDE like we went over and installed in the previous, previous video. This what we should
be looking at now. Of course, if you're
using the internal ones, we're looking at same thing
just within your own editor. So for this video, we're not going to need
this process function. So we can cover that in the
future as to what that is. And here is the
piece that we have. This using stuff at the top. Don't really need
to worry about it. We're going to have
system and Gato by default at the
top of every script. And that's basically
just telling our code what we can really use or what
you have access to. E.g. there are some features
that you may want to use that may be locked behind. If you have used other
languages in the past, something like
maybe C plus plus. These are like namespaces. Some kind of think of it as basically all this means
is that we have access to other functions and things that we are
normally able to use. E.g. if I type bio in here, you'll see that
we have one here. Sorry, it's not popping up. I'll be sure to
include an image here. If we type file, we will see that over
on the right-hand side. We can see it says
System dot io. And if we were not using system, we would not have
access to that. Alright. So hopefully that clears
up some of that confusion, but we do not need
to worry about using god or using system
that'll be up at the top. Have all of our scripts
we want to focus on, is this ready block, or rather this
underscore ready block. Now, in C-Sharp, every block of code is inside
a pair of curly braces. And what we have
highlighted here are the curly braces associated
to this ready block. And inside of this ready block, anything between
these curly braces is going to execute as soon
as our program launches. So what we're gonna do in order to print out to our console. Would be JD dot capital
P. Already up print. And this takes in horror
as followed up with two parentheses
with an argument. In this case, the default that gets put in there
as the word this. And it ends with a curly, sorry, not a curly
brace, semicolon. Now and C-sharp, every line of code is going to end
with a semicolon. If you're coming from GD script, you don't need semi-colons, but you could use them
if you wanted to. Now, if we're going
to print this, print something
out to our screen, we're going to do, we're going
to delete the word this. We're going to have a pair
of boats, quotation marks. And we're going to type something in there
such as Hello World. Now have capital G, capital D, dot capital P, RIN, open parenthesis,
double-quotes. Hello world. Another
double quote. Close our parentheses, and we end our line with a semicolon. Now if you're using Visual
Studio like I am here, you see yellow on the side. And that's this
indicates that we have unsaved changes when we
hit Control S and save. That's just going to turn green and show us that our
changes have been saved. We go now back in
the Godot Engine. If we go ahead and run it, this is going to
wait a moment and our project is going to build. And don't worry about
that error that I have that just because
I've had scripts in here that I've deleted and looking for object
you're seeing, you'll get a window
here with a gray block. Don't worry about it. That's fine. Because we're not rendering anything out
to our screen right now. But if we look down inside
of our output console, down here at the bottom,
we can see Hello World printed out
just like we want it. Now in the Godot Engine, we actually have different
ways that we can print e.g. if we put a comma there in-between if
our two arguments here and we tried
to print them out, we're going to print them and we have two arguments like this, different items we
want to print out. When we just use print, it's gonna be smashed
together as if it was one word with no
spaces in between. Now if we wanted to print out multiple things like this
and we wanted to have a space between each item. All we do is we add a capital
S at the end of print. So we have print with a
capital P and a capital S. And that S means
we're going to put a space in-between each item. If we do print with two T's, but we haven't capital P
and a capital T at the end. Then that capital
T means that we're going to put a tab
in-between each item. So depending on what
you're going for, would depend on what kind
of print you want to use and how you want to
visually structure that. One that admittedly, I
don't use all too often, but can be useful in
certain situations. Is print, capital E, RR. This means print error. As we're gonna save
that and run that. You'll see something
very different here. It's going to come
up with red text and it's going to have
those red dots beside it. Now way it's something
that's really eye-catching that you can easily see and not mistake
for one of your other prints. If you're doing a fair amount of printing to your console. Alright, so those are the
different ways that we can print out to our console. And congratulations, you have made your
first little program. It's simple and basic, but you have written code
and you are executing it. You've made your program. And the next one would
take, start taking a look at variables. And then we'll jump
into doing some math.
5. Variables: Let's talk about variables. A variable is something
that can be utilized as a designated identifier to preserve a specific
piece of information, such as the previous Hello World that we output in
the last video. So to incorporate this, a new line would be added
before our ready block. So let's go ahead and
add a new script. I'm going to call my variables. Here we are again, we won't need this block, its process block it off. And anywhere above
the ready block. But before still has to be within this pair
of curly braces here. What I'm gonna do is anything inside of a pair
of quote is called a string in programming. So I'm just going to type out the word string with a capital S. And for my variable I
want it to be called hello. Now, you could write it down lowercase just like this,
that's perfectly fine. However, in C-Sharp, typically, the first character, it's
going to be a capital. And to set that
equal to a piece of data like our helloworld
would just say equals. And then we put in our quotes
and typing hello world. Have you tried to compile
this and run this, you're going to
come into an error. And the reason for that, if you remember what
I said last time, in C-Sharp, every line
must end with a semicolon. So we have to make sure to
include that at the end. Now if we were to come into our ready block and we
were to print this out. Instead of typing
in Hello World, we can just type in our
variable's name, right? Because remember this is storing that piece of information. So we're going to
use this identifier. Like this. We're just going to print out hello, save that. And if we were to
run that, we should get the same result as we got in the last video down inside of our output where we
just see the word HelloWorld get printed out. Now, what's a benefit to this is this value can actually be modified while the
program is running. So you can imagine this
as, as an example, if we're playing the game and
the amount of damage you do to an enemy might change based
off of the enemies armor, the weapon you're using, your character stats, and so on. And because of
that, that damaged number is going to
constantly fluctuate. Then you may have Crick's
to take into consideration and maybe where
the enemy was hit. So we've got all these
different different variations to take into consideration
that can alter an effect, what that damage a
variable would be. And that's just going
to go into doing a bunch of different
math and calculations. But we don't wanna do that. We don't want to
touch math right now, but we can still
change the value of our identifier inside
of our code here. We can go ahead
and make a change. So after we print that out, we can go ahead and we
can say hello equals. And we could type
something else in there. So we can say, see,
shirt is awesome. Remember to end this
with a semicolon. And if we go ahead and
print this out again, We'll see you have HelloWorld first because that's what it
was initially assigned to. And then we change the value
of it to C sharp is Austin. And then when we print again, you should get C-sharp is
awesome and not helloworld. They'll go ahead and save that. Go ahead and run it. You take a look
inside of our output. If we go, we can see we've got both values depending on
where you're printing, whether it's before or
after our value change. Now, when it comes to
naming your variables, there are a few rules that
you should keep in mind. Now a variables
can be named any, or you can name them
using any combination of letters, numbers,
and underscores. Our, However, they
must not contain spaces and their names shouldn't start with
a number either. You see if we come up here
and we type in one hello, we're instantly going to
get that error at the top. You guys were having
we've got an issue here, so we're going to run
into an error so you cannot start your
variable with a number. Now to avoid any errors,
it's also important, important to avoid
using any keywords or any of these function
names that we may run into throughout the course that are already present
within GD script. Now you might think, well, well, TD script, but we're
learning C-sharp here. Why do we have to know about things that are already
named within GD script? Well, the reason
for that is because some of those same
values are going to carry over here to C-sharp. So e.g. looks like there. If I come in here and
I use the word name, this is technically fine. However, I'm going to run
into a bit of an issue. And the issue here is, you can't see the pop-up here, but I just have my
mouse hovering over it. And it is telling me why I have this little green
outline under the word name. And as it says here, variables, dot name, hides
inherited member, no doubt mean, use the new keyword if
hiding was unintended. So we would have to put in all this extra work just to use another use something that already has a name
that exist for us. So instead of putting in
that extra work bore, no no real reason. I would say. And to
avoid confusion, I would say it's just
better to learn what they are and avoid them. So if you get a little
green squiggly, at least inside of
Visual Studio here. That's gonna be y because
that already exist. Alright, now for clarity, my variable names
should be concise and accurately reflect
the data they contain. So for examples and saying, which is coming here, we could say name underscore length. They'll typically again at the end and the L would be
capitalised and C-sharp. And you typically wouldn't
use a underscore at all. But there's nothing
preventing you from doing it. But this would be a much better variable name
to tell you what, exactly what it is. Rather than having
something say, length of my name. Right? They're both the same thing,
but one of them is a lot more concise and they're getting the same information across. Some things to keep in mind. Now, when it comes to variables
and potential errors, if I were to use a
lowercase h down here, we're going to run
into an issue. Is if I save that and
maybe don't see it, you ignore it, whatever, and you try to run
a pilot program. We're going to run
into a failed build. And this is what
you're going to see. You're going to
see the name hello with an underscore
age does not exist. In the end. We can't really see
the rest of it. And the does not exist
in the current context. And we didn't see right here in the path exactly
where it is ten, our variable, C-sharp script, line ten at character 12. Now we can then go back into that and we can
take a look at it, see what the issue is and
the issue is because it's a lowercase h. So if
you have two words, maybe once in capitals
and once in lowercase, once uppercase ones, lowercase. They're not gonna be equal to the same thing when
it comes to code. If you're going to have
capitalize as your variable name, you need to make sure
you capitalize it wherever you use it, wherever
you're going to call it. This is a fairly common issue that I've seen quite a
few times when it comes to new developers is just for getting small
things like that. They just oversee. And what we just saw there in the Godot Engine is where
our errors will appear. So if you ever building your program trying to run a
scene and the build fails, those are the errors that
you're going to see. And as you saw,
it told us script the error was in
what line it was on, what character the issue was at. And then it told us the
issue which was hello with a lowercase h does not exist. It's something that
we don't have access to. Something made up. So we just saw our
first ever and how to kind of take a look at it to find out where the
issue actually is. Alright, so that's gonna
do it for this one. And we'll go now. We're going to continue
with some variables go and bleeding into
the next video.
6. Variables Practice: For the Skillshare
exercise here, I want you to take a moment
to remove, of course, the old script off
of the control there and create
a new script for the first practice exercise. What I want you to
do is I want you to print things out to
the screen and then alter the contents of that variable and output
the new value.
7. Strings: Now we can use strings and variables like
we saw with HelloWorld. But what we can also
do is we can use this to interject them into
some of our senses. And as string str, things that we can call on them. Now, we mentioned
that the end of last video things
like uppercase, lowercase, capitalizing
our variable names. But we could do
things like that to the actual text itself
have manipulated. So I'm gonna go ahead and create a new variable that is a string. When I call it my name and
set it equal to my name. But all lowercases,
and of course, ended with a semicolon. Now if I come in here, I could print it out. And it would be just
as you would expect, save that, run it. Project builds take second
and we can see it down there. The name printed
out perfectly fine. And we've done this
multiple times now at this point, however, this is a string because it is inside a pair of the
quotation marks. So that means we can
call things on it. So after we put in my name, we can do a period. And if you are using something like Visual Studio would
have the autocomplete. You'll notice all of
these pop up here. And the ones that
we can look for in this case is we can look
at happened allies. Now, we're getting a little
green squiggly here. And that's just because
we need to end this with a pair of parentheses. In this case. Now, if we were
to save this and run it, Pharmacy, Kevin weren't
able to see that. But we have my name here. If we just do the
dot capitalize. You see there? And then There's the green squiggly
line I mentioned there. Will each do an open and
close parentheses at the end to call that
method on our string. And if we were to run that, you'll see in the output that
our name gets capitalize, which means the first character
of each of those words. First character of
our first and last, are both going to get started
with a capital letter. That's not the only way that
we can manipulate this. Manipulate our string here. We can also call
methods such as two upper and then I will again have end with a pair of parenthesis. And we can do the same thing. I'm just going to copy, paste that down the line. And we could do to
lower. If we run that. You can see where any at both these printout one's
going to be in all uppercase and what's
gonna be in all lowercase. So we can manipulate a string in multiple
different ways here. There are plenty more things that we can call on a string. Some of those things
we'll get to later. Others you'll find out through either
reading documentation or just experimenting
and taking a look at some of the things
in that drop-down. If you have that
autocomplete feature. But to use a variable
inside of our string, we go ahead and write
out our screen here. Say hello, friend is, we can't just type out our
variable name because it will be taken literal,
like you see here. Instead, if we want to use a variable inside of our string, what we have to do is
go to the beginning. Just before our quotation
marks, put $1 sign. And then we want to
use our variable. Actually surround
our variable name with a pair of curly braces. And you'll see it'll
turn white in this case. And that means we're going
to plug our variable in to that place. If we
were to run that. Now, now see the name get
plugged into that string, and we'll see that sentence
fully written out. Now in this case,
since it is a name, you're probably going to want to use something like
capitalize on it. And you just do that inside of, right inside of
our curly braces. And so if you want
to call anything you do what's inside of that. So just like you can see that if you were
to actually run this, now, we're gonna get
that same result just with the
personal last name, started with a capital letter. That's not the only thing that we can do with strings here. We can actually manipulate
them further by using things like tabbing
out spaces, right? Who wanted to, we
could actually do a forward slash or backslash. I always get the two confused. And a t, like you see here, I believe it's backslash x. We're going from the bottom or from the bottom-up or
leaning back, right. Hopefully I got that right. And if we go ahead
and print that out, you'll see down in the
output, we got this. We got a much bigger space here compared to the other location. Now what else we
can do is we can actually break this down
onto a new line as well. So instead of t, If
we use an n, right, the backslash n, save
that and give that a run. And we just wait for this
to you. There we go. And now whatever was
printed next was broken down onto the next line. So we do have a few different manipulations like that with our slash and slash t.
Really break that up. And that'll, that'll wrap
up our little strings and introduced or introduction to our first variable type here. Next, we're going to jump
into taking a look at some numbers and doing
some mathematics.
8. Strings Practice: Alright, are you scared, Skillshare people out there? We're gonna go ahead and you can go ahead and
make a new script. And this will be your next
practice exercise here. What I want you to do
is I want you to design your own little program
here and employ multiple variables to serve as substitutions for placeholders
into your strings. And as a bonus, try to use some
modifiers like to upper to lower capitalize. And maybe you'll find
an interesting one that we haven't covered yet.
9. Mathematical Operations: Alright, so I've gone ahead,
I've got my new script. I've called this one math, seeing as that is
what we're gonna be getting into for the topic of today's or rather this video. And again, we're just going
to need the ready block here. We're not going to eat
that process blocks. Even go ahead and get rid of it. As programmers. Number serve a
multitude of purposes, from keeping track of
scores and game mechanics to data visualization and information storage
for applications, numbers will either
be represented as an integer or a float. Let's first take a
look at integers. Now, integers are whole numbers similar to those utilized in elementary education
and offer for the same basic mathematical
operations of addition, subtraction, multiplication, and division here in C-Sharp. So these numbers are gonna be whole numbers like
12345 and so on, as well as their
negative counterparts. So negative one, negative two, negative three, and so on. So if we go ahead, we can
actually perform some of this, some of these mathematical
expressions, these operations. And we can actually do
this inside of our print, so we can just see our
basalts printed out. So if we go ahead and print, we could print out
five and that would work out tan and so on. What we're gonna do is we
can print out five plus ten. Go through all of our
mathematical operations here. Oh, here's That's not
grab all of that. We can do five plus ten. Now you don't have to have
this spacing in-between. This is purely for readability. You see we use the asterix or multiplication and we use the
forward slash for division. And it's as simple as that. We literally just five plus
105 minus 105 times 10, 5/10. And who are to save that
and give that a run? We'll see down in the output,
down there at the bottom. Soon as our program
is done building, we see 15 because five plus
ten is 55 minus ten gives us negative 55 times ten
gives us 55/10 is zero. Now, zero because ten cannot go into five
any amount of time. So this would give us a, a
fraction of a number, right? Don't go in less than one times. So because of this, since we're working purely with integers, we're just going to get a zero because we have to have
whole numbers, right? But what if we need to do a little more advanced
mathematics here? What if we want to start using things like exponents, right? We want to have, go
ahead and have never, what if we wanted to do
something like exponents? I wanted to have like
five to the power of ten, which would be five
times five times, five times five times 510 times. Alright? Because we're going
to need that at some point when you start doing some more advanced calculations. Or if you're gonna do something
with data manipulation, maybe you need to
figure out some of these things or
game mechanics. Like if you wanna
do something like a swinging from Spiderman, you're going to have
to start getting into, gonna have to bring into
knowledge of trigonometry to start figuring out
some of that stuff. Swinging arm extend the
momentum and everything. But for now, we're just going
into the exponent is also known when they say is e.g. check five to the
power of ten, right? So to get to the
power of something, we have to access
the math class here. So we have to type out
math and capital M. And then we can do dot and P 0 W. That'll give us
our maximum power. Open and close parentheses. And that will take
two arguments. A5, ten. My mistake. There's a math F
at the end there, dot POW ness capital P, capital M. And we just
pass in two arguments. The first one is
our regular number, and the second argument, it would be our exponent. So here we got five
to the power of ten. Then if we wanted to do the opposite,
something like that. First, let's go
ahead and run that. You can see that printed out
down there at the bottom. And that gives us a
total of 99,765,625. As a result. That's
quite a large number. But if we want to
square root something, so we take something
like eight squared, which would be eight
to the power of two. We take that there. So we do eight to the
power of two and we run that we know
we're gonna get 64. If you remember your
multiplication tables, that'll be eight times eight. So what if we wanted
to do the opposite? We want to get
square root of it. Well, we can go ahead
and get back up again. And we can get the
SQRT capital S, and that'll give us square root, open and close parentheses. And this takes one argument. And that is the number. That's what I get
square root of. In this case, we're
going to use 64. Again, remember
all of your lines must end with that semicolon. And now if we get the
square root of 64, we should get eight. For the second item printed out. Alright, there we go. There you go. We've seen us. Do you know what integers are? And you know how to perform these basic mathematical
operations. Addition, subtraction,
multiplication, division, exponents, and square roots. And then of course, you
can combine all of this together into one long equation. E.g. we can do five. We can do five plus, let's say eight to the
power of 2/3 that in there. And then subtract
nine at the end. And of course we're going
to work in the order of the order of operations, whether you know that as bad, miss BOD, miss headedness. I think there's a couple of other ones out there as
well then I've heard of, but they stopped coming
to me at the moment. So we look at it. We're going to work with parentheses first. So we're gonna do
what's inside here. So I'm going to do 810
power of two, which is 64. Then we're gonna
divide that by three. Which will give us, that
would give us what? To give us 20. If I'm doing this right
and I 20, maybe 21. I could be wrong on that. And then of course we're
gonna go left to right. So we have as addition and subtraction with
cabinet the same time. So start with five and subtract or five plus 21
that they subtract nine. So if my math is correct right now off
the top of my head here, I'm just going to give us what, 26 and then minus nine. So 17. Hopefully I'm
not too wrong on there. Find out. Go ahead and run
this and it gets printed out. Okay, we got 17.333 of course, because it doesn't
go into 64 evenly here. But there you go. We've got ahead and
moved into that. And now this is the
perfect time to move in because you see we have
this decimal number here. So now we have rhabdo numbers. These are called bloats. These are numbers
that are not whole. Alright? So we can take a
look at like 5.5, 0.01 of them is an integer
and the other one is a float. Now, depending on your
language you're using, sometimes these can be seen
as equal and the same number. And in other
language that may be seen as completely
different numbers. So it's important
to know ahead of time which is which and the language that you
are currently working in. Now, what if we
wanted to convert, say, our five,
right, or integer? What if we wanted to
convert that into a flux for our operations because we have to pass in a
float for what we're doing. Well, all we have
to do in C-Sharp, as we put it in a pair of
parentheses before the five. And we go ahead and
we type in float. And this is going to convert
R5 into a float for us. Now, if we go and
save that and where to print it out.
And take a look. When you see down there still
just looks like five to us. It's not make a whole lot
of a difference here. However, if we now
take that five and we were to divide it by ten, like we did earlier, where we got an answer of zero. You see that now we're
getting an answer of 0.5 because now we have a
float that is involved. And we can do this in the
other direction as well. So we can have a number like 5.7% to convert that
to an int. Same thing. We do a pair of parenthesis at the beginning and type in INT, then put it into an integer. Now if we go to French that by 0.7 after it's been
converted to an integer, proceed, we get
printed out just fine. Because that 0.7 gets left off because we only have decimal points
inside of a float. Now, we don't have to
necessarily point this part out, but we can convert strings. So if a user were
to type in 5.7 e.g. just because you needed the user to supply that number for whatever reason you can actually do or say decimal
point afterwards. And we can actually call to bloat open and close
parentheses at the end, you convert it to a float. And likewise, as
you would expect, we can go to and to convert it into an
inch into an integer. And you perform all the same
mathematical operations in the same way with floats
as you can with integers. Now, something I want
to mention here is when we are working
with variables, you may need to assign one of
your variables to a number, such as an integer, right? So say num equals ended
with a semicolon. And now you know, you can come in here
who can print out num. And this is going to give us 50. We know that and it's an integer because
we told it to be. So now we can perform anything
that an integer can do. Now here's the thing. What if we get confused in our code? And at some point we tried
to assign num 24747, e.g. we say we're going to
be allowed to do that. And we're not gonna have
any issues with that. And you can see that for
go ahead and run this. We're going to have
a successful build. Everything's going
to run. No problem. Prints out 50 because we changed the number after what's printed. However, what if we want
to make it constant? We don't want that
number has changed at any point in our code. E.g. this could be
something like gravity, something that's
always the same. What we can use this keyword
here at the beginning, CONST, CONST before declaring
and creating our variable. And that means this variable
is going to be a constant. Which simply means
that this variable can never be changed. Once we declared it
there at the top is 50, it can never be changed. Num as you see down there, it has a little red
squiggly underneath of it. And if we were to try to
make a build with this, we're going to run
into an error. And that error tells us then
our math script, line 113. Left-hand side of the assignment must be a variable
property or indexer. And such a yelling at
us because we're trying to change the property of num, which is a constant and we're
not allowed to do that. And this applies
as well to floats. So instead of int, we just type float if you want
to create a float. And at the end if you want, we can go out with the df, e.g. that's common in
c-sharp is wall. And the last thing I want
to mention here is comment. A comment is not read by the
engine when it is compiled. So anything that's
commented out, we'll do absolutely nothing. And all you have to
do is just put in these two front slashes. And then you can type
whatever you want. And nothing is gonna be, it's gonna be as if that
comment line was never there. All right, It makes no
difference at one perfect code. You can write a line of code, such as our French
here and there, but if commented out, so we'll never be
able to do that. Now, as a beginner, you can certainly comment
your code to help you understand what's going on or why you did something
that way he did. But in general, personally, I don't like comments myself, simply because comments
well, for one, you're functioning that should
discourage should be named well enough that it tells
you exactly what it does. If it tells you what it does, you shouldn't eat a comment
to tell you what it does. And not just that comments,
they'll lie to you. Right? As time goes on, new people come in and
work with your code. And maybe someone who
works with their code. And the code will be updated, will get updated in a
production environment. But the comment, and then
the comment becomes a lie. Then you think the code
is doing one thing, what it's actually doing
something else because he'd write a comment that
is out-of-date. But like I said, when you're just learning, comments are perfectly fine. That might help you get
your thoughts together, organize things logically in a way to help you walk
through things step-by-step. Alright, so that'll do it
for our Math section here. We know integers,
we know floats, we know how to perform
mathematical operations. We know how to
convert our strings into numbers that we can use, and we know how to create
a constant in case we ever need to keep a number or a variable that holds
the number the same row.
10. Mathematical Practice: Alright, for your
Skillshare practice. In this section, I want you to go ahead and of
course create a new script. And what I want you to do. So go ahead and crank this
up you a little program that uses both floats and integers and facilitates a
conversion between the two. Floats to the integers
and integers floats. And ensure to incorporate some comments just to
make sure that you understand what they
are and how they work that describe the
purpose of your code and help explain its functionality for your own future
reference in case you have to come
back to this for whatever reason in the future.
11. Creating Arrays: All right, In today's
video, we're going to go over lists. And if we want to be a
little more specific, we're going to go over
arrays within goto. Now I say within God, oh gosh, technically C-sharp does have its own version of
arrays and it has lists as a separate item
type or an item object. And sometimes depending
on what you wanna do, you kinda have to flip flop between the two of
them because they each have their own
complete set of things to do to them are
called on them. So instead of learning both
of these two things together, we're gonna do is
we're just going to learn the Godot arrays, the array class from
the Godel here. Since that's what
we're coding in. And not only that, the kind of also eliminates the need of that whole swapping
between lists and arrays in standard C sharp here. So it's kinda like a merged version of both those together, if
that makes sense. Now, we can't just come
in to our editor here. Now. You see, I do have a new
script here, of course, and I've got mine named
arrays or write.csv. Now, you might
think based off of before where we had
string integers, floats that we can come in
here and we'll say array. And we can create a new
outbreaks or say hi lists. And we can go like this. But we can't do this because
this actually creates the array for the one
that's built into C Sharp, and that's not the version
that we want to use. The monarchy is the one
that we want to use. We actually have to come up
to the top and we have to bring in a new namespace. And we have to say using
goto dot elections. And of course end that with
a semicolon at the end. And to correct this variable
that we have created, instead of array, we would
then do elections dot array. And when you do
that, same thing, here, the answer we say new. God, oh, collections dot array with our open
close parentheses and a semicolon on the end. And of course, it is a new object that
we're creating here. So this is how you
would write this. Now, this does get
a little long. How would it comes to
writing all of this out? Especially in some
lines where As you see, this takes up a substantial
amount of space. Writing out goto dot
collections, dot re. And we'll have to this later
for another class as well. So what we can do
is we can actually say in R using statement
at the top and say using, well you say gc as
two capitals as, and then having got Ow.ly. My mistake there, as I've
realized that real quick, C-sharp is not S, as we'll say, using GC equals goto
dot collections and end that with a semicolon. Mostly they're on a
slight syntax there. So instead of doing gao
dot collections dot array, we can now just do GC dot array. So we can really short
and all of that down. Now of course you
don't have to do that. But we do have the benefit of
taking up much less space, saving some of our room here. Alright, so if we go
ahead and we could save that, and it's perfect. Now, we have our open
and close parentheses here at the end. So we have new GC, dark gray
open close parentheses, and a semicolon going to
create a new array object. But what if we want to have this already have items
in it by default? Well, instead of the
parenthesis here, we're going to have a
pair of curly braces. And we can stick
our items inside. So what I'm going to use
for this is I'm going to use some grocery items, right? So this can be like
a grocery list. So we'll have tomato ketchup. And let's go with funds. Now, if you're a list does
start getting really long. Remember, anything inside of the curly braces is
considered part of this. Lock, the new array so we
could drop down to a new line. So it's hitting the Enter key. And this will still be valid. So if you want to make it
a little more concise, a little more compact,
you can do that as well. Now, what is it we are
gonna do with this list? Well, first thing we're gonna do is we're going
to print it out. So it can be JD dot print. And we're going to go
ahead and print out our list, my list. And of course, semicolon. Save it and run it. We'll see down at the
bottom and our input, our lists, getting them
clearly written out. Perfect. So we can print out these lists. It doesn't have to just be an integer or a float
or scrape course. Now, just being able
to print it out isn't going to
necessarily any good. Because we're going
to need to be able to access certain elements
within that list. So to access an element, an element in that list, right? So we have my list
when referring to get, all we're gonna do is we're
going to put a pair of square brackets beside our list. And inside of that, we're
going to type in a number, and that number is
called an index. Now, this is where
the computer and a human will think
differently here. So if we take a look
at our list here, the items in it will
say tomato was one equals to catch up
three bonds, four. But that's not how a computer, I will interpret that. As humans. We start off with one
as the first item, whereas computer is going
to start with is zero. So Peter's going to
say tomato is zero, pickles one heads-up
to bunch delete. If we come in and we type
in two for the index. And go ahead and give that
a save and let it run. And we should see
get printed out. Index two is catch-up. Do the computer, whereas us
that would be like item. So if it helps, you can kinda just think of, you can kinda count
out your eye, your items and then minus
one to get the index. If that helps you. Myself. I've been
doing this long enough that I just started thinking things starting
at zero nowadays. But that's how we can access their specific
items in our list. So if we said one, tomato, make some money initially
expect we're going to get the pickles bills and we see it down there in
the bottom on our output. And this is kind of how an
index position will work. And we could work this in
with our strings, right? I'm going to work this answer. We can say this in. We say member to pick some. And we can have,
alright. Now remember, we're going to use a
variable in a string. We have to have $8 sign,
start or quotations. And inside the screen we
have a pair of curly braces. Inside of there is where
we have our variable. So in this case, I list square
brackets and we'll say, let's go with three. High, messing that up. There we go. We go my
list and index three. So now I should say remember
to pick up some bones. Go ahead and so down
here in pencil. And then we go, remember
to pick up some bugs. In that case, it
doesn't make sense from quantity, capital. So if you wanted to come in, remember this is
the string item. Hey, it's in quotes here. So we can come in and be
then do Asheville to lower. But technically, this
isn't seen as a string, even though we would
interpret it that way after what we've
learned so far. So if you actually
want to use string, man's on it, on
this specific item, we would have to say toString. Then we can call things like to upper to lower to make it more
grammatically correct. For now. That's how, that's how we create a ray
from the god oh, collection. As well as access different
elements or items inside of that array using
what we call an index. In the next video, we'll go ahead and continue on learning more
things that we can do to an array and how we
can start manipulating it.
12. Creating Array Practice: Alright, so you're
on Skillshare. We're gonna go ahead and
take a moment here and have our little practice exercise
with using erase here. What I want you to
do is I want you to go ahead and Crick
self and lifts. That comprises of they can comprise of things
like names, greetings, vehicles, or
whatever other items that come to mind
you want to use. And I want you to use this list to hone in on your
skills on being able to access specific elements that you are looking for. So if you want in our grocery capital here,
if you want ketchup, practice being able to get ketchup right way without
maybe having to spend 5 min just what that indexes
and maybe guessing at it. Why are you using that? What I want you to do
in order to use that, I want you to take
the item that you access from your list, and I want you to use that
within some sentences, right? It's obscuring, shall we
print out to our pumps?
13. Modifying Arrays: Hey, I'm gonna go ahead
and I'm just going to use the same script
that we had before. So I've got using GC equals God, oh, collections here at the top. And I've already got
my grocery list. I created here called my list. And what we're going
to take a look at now is we're going to take a look at how we can add
new items into our list. Now of course, you're
going to want to do this because route, whether you're looking at data or maybe specific elements
or items related to games, such as maybe adding
numbers to a player's hand. If it's a card game, or maybe, maybe it's an inventory that you have
to add two items into. And there are a variety of
different situations there. You're going to need to
learn how to add items into your gut to erase. And to do that, this is
actually gonna be pretty easy. I'm just gonna go ahead
and print this out. So ged dot print my list, of course, and all your
lines with a semicolon. And I'm going to
print that out twice. And in-between, I'm
going to add a new item. So we can see this. The
way that we add a new item to our list of groceries
here. Got them. Oh, great. I'm just going to
type in the name. So my list. And we do dot and we call add on this pair of open and
close parentheses and ended with a semicolon. And what you have inside of those parentheses is the item that's
going to be added. So in our case, I'm
just going to use air quotes and type
in the word taco. I'm going to add taco
onto our grocery list. And if we go ahead
and print that, we should see the old
version without taco and the new version with Taco
down here in Ireland. And there we go. You can see our new
edition gets added onto the end of our list. As you would expect if you're
going to add something onto maybe a list that
you've written down on a piece of paper. You're probably going to
add it at the bottom. Adding onto the
end of your list. Makes sense. Now,
what about situations where you may want to insert
items not at the end, but maybe in the
middle of our list. Now, this is possible and it is depending
on what you're doing, this may be situational. What we're gonna do instead
of adding taco to the end, what we do is we call
something called Insert. And of course we have our
open and close parentheses. And this takes two arguments that we have to put in, right? So we've got type in two things. One is gonna be the position
we want to add it to. The second one is
gonna be the item. So remember, last time when we're talking about
items inside of our list, we're talking about index. And that's what we
need to add in here. So if we say it's zero, then our items is
going to be added to the beginning
of our list here. If we say one, It's going to be added
between tomato and pickles. And I think that's where I want to go
ahead and put my taco. I'm going to insert it at one. And we go ahead and run that. Second. A builder. You see there we go.
Taco is now index, are inserted as index
one into our list. And now he's pickles has moved up and it's no
longer one pickles now represented by index
to catch up green bonds. So not only can we just
add items into our list, we can also insert it into a
specific Part of our list. If there is any reason to act, you may need to do that. Now, what good is adding items
and inserting new items? If we have no way to
actually remove items. Because we may want to
keep a track or keep track of a list of items that may
be a user asked to collect. And we want to, maybe we want to remove the
items out of that list, ask the player who likes them. But what we do is we actually
call a, we call remove. And of course our open and close parentheses and end it with a semicolon and remove. In this case. We take a look. This is going to
take a very item. And we're going to put in, this is where we can type in something that we want
to put it in there, e.g. we can say patch up, right? So we say remove ketchup, my list dot remove, and we put it in catch up. Now remember this is going
to be case sensitive, the situation, so we can
go ahead and save that. And if we print that out now, we should now see
our second printout have the same list but
without ketchup it. So we can remove specific items out of our list if we wanted to, if we know the exact item
that we want to take out. Now, alternatively,
another one that we can use called removed at
and of course open, close parentheses
and the semicolon. And instead of taking an item like we did before with ketchup, this actually takes
a index position. So if we want ketchup,
we're going to have to go ahead and put in index to pass in index two when
we call remove that. Now if we run that
now we should get the exact same result here. And then we go, Let me see. Ketchup has been removed
out of our list. So that's how we can add items
to our list, remove them, and in a sense, kind of erase them out of, out of our lists when
we no longer need them. All right, so now you know
how we can manipulate lists to add new things, to remove things from
it in different ways. And in the next one
we'll go ahead and cover the last section that
I want to go over here. But it's got two arrays. And that's going
to have to do with things like length
and organization. And we'll get into all
of that next time.
14. Modifying Practice: So for our practice
exercise, this time, I want you to go ahead and
using your own list of items. Again, this could be cars, food, objects, whatever
it is you want to use. And I want you to practice
adding and removing items from your list using both
index positions as well as items,
the actual item. Go ahead and practice
until you get comfortable with how to use it and you feel ready to move on to
the next section.
15. Organizing Arrays: Alright, so I have my list
here and again I have my God, oh, collection namespace
we used at the top. I have my nucleus that
is being created. And I haven't pre-loaded with
some shopping ingredients. And today we're
gonna talk about how to organize our list. Now, this doesn't have to
be items specifically. Course, this could be numbers. Alright, so say my numlist
space that out a little there. So we'll say, let's put
some numbers in here. For our last number, Let's go. So our numbers are
slightly out of order. You don't send me 60 to
really shake things up. So now we have two lists here. One was our strings, string look, and one
with all numbers here. Now, how can we organize these? Because these are gonna
be added into specific. I just added in,
usually at the end. And if you're gonna go in
alphabetical order, e.g. having to constantly
know where to insert is going to be really, really annoying when
it comes down to it. So how can we
actually sort this? Well, we can go ahead and we can print this out so we can
see the before and after. Print out my list. And again, I'm gonna
do that twice. And in-between is where we're
going to organize things. So in order to organize this, lucky for us, we can actually
call something called sort. So we've got my list, dot, sort, open and close parentheses
and our line semicolon. And we'll see when
we print this out, just down here inside
of our output council. When we call sort on it, we see everything gets sorted
into alphabetical order. Which maybe what
you're looking for. Again, this is going
to be dependent on your situation in
which you want to use. And likewise, we can do this
with my numlist as well. And this will sort our numbers into being
in the correct order. Okay? So we can see at the top, it's all in the same order
that we wrote it in. And then at the
bottom, it's all going in a proper numerical order. Smaller to larger. I do want to just
go ahead and note in here that you can
reverse the list. We can call something
called reverse. So instead of sorting
can come in here and we could reverse it and it'll be in the same order
course as we put it there. But if you wanted, you
can go ahead and you could sort it first, right? To say sort, open,
close parentheses. We can sort it. And then we can go ahead. Hi, list. And remember this is two lines. We have to have a
semicolon for each line. And if we were to do
this and print it out, we're going to see that
our list of numbers, it's gonna be
completely sorted out. And then we're going
to reverse it. So now we're looking at not only do the organized
it and now we're showing it in Largest to
Smallest Now sit a small solar. And of course, if we did
this with our regular list, with our shopping ingredients, this is going to go from a to Z, or you can say, we're
gonna go from Z to a. There are other things that we could need and what's a very, very useful is being able to
know how long the list is. Now when it comes to gaming, you probably going
to use this for some to loop through everything, maybe a set number of times. But you may not necessarily know how many times that
number is going to be. Or maybe you want to
do something when someone's list
becomes a set size are going to get so big. And maybe you have a maximum inventory capacity that
you want to work with. Either way, we're gonna do is
we're going to go ahead and print out our list here
just like we did normally, only to get the size of it. We're going to call
something called count. Open and close our
parentheses or sorry, mistake now, no
parentheses on this one. It's just helped
with a capital C. If we were to print this out, we would see which one are you using what
using my numlist. We're going to see four
as a result printed out. That's because we
have four items. So this is more similar to
how we count the syllables. It was a 12 or. So. We have four items. Remember, our index
would only go 0-3. All right, so that's
how we go ahead and get the length of the list, as well as organize
it both going forward as well as
going backwards. Alright, that'll
wrap up our section here on when it comes to
creating these lists. And how we can manipulate
and sort them out into ways that we
need them to be. Next, we're actually
going to start using them to go through everything. All right. Well, I'll
see you in the next one.
16. Organizing Practice: Alright, so for our practice
exercise in this section, what I want you to do is I want you to go ahead and create your own little program here that glucose is going to
add items to your list. And if you want, you can
remove items out of it. So we want to completely
generate a brand new list. And we also want this list
to be sorted alphabetical, both going forwards and back. When you feel comfortable
with being able to organize your created lists. Then come back and we can
move on to the next section.
17. Foreach Loops: Alright, so to get
us started here, we're going to go
ahead and start taking a look at for loops today and how we can actually use them with the list that we
learned last time. So I've gone ahead and I've
created myself a new script. And I call it just
calling it for loops. And now to create
ourselves FOR loop, of course we're in, need
something to loop through. So what I'm gonna do is
I'm gonna go ahead and grab my list from the
arrays section last time. And I'm just gonna go ahead
and type that in there. And of course we need
to be using at the top. So we need to using GC equals, you know, collections
and semicolon. We go, we're not going to need this process section that
just like we've been doing. And I'll hit Control S to save. Okay, Now a for-loop. What is it? Why do we use it? Well, a for-loop
will allow us to perform a set of actions. Every entry, item, every object
that is within our list. So in our case,
our grocery item. So every item that's in here, whatever we put in
side of this for loop, we're going to do
it to tomatoes, we're gonna do to pickles, we're going to do
to catch up, and we're gonna do it two bonds. Now, this will obviously say times if there may
be some situation, maybe there's four or five
lines that you want to run on each item. And you just take up those four or five
lines instead of taking up like 20 lines of code. So this way we get to not
only write a lot less, but we get to fall with
kinda DRY principle, which stands for don't
repeat yourself. So this x is specifically, I'm going to be looking at a for each loop because we want
to loop through each item. Alright, so to do this, we've got ourselves,
our grocery list ear. And to loop through and
go through each item. All we do is we type for each as one word here, open parenthesis. And she would create
a AVR, right? And create ourselves
or a variable, we're going to call it item. This item is going
to represent tomato, and then it's going
to represent pickles. Then it's going to
represent catch up. And then it's going
to represent bonds. It's going to go
through each item in side of our list here. Okay? And remember to close
parentheses at the end there. And of course, curly
braces open and close. We need a pair of them. Whatever we put on
the inside in-between these pair of curly braces is what we're going
to run on every item. So if we go ahead and
we can just print out each item and save that and
go ahead and run that for us. One moment here. Because
we're not going to need that. Go ahead and run this
even a moment to build. We go, we can see we've went through each item in
our list down there, the output, and we printed
out each item as we got it. So what we can do now is we can now do something to it, right? And for this we could do that, make sure that we
have it as a string. And then maybe we do a 22 upper. Let's go with that. Will make everything uppercase and save it and rerun that. And now we're going to
print out everything, all in uppercases, each item. Now if you want to do something
outside of that for loop, of course all we have to
do is go outside of these, this pair of curly braces here. Of course we have
to stay inside of the other ones that are a
part of are ready block. So if we go after the last close to go after the closing brace
of our foreach loop, but before the closing
brace of our ready pair. So we can now come in
here and do a print. And we can say done looping. And that with our
semicolon of course. And now you see, well run
through all of these items. Then we have are done
looping at the endpoints with exited out of
that pore loop. Now, something that
I'll mention real quick is you can exit a loop early. And we'll get into
this more when we get into while loops later, which is a whole, another way of looping through or not so much looping through an object but making sure
something continues to happen. Whereas here we're just
going through as long as there are still
items in our list. So we'll get to that later, but I just wanted
to show it to you. There are ways that we can
break out of loops early, but we'll just get
into that later. Now, it is important to pay attention to
help avoid errors by paying attention to the spelling of that
to type in there. And for the curly braces,
as I've been mentioning, make sure that the
code you want to run inside the curly braces
related to your foreach loop. Now having incorrect curly
braces in some search witness, some situations can go ahead
and lead you to errors. So it is important that you double-check before
you run your code, because your code
may still execute. It just may not execute the
way that you want it to. And this can be especially important when it comes to longer pieces of code. You may have multiple levels of you may go with
these indentations. I'm SEC, as we run
in curly braces, the tags just kinda indented in. The indentation
doesn't really matter, but it does help visually
organize things for you. Alright, I think
that'll do it for this section on going
over a for each loop. And in the next video we'll go ahead and we'll take a look at the other kind of for-loops.
18. Foreach Loops Practice: Alright, so four-year
video practice here. What I want you to
do is I want you to go ahead and create
your own list. Doesn't matter what it is or what items are
within your list. But I want you to go ahead
and take that and use the for each loop and run a
block of code on your items. In some way. Maybe you want to remove item So we print them in uppercase, lowercase capitalize
them maybe or maybe you want to take them and
print them out with into sentences out
to the console. And maybe want to add it in. Added on into what's
being printed out. Whatever it is you wanna do. I just want you to go
ahead practice with those four each loops
and a list of your own. And go ahead and get yourself comfortable with using them
and how to write them out.
19. For Loops: Let's go ahead and we can take a look using the
other for-loops here. And I'm just using
the same script here. If you wanted, you can comment Delta E for each
section if you'd like. Let's go ahead and jump
into the other four loops. And if you've used GD script, you're going to be a
little more familiar with. Right now for this,
we're gonna go ahead and we're kinda working
with numerical data here. So what we're gonna do, the first show you
how this works. We're gonna go ahead
and write for space. They will have our open and close pair of parentheses here. And inside of there is gonna
be our little conditions. So what that is, is that's
gonna be a variable. Call it a value. You can
name it whatever you want. And we're going to set
that equal to one. By default. We're going
to put a semicolon. And then we're going to say, have our condition of
value less than six. Put a semicolon in there, and then we'll say
value plus, plus. Plus, plus just means
increased by one. So if you realize here, it's going to go
ahead and add my pair of curly braces to get
rid of that error. As you realized,
this is similar. If you've done GD script, you'll see some
similarities here. They just all kinda bunched
up into the top section here. Normally you would look for value less than six
and you would move on. And this value plus, plus you'd have like at
the end of your loop, e.g. and C-sharp would just kinda all condensing
it all at the top. Saying, Here's what
we want to use, the value that we want to use. Here's the condition that
we want to do this for. And then when it's done, we're going to add one to it. So we're just kinda
condensing it all at the top. So if we go ahead
and print this, Let's see, there we
go and run that. And you'll see if we print this, as long as the value
is less than six and it's going to start at one. We're going to print out 12345. And we're not gonna
go any higher. Because at that
point, once it rolls over to being 66 cannot
be less than six. Alright? So we can't run through that loop
anymore. We're done. Now, this can be helpful
for if you want to run through a set of range. E.g. we can come in here to my list and get
the count of it. Right? Now we could
print out my list, use the square brackets here
and just pass in a value. And in this case, let's
start value at zero. So now it's starting at zero. And we're gonna
go as far as our, as long as it's smaller than
the total number of items. So if we were to
print this out now, and we'll go ahead
and take a look. You see we're going to
print out all the items. Now, you saw students
with the for each loop. And that would be a
better situation because we're printing out each
item for what it is. In this situation, we're using the value as to act as an index, in this case, to access
elements within that list. So that's another way that
you could kinda access this. I'll go through this. But it's not the only thing we could do. We could, of course
come up crop and we could create a whole new list. Let's go ahead and
create a new array. We'll call it a numlist. I will set that equal to
a new array brush here. I'll go ahead and change
this back down to six. And instead what we're gonna do is we're gonna take our numlist. We're going to add
not just value. We're gonna go ahead and throw some math and
then we're going to say math f dot tau. And then we'll pass
in value comma two. So now every item, we're going to square it. So we're going to start with
00 squared is still zero, so zero will be our first item. Then we'll have,
there'll be one. So we'll have one squared, two squared, three squared, four squared, five
squared, I'll get added. And once we're done with this, just like our foreach loop, we can go outside our curly
braces for the, for block. And we'll go ahead and print out our new novelists that
we have I created. So we'll go ahead and run this, excuse me a moment to build. And we can see there's
our list is 014,916.25. And just like that, we've
used a for loop to completely generate a brand new list
for us that we could use. Now, this situation
might be a little specific in terms
of its use case. But just kinda keep in mind, you can use for each. If we go into items. You can use for regular for loops when we're dealing
with something like numbers. So that covers of both
kinds of for-loops. Here in the C-sharp language.
20. For Loops Practice: Alright, so for your
practice video here, what I want you to
do is I want you to go ahead and create some kind of code here
that will end the end, produce a numerical
list of some kind. You can do this through
generation, generating a list, or maybe you cannot find some creative
ways to go about it. But what I want you to do is
to create, at the end of it, I create a list or
other construct a list using a for-loop
and some kind of a range. Alright, go ahead
and do that and help get yourself comfortable
with using the for-loops.
21. if Statements: Alright, so we're gonna
go ahead and jump in. I have my new script already
set up, created and attach. This is going to be for our
equality checks and loss. We'll take a look at are now
inequality checks as well. Alright, so let's go
ahead and take a look. These are gonna be called
conditional tests. And what these are is we're basically going
to be checking if two things are the same
or if they're not. That's how we're going
to start things out. Now if we were to go ahead and create ourselves
a list here, I'm just gonna go ahead
and bring this inside gc as goto dot collections
and the semicolon. And I'm gonna go
ahead and just create a new PC array here. I'll call it groceries again. Here. We'll set that
to a new array only. We're going to need some values. I'm going to go ahead and
set up a default here. I'm going to call it
tomato with a capital T. And that's going
to be important here. Alright? So when we're doing inequality check inside
of this red block here, what we're gonna do
is we can actually print out inequality check. And what we're going
to get is whether or not that statement comes back as being true or if it
comes back as being false. Now, in order for us to do that, all we have to do is of
course, ged dot, right? Well the capital P, open and close parentheses
and a semicolon. And in-between
these parentheses, what we're gonna do is
we're going to access the first element inside
of our groceries here, which is going to be groceries, pair of square brackets. Inside of there. We're going to put zero,
get that first item, which is gonna be our tomato
with a capital T. And what we can do is we can go ahead
and use two equal signs. And what that means
is two equal signs is a comparison or
as one equal sign, like we did up here when
we credit our groceries, will assign a value to
least two sides here. And what we're gonna
do is we're going to use create a string
here and just write tomato, it all lowercase. Now, we'll see in
this case, we have, we need to actually call dot two string on our element inside of our list here so we can get a proper comparison. If we were to go ahead
and run that yet. Let's go ahead and
we'll do the same thing right after that, only with a capital T. Save that. And if we were to
go ahead and run that now, we'll see down in our output, it seems our practice
builds here. We're going to see that
we're going to get false as a result as well as
true after that. And the reason we're
getting false and then true is because you
gotta keep in mind, we haven't uppercase
inside of our list and a lowercase down
in our comparison. So as far as code is considered, these are two different values. This can become an issue. And why can it become an issue? Well, what if you
need a user to type in something and it comes back? This might be maybe the user is typing in an answer and you're
creating a quiz game, e.g. the user types it
in and you have to compare to see if this
is the answer we're not. But if the capitalisation
doesn't match, like we're seeing here, it's going to come back
as being wrong, even if the answer is right. Well, how can we solve that? Well, in this first, our first print statement here, what we can do is we can
ignore the case equality. We can see that we're comparing this with lowercase version. So what we can do is we can
call it to lower on it. Or alternatively, another
part here in-between. So alternatively, we can go to the one that
we're comparing it to. And we can, in this case, we just want the first
letter to the capital, so we can call capitalize on it. Now if we were to run this, all three of these statements
should come back true. Because we have our
third option here, which is printed out exactly
as it is in our list. That's a perfect match. On our printed in the middle,
we're running capitalize, which is only going to change the first character
to an uppercase, which again matches exactly
what we have in our list. And in the first print here, we're converting what we have in our list to be all lowercase. And we're comparing it to
something that's all lowercase. Now of course you can
also, if you felt like it, call to lower or
two upper on both, the item in our list as well as the one that
we're comparing to. Just to be as or just to confirm as much as he can if you really wanted to. But I see you don't
really need to. Any of these solutions will work perfectly fine and you
can see down in output, all three of our results
come back as true. That's how we can ignore casing. When it comes to this. Of course, you can do this
with numbers as well. And numbers is interesting because where we
can get into some, another comparison or a
combination of comparisons. But before we jump into that, let's take a look
at the inequality. Now. What is an inequality operator? While the inequality is when you're not checking
for something to be true. Like we are here. We're actually checking to see if
something is not true. So we want to get
that ball state. So as the stance, this is
going to come back false. But if we're just checking
this directly and then performing a block of
code based on this check. Well, that's going to
come back false and that block of code
is not going to run. So if we want it to be false, to trigger our block of code, that would be, that
will come with it. We actually do
exclamation point equals. Remember, the top two here
came back as false initially. Now if we go ahead and go
with this or see now that first one actually comes
back as being true. Because now we're checking
if they are not equal. So if they're not equal, which in this case they're not. It comes back as true. Just like our equality and where you might
want to use to upper, to lower or capitalize to
make sure everything's same. You're going to have situations
where you're going to check where things are not
equal instead of equal. All right? Hopefully that
wasn't too confusing there. But now that we
have our equality, there are two equals and our inequality using
exclamation point equal. We can take a look
at numbers, right? Numerical. So let's
go ahead and jump in. Now, let's say we can do r
equals again if we wanted to, we could say six. If we were to compare. Just like way back in
math class, right? We can check if six
is equal to six. Obviously, if we run this, this is going to
come back as true. And if we were to
run something else like five is equal to six, obviously that's going to
come back as false and we can take a quick look at that
as the project builds here. So we can see six is indeed
6.5 is not make sense. But when we're dealing
with numbers here, typically we want to check
if a user or something is equal to or if it's greater than or less
than or equal to. Now, something like
this, as an example, could be if you were creating an application for
maybe something that automatically checked id for drinking or for a movie
ticket or something, right? So if we go ahead and
whoops, my type that right? So if we pretend we haven't
asked to work with here, up here in our variables. And if I were to set that to, let's say 18, we have what is generally
considered a legal adult in the majority of
places around the world. And we came in and we
checked age, right? So if we wanted to maybe get into they wanted
to get to a club. And inside of this club, they had to be
one-to-one or older. Then what we're gonna do
is we're going to check if age is greater than or equal to. So I'm just going to combine
these two signs together, just like we did
with our inequality. Now, if you don't know
what the greater than, less than symbol is this
the one that looks like this little pointed arrow
that points left or right? As you can see here. I suggest as spam it out there, but that is your greater
than and less than quality. And we combine that
with the equals. Because if we don't, then if the rule to get into this building is
that you'd have to be 21. And we don't have
the equals in there. Then if the user's age was 21, that we'll still come
back false, right? Because 21, it can't
be greater than 21. It's equal to it. So that's why we would
do a majority of cases is greater than or equal to
or less than or equal to. And in some situations
you're going to put that less than or equal to, greater than and equal to. Instead of just
greater or lesser. Just too, catch any potential
issues that may occur. E.g. for player looses the
life, you don't want to check. If the, if the health
of the player is zero, you're going to
check if it's less than or equal to zero just in case something happens and maybe the health bugs out and end up with negative health. Because if that were to happen, then obviously that
zero is never hit. The game over is
never triggered. And now you have this infinite health
kind of bugs situation. So we're going to be,
that's why we are typically going to use a
greater than or equal to or less than and equal to. If I go ahead and run this,
save that and run it. So we'll see if age is
greater than or equal to 21. It's false. Okay,
so in this case, we are print something out. With this being true. By saying something such as, too young, you cannot
enter this building. What if we were to say
go to 21 and run this? You see, now it comes
back true because we're fulfilling that equals
portion of our condition. Now, obviously, you would
want to have a block of code that follows
this condition. You don't want to
just print out, you want your block of code to work. So how do we do this? Well, this is where the if statement is going
to come into play. So we just write f and we
have a pair of parentheses. Inside of these parentheses is where we have our comparison. So here's where we can say age greater than or equal to 21. I then outside of
our parentheses, we have a pair of curly braces. And if I just hit Enter, it'll line everything up. Nice and neat. Bright blue. And this is where we
can enter our block of code that we want to happen. If we do TD dot. Now notice there is no
semicolon on this if statement, either at the top or at the
end of our curly braces. Just like our for-loop
that we went over. It says if pair of
parentheses with the condition and then
a pair of curly braces. Right? So we were to print
out, we can say, welcome to the books,
to the establishment. Save that and we run it. So as long as we meet
the qualifications of being 21 or older, we get this message printed out. Welcome to the sound. Now, if we were older than this, of course 45, and keep
that another run. And obviously that's going to be true again because now we're fulfilling the greater than portion of that and we can
see it printed out here and I'll put again, however, if our age was under, again, we'll go with 18 this and do
a rebuild of our project. And we'll see nothing
gets printed out because this statement
is no longer true. So that's the gist of how
these if statements work. And again, you
could do this with checking items from
your list like we did. We are printing out
the tomato comparison. And this will work for, again, if you want to check equals
or inequality less than, greater than, less than equals. Greater than equals. Alright, so that'll do it
for this section here. I'm gonna go ahead
and break this if statements down further
in the next video. But for now, that'll
do it for this. That was a lot of information
you learned about the different inequalities
and how to use them, as well as how to
write out and if statements to execute
a block of code, if the condition is met.
22. Multiple Conditions: Alright, we're
gonna go ahead and continue with our if statements, but we're going to get
a little more advanced. We're going to take a look
at multiple conditions. Now. Why would we need
multiple conditions here? Well, maybe we're
creating some kind of self-serve machine
for maybe a cinema, theater place where you
can go and watch movies. But maybe you scan your ID. It takes in the age. And then based off of that, it, our machine knows what to charge the user
for their ticket. Now, in that situation, you would have
typically three Basic or three base
conditions, right? Yeah. The age is less than
a certain number. We have a child's ticket. If it's above, we
have an adult ticket. But also if it has to be
below a certain number, because once we reach a
certain age and higher, typically 65, we then have
senior prices that go on. So something like
that with a look like I don't know what
the standard number is, half for child tickets. I'm gonna be honest there.
But we'll say 15. Right? And you can go ahead and we can use this if
statement two more times. I'll say if it's greater
than or equal to 65. And I'll just go ahead and
change these print statements. And let's say here's
here is a senior ticket. And likewise, here,
here's your child ticket, or change our statement to
be less than or equal to 15. Situation. Now,
here in the middle, we need to have this adult. Alright? So in this
situation, we need our age. We need two conditions here. We got to see if
they're older than 15, but less than 65. So how would we do this? Well, we would use what is
called the and operator. Now to do this, what we
would do is we have if age and we would say
righter than 15. Right? Because it
doesn't matter if the R5 that's going to still constitute for
their child ticket, we're doing less
than or equal to. So if the age is
greater than 15. And for the and operator, we just use two ampersands here. And on a QWERTY keyboard with the Standard
North American layout, that is just gonna be shift
and the number seven up top. If I can't find it there it is. And if you're using
a European layout, My apologies, I don't
know where that is. It might be the same button. It might not, but you can
see it on the screen. Or you might just know
what an ampersand is. There's just two
of those to grain and we'll go ahead and
say age less than 65. So we don't want to say
less than or equal to because once they hit 65 or
whatever the senior team. So now if they're 15, render this one will be true. There's 65 or older. This one will be true if they're older than 15 and less than 65. This one will be true. So since we are using
this and operator, we need both of these
statements to be true, both older than 15
and younger than 65. Both of these needs to
be true in order for the entire condition to be met. At the moment, our ages 18. And if we go ahead and run that, we'll see that is older than 15.16 and we can
see printed out, we have an adult ticket. And we can of course check this. If we go down to 15 and
rebuild and run this, we should get a child's
ticket. There we go. Here's your child ticket. And remember, we're
getting that because we said less than or equal to. And likewise, if we
say 65 or older. So let's go with 75, save that, run it
through another bill. And we'll see that we
hit our senior team. Now this isn't the only kind of operator that we
may be looking for. We might be looking
for the opposite. We might be saying, oh,
if this or this is true, then we can allow them to enter. So in this case, we can
have our statement here. But instead of doing an end
with our two ampersands, say if the age is older than 15, we can say, or by
using the pipes here. And again that is just
holding Shift and. The button above my Enter
key or above the return key. On a North American
layout, European layout, I think that's in a
different position. It's been awhile since
I've seen that keyboard. You want the pikes. So that's just the
two vertical lines, and that stands for war. Now we can say something
like age equals two, e.g. and then maybe if the age is to maybe they just
get in. Right. He's just get in with that
adult taken or something. I don't know. It's a little strange with this setup when
we're looking at movie ticket prices
for this situation. That allows either of
these two become true. This case, if we were to change our
situation here to be two. Now, in this particular case, we're going to get
two things here. Come back as true. Both
our first condition and our second condition. And the reason for
that is simply because they're running IF checks
here, but that's fine. We'll look into a
solution for that. I gear in the coming videos. So if we were to run
that, you'll see, you see you that happened
down here and the output where both of these conditions are true and have we got to
pay for a child and an adult ticket
in this case? Remember, our age
is currently two. So that means our first
condition if being 15 and under, but it meets our one of our conditions in
our second argument, which is aij is equal to two. So in this statement where
we're checking or only one of these conditions have to be true to enter our
block of code here. Now, we can do this
with checking for items inside of a list as well when it comes to
our if statements. So we can go ahead and
do something with that. We would say if the
condition we would pass in would be getting our in
this case, say our groceries. And you might use
this for something like maybe band users. If this is something
that has maybe a database that have been banned for cheating
in your game, e.g. but looking at grocery
with you histories. So if groceries
and we're going to use contains and contains, it's going to require an
open, close parentheses here. And inside of these
parentheses or contains, we're going to pass in whatever
it is we're looking for. In this case, we'll say
tomato with a capital T, which we know it is in here. And we'll say Meno was found. If we run that, that contains keyword that we're calling,
it's going to search, this case our entire
list to see if what we're looking for is any of
those items inside of it. Now remember, this is gonna
be case-sensitive still. So you have to keep that
in mind of it being an exact match for
that to come back. So as you see with
a lowercase d, that comes back as false. So we never get our print statement being
sent out to our console. Now, what if we want
to do the opposite? We want to see if our list
does not contain this tomato. Well, we can put our exclamation point
right at the beginning. So now we're doing
exclamation point. Groceries. Dot contains tomato with our lowercase t. And
if we were to run that, we're going to get our
print statement here. This is our tomato was found, which it wasn't in this case. But you can see, you can
check if something is inside of the list or if
it's not inside of list, that's how you would check
list as well as the use the inequality operator to see if something is not
inside of your list. Now ultimately, what
we're getting as a result here is a true false. I like we saw in the last video when we were printing out
these conditions. And that's true. False is just called a Boolean expression. That's where an expression, like we see here just comes
back as either true or false. All right, that'll do
it for this section. And then next, in the next
section here we're going to take a look at expanding on our if statements with some solutions to the issues that we were
looking at previously, where we had those multiple
f checks being triggered.
23. if statement Practice: Alright, so for this section, for our practice video, what I want you to
do is I want you to go ahead and start
with a new script. And I want you to go
ahead and practice using all these different
conditionals that we went over. We went over quite a lot
of them shaking list, checking if things are not
enlist equal to, less than, equal to, greater than or equal to horses normal greater
than, less than. And seeing if text now
just straight up equal to. So we had a lot of
conditions there. I want you to go ahead
and practice using those. And if you want a stick them in your f blocks with
your block of code, like we still see on the screen
here from the last video. To make sure that everything
is running as expected. And when you feel comfortable
with using those. We can move on to
the next section.
24. if else if else: Alright, so here we're
going to take a look at if else statements. Now we saw what happens when we check if
something is true. But what if we also want
something to happen if that same thing is
false or if anything, that isn't true to
that condition. What did we do? What we're
going to go ahead and use the else keyword here is I'm just gonna go
ahead and Hey Stan, block of code that
I've already got here. And we're checking if
Jack is equal to Jill. We're just going to
print out this is true. Now obviously we know Jack
and Jill are not the same. They're not going to
come out as being equal. Not only do there. Well, their name says,
plays simple, do not match. So what does it do? Well, since our if
statement does not match, we are going to fall back
to this Else keyword. Well that's gonna
do is that's going to catch everything else. So if the above statements, in this case our if
statement is not true, we result or resort
to our else block. And where we're going
to run this state. Here, we're printing out
this statement is false. And if we were to go
ahead and run that, obviously that's going
to come true, right? So Alice does not have a true
or false response to it. It just catches the end
result no matter what it is. Now of course, if this was matching and it
was Jack and Jack, and obviously this will
come back as true. And we would come back
into Or statement here. This being true,
which means our else is never going to actually run. If we rebuild that, we'll see that down in
our console down here. We're printing
out. This is true. Now, if if your
monitor allows you to, you might have noticed that the color of my string
actually tweaked a bit there. So if we're looking down here,
we can see this is back, this whole line here is darker. And inside of our if statement,
it's actually brighter. So if you're able to see that slight difference
on your monitor, we can see that tweaks itself. And it kinda shows us
what our code is going to take this specific
stamps, right? It darkens here showing
us that this is false and else is
bright and lit up, showing us that that's
the block that's going to run out of our conditions
that we have set up here. So that's something
that's a little neat and as far as I know, that's not built in, so
that's just another benefit. Or running this external
IDE in this case. It's not really make
it or break it, but I don't know. It might help you when
trying to debug something. At some point. Now, this isn't
all that we have. We also have
something in-between. So if this first
statement is false, we automatically fall back to the else statement to
catch whatever it is. But what if we want
something in-between, right? What if we wanted to
check another condition? Not necessarily jump
to the else block? Well for that we would
use what's called an else if we just print that out as
two separate words, they're just like so. And just like our if statement, we have our pair of parentheses with a
condition inside of it, as well as a pair
of curly braces. Now, this will come in handy if we were to go
back and take a look at that H system that
you hadn't place. Alright, so if we were to
go back and take a look at this age issue that we had. If we remember here we had an or statement and we said
if age is older than 15, or if age is equal to two. This is when we're going to
print this statement here. In this case we're
saying this is special. And before that we have a regular f statement
that's saying if it's less than
or equal to 15, we print out This is true. Now, which do you think is
going to print out here? Which of these statements?
Let's go ahead and find out. Now this came out
as this is true. So our first condition here, the if statement block
is what was printed. Why didn't our else block here get printed because
h is equal to two. So they shouldn't came
back as true, right? Well, else if when we're
using else and else if here, only one, only one of these
conditions can be true. We're actually going
to go down in order. So if this is true, then we're going to execute
this block and then stop. We're not going to
bother to check rest of our options down here. We're going to say,
okay, that was true. Do this code, and let's move on. Let's skip the rest,
just keep going. Whereas if we had the two if statements like we had previously
where we saw our issue, this comes back with both
of them being printed out. And the reason for that is because they're
both if statements. So if they're using
multiple F's like this, then both of these
statements can be true. If you're using the else-if
with an if statement here, only one of them can be true. And that's gonna be in the
order of top to bottom. So the order that
you write that in or that you order
your comparisons in, depending on what you're doing
could be very important. So in this case, could actually take this and where to
move this up top here. I've pulled this
back here. But now if we were to do this
the other way around, and this is an else-if
we cannot start with an else-if our first
condition must be an F, then our second one here we
can turn into an else-if. Right. Now if we
were to run this, we're going to get just
our first statement there of this, especially, Yes, Remember we hit our first
condition that is true, which is our h
equals to situation. And we never hit
this section here. If we were to change
this and make our comparison of
aids equal to three. We see that we're
going to skip over that first condition
because it's not older than 15 and the age is not equal to three. So
this is not true. So we move on to our
next option here, the else-if, see what are other conditions are and see if bills, any of those are true. In this case it is. We hit our ages less
than or equal to 15, which to certainly
is. And we stopped. So we never reached
this else block because we already have
a true statement for us. So what we can do with our if, else, if and else chain, and you can come in here
and you're going to write as many else ifs as you
want here, you can come in. We have all these different
options inside of our code. And this is all perfectly fine. Now in this case they're
all the same condition, so it doesn't really make sense. Go and then have as many
different branching options like this as you want and there's
nothing wrong with that. We also saw the
comparison of if we use two or more IF
statements instead of using or versus using
an else-if statement. And we went over that your first condition must be in if it cannot
be an else-if. The else keyword here is gonna be a catch-all in
case everything else, all our other conditions don't
come back as being true. And if we wanted, we could
use these altogether for you could use this altogether for
putting things together. And remember, we can use
this with lists as well. So maybe you want to check
if this item is in our list. We do this. This item, isn't it? We do this, this item
is in it, we do this. Otherwise. Let's execute this
block of code. So we could have something
like that going on in here. Now if you want to
feel really confident and really start pulling
everything together here, you could go ahead
and make yourself a list if you would like. And then maybe you can run a foreach loop for every
item inside of that list. And we can compare that with a second list
that is out-of-stock. And we could check if
our out-of-stock list contains the current item
inside of our groceries, e.g. if so, then we can print out
this item is out-of-stock. And you can always,
I don't remember, go back and use the curly
braces and dollar sign to input that item right into our list. Alright, so I've
gone ahead and I wrote out this oxygen here. And we can see what I just went over is now written out here. So we have two lists. We have groceries and Alyssa thing
items that are not in stock. And we're going to run
through a for each loop, check each item inside
of our groceries. So we're going to
check the grocery list of things that we want to get. And for each of those items, we're going to
check if that item is contained inside
our no stock. If it is, then that item
must be out of stock. So we're just going
to print out whatever that item is and say
is out-of-stock. Now if we want to go
the extra second year, but this if statement, we can go ahead and add
in an else block with it. And we can just go ahead and say, shouldn't get that right? Should we end with
our semi-colons? Instead, we can go ahead
and do a print here if the item is in stock. So if we go ahead
and run this now, before me rebuild
and take a look down at our output console here and match it with
everything I would come up. We're checking the first item in our list, which is tomato, is, if tomato is inside
of our no stock, which it is, it's right there. So printing out tomato
is out-of-stock. Next up, we have pickles. Pickles contain inside of our no item or no stock
list? No, it is not. So that falls down
to our else block versus pickles is in stock. In this case, it should
be pickles are in stock, but whatever, that's fine. We then move on to ketchup. Ketchup contained in sidebar. No stock list? Yes, it is. It's right here. So that's true. So we print
out catch up is out-of-stock. And then we'll move
on to our final item, bonds is bonds in our list of items that
are out-of-stock, know. So we print out bonds. Again, six C-H bonds
are in stock in this case. Well, there you go. There's an idea
of how we can use everything that we've
learned so far. Together. We're looking at using lists, in this case, with for loops, if else blocks and
a conditional, seeing if something
is inside of a list. As well as using variables
inside of our strings to get more informational
things printed out to our comps for us. We'll just put a lot
of things together to make this one simple little
program that just checks. If f were basically if we're
able to pick up the items on our grocery list or
not from the store. Alright, that'll cover the
last things that we did here for if else-if else and
all of our conditionals. Hopefully that makes
sense for you. Of course, if you need to feel free to rewind the
video, go back, watch it again to help you get a little more familiar
and with everything. But of course, the best
way to get familiar is to practice and
write the code.
25. if else if else practice: Alright, so for our video
practice on this section here, and all this
information that we've gone over since last practice. What I want you to do
is I want you to go ahead and construct
yourself a little program, much like we did here
in this situation. I want you to go
ahead and practice using multiple conditions
inside of your if statements, as well as using your if, else-if and else change. How you want to go about this
is completely up to you. You could use something like this where we're going
with a for loop. Then inside of that loop we're making our
conditional checks here. Two different branches
that we could go based on. The results are what
section is true, false, or in this
case, our catch-all. Feel free to get
creative and practice these in whatever ways
you feel comfortable. The important thing
is here is that you get yourself
comfortable with using if, else, if Alice and all of the conditionals
that we've gone over. At this point. Best way to
get yourself familiar with, with these basic or rather core coding
concepts I should say, is to actually write them and make sure that you
fully understand them. Before we move on to
the next section.
26. Dictionaries: Alright, welcome here
we're going to talk about and get into dictionaries. Now, what are dictionaries? You may be asking
yourself, well, dictionaries are
a way that we can store data like arrays. Only dictionaries can
store any type of data inside of them, including
other dictionaries. This also gives us a way to clearly organize off
our information, such as game settings or
application settings, or maybe even character
and enemy details. So we're stats, right? Dictionaries have
the or make use of the curly braces just
like the arrays do. And just like the Erase do, we need to bring in the namespace for the
god oh, collection. So let's start by heading
up top in our script. And we'll bring in the
photo collections and have it renamed as GC to make
this easier for us. Let's take a look at how we can create ourselves a dictionary. So to create this, we would go with our GC. So on access to data
collection dot dictionary. We'll go ahead and
give it a name. I'll say my dict or dictionary. What does this equal to
a new NTC dictionary? And this is where
we can bring in the curly braces
and our semicolon. Now, how do we add
entries into here? Well, the way this is structured is inside of our semi, sorry, inside of our curly braces, like you see here, which have a, another pair of curly braces. And this is one item. Inside of these spaces, we have a key and then a
second argument of a value. So we come in here, we say t1 comma, and we'll just call
this value, alright? So value one. Just like that, I
need to put a comma. And in my case, I'm doing the next line just
to keep things organized. And what K2 and
value two like so. And just like that, we
can have each entry in the way dictionary works
is we get our dictionary. In this case, that's
gonna be our, my dict or dictionary. And we go ahead and we
can print this out. I'll show you how to do
that. Let's go ahead and ged dot print. And when we access this
is we go into my deck, great, because you wanted
to get your dictionary. And then we put it in a
pair of square brackets. And in this case, we are first key because that's
what we want to call us. Our key is a string
called T1, all lowercase. And if we were to
print this out, print out T1 from my dict, we would get value1 print
out to us and that's what, that's the key with this. It's always going to give
us the value and returns. So if we save that and run it, we'll see down here
at the bottom. Once our project builds. There we go. We got a
value one get printed out. Now if we change
this to access key to save it and rerun it, we're going to get the
value associated to K2, which is going to be valued to. So we can now see how
we can start gathering this information for
enemies or characters, or even again, your
game settings. We can come in here
and we can say, we can call this display
resolution e.g. right. And then we can have our resolution over
here, Ashley value. So that's how we can nicely organized everything and why dictionaries can
certainly be useful. Now, I did mention earlier that we can have dictionaries
inside of dictionaries. Now, at this point, we're going to have a
lot of curly braces. So it's gonna be
important to really keep these organized. I'm just going to
put it in a little extra spacing in there. And here's what we
would do at this point. So we have our first, we have one entry in our
dictionary at the moment. I'm going to call my
dictionary the Beastie airy. And this is going to hold information for my
creatures or monsters, enemies of the game. I'm going to call the first key. I'm going to call it
work and it's going to hold information for my order. Now the second argument here, we're not going to use a string. We're going to create a new
dictionary inside of here. I'll say new GC dot dictionary. And then we have our
curly braces with that. And we did not hit our closing one
generated. There we go. Inside here now we
can have my name. Sorry, I get that
snow colon there. And we'll say that name
is or of this species. We can do a comma, come down
and get our next pairing. And let's see, what are we
going to put it in here? We can go with something
like our health. Alright. I will say they have
a base health of 100. So just make sure that you have enough curly braces in
there for each pairing. Now, what's nice
with Visual Studio, as you see if you just
have one selected, right. It'll highlight the
pairing with it. So we can see we have
our last one as well. I guess this our last one
down here at the bottom. It tells us off our
entire dictionary. Our next one is closing off
our E-value pairing here. The one before that closes off the new dictionary
inside of this value. And then the pairing inside
of this new dictionary. So that can be,
might be a little confusing to keep track of, but make sure that you have enough curly braces,
double-check everything. And if you have an error that might cause or reason for it. But there we go. So now we have an orc entry inside of
our beliefs, Jerry. We have its name as well as its health printed
out to us here. Or at least will be
printed out to us. And again, we can come in here and go down
to the next line. And we can always do a, another one, another entry here. And press last one. Let's go with level. I will say its base level
is right, and we save that. And we can ask, we can access this again following
the same route, right? We've got B's Jerry.
And in this case, all we have is 4k because that's the only creature
that we have in here. But we're still gonna have
to access it with 4k. Now, here's the problem. If we were to print
this out here. We can see what we get is we get our dictionary,
print it out here. That looks right. It
looks how it should be. Now, you might think, okay, now we just added another pair
of these square brackets. And it's trying to
access health from it. Give it a save. We
have this error. Maybe you don't see it. You try printing it out. And we run into an
issue or build fails. Can you see the issue here
is that it cannot apply the index with curly braces
to this variant type. And all that's saying is that this is not the
right time, right? Our work here is a
quote unquote variant. We need to do is we need to tell the language to treat
this as a dictionary. So what we do after
we access 4k, because its value is
a dictionary, right? What we do is we call
doc as Gato dictionary. Open and close our parentheses. But that now is treated as
its own new dictionary. And now we can do
our square brackets and call health on it. And we print that out, our build is gonna be a success. And we're now going to see
100% down to there we go. And we will do the
same thing if we had an array in
here as an example. So if we come in
here and we have a new item that's closing
that section off a comma. There are new item. And inside of here. Let's go ahead and
call this groceries or something, right? Just like we had before, comma, and this is where we can
create a new GC, the array. And we could do all
different things, right? We can add to it, we can
look up items in it. But it's a similar aspect here. Instead of orc, who would get r. Groceries. And instead of
getting things as a dictionary, would get things as Gato, re, open and close
our parentheses. And now we can do whatever it is we want to
do with the array, right? Add things to it, removed
beings access items or elements inside of it. And that's, that's kinda
how dictionaries work. That's how we create them and access elements
inside of them, as well as create dictionaries and arrays inside
of dictionaries. So we can keep everything
beautifully organized this way. Now, you can have, of course, all this other data. So you can see that
dictionaries are not limited by what types of
data that can have in there. Just in this alone we're seeing inside of this dictionary, we see another dictionary. We say strings,
integers, list, or race. You can even use Booleans
in there and act true, false if you wanted. Alright, and that's
all cool and all. But how do we add items
into our dictionary? Alright, well, I'm gonna go
ahead and back that out. So we just have our orange here. And I'm just going
to come back here and we'll print out the speech. Jerry. Spine will be fine. I'm going to print this
out a second time, but in-between of these prints, I'm going to add
something into it. So what I'm gonna do is I'm
going to get the BCE GRE. And I'm gonna go ahead and use. Now, the easiest way to do this would be almost
to access it like an item at my line
with a semicolon. So I'll come in here
and instead of Oracle have, what do we want to put? Let's add in a goblin. And we can set this equal
to something new, right? So we can just come
in here. I would call it blue if you wanted to. And now goblin will be equal to. Well likewise, we should
be able to come in here and write this as
a new dictionary. And we could add in this name, health level and all this
information in there as well. Just write this down. Let me just sort
this out real quick. See that they're not correct. We have one-to-many in there. Then we go sort that out. Name. We can call it goblin. Health. Make things a
little bit different. Let's say they got 50 health and their base level
starts at ten. And now we're going to of course printed out afterwards
here again. So we can see the
difference we have saved. Let's try and build up, make
sure we have no errors. There we go. We can see our
new entry inside of it. We have the goblin
class setup or not class or the goblin
entry set inside of here. So that's all we have to do as the simplest form to add into our dictionary is to almost call the key
as if it existed. Then set it equal to whatever data type that
we want to put it. In. This case, we're doing a
new dictionary as its value. Now of course, if we
wanted to come in here and we can get the books, What am I oppressing? We can come in here. You get r, square brackets. Goblin. Tell it to treat it as
hey, Gato dictionary. Like we did before. We can now access the
health and print that out. And we can see the
difference here. Of course we're
accessing it now. We have no issues because
that entry does indeed exist. Alright, I'm gonna go ahead
and stop this one here. And in the next video we'll take a look at how we can loop through all of the keys
within our dictionaries.
27. Looping DIctionaries: Alright, so let's take a look
at our dictionaries here. We have our dictionaries. I'm going to go ahead and move that print and remove that one. And we can continue to just add in our goblin
at the beginning. That's fine. Because we want multiple entries in this case, because we need something
to be able to loop through. So we have our B-series setup, we have our 4k, and
we have our goblin. Now these are our two keys. Remember the dictionaries
associated with them are called the values. The value of our key. Now, how can we loop
through the dictionary? I've just by going through
all of our keys themselves. Well, we're going to use a for each loop here because again, we want to go
through each, right, that's the keyword that
we want to go through each key in our dictionary. And you can see the
autocomplete in there, and that's exactly
what we're gonna do. We're gonna get our item, which we can name it whatever
you want to represent, each object or each key
inside of our Beastie area, which again, it's
grabbing there. I'm just going to hit Tab to go through that
auto-complete there. It entered a drop that
down to a new line. There we go. So we're
going for item and b, Sherry and remember, item can be anything you
want. You don't want to be. I can BI if you want
it to be monster, monster, whatever
you want it to be, it does not matter. I'm just going to
leave mine as Mohsen now just since I've
written that out. Now, whatever we wanna do, monster is represented
by that key. So how can we go through, how
can we look through this? Well, let's take a look and see what we got
when we print this out. So JD dot print and
we'll print out monster. Let's just see what we
got going on here, right? What are we working with here for the results that we're
getting for our loop. Okay, we're getting the
actual entries here. We're getting our
full entries, right? So how can we work with that? Well, if we want the key of this item and not that entire, the entire thing, we
don't want that whole key value pair working on. Well, what we do is we
have to get monster. And with Monster we call
dot d books. Are there. We call monster dot e.
And that's key with a capital K. And if we
print that out now, we're just going to have the key of each of those items
in our dictionary. Now we have, or can we
have goblin? Right? So now we have keys
that we can work with. We just call key on our
monster and we have it. And as you can
expect, if you want, you can come in here and
we could call a value with a capital V instead of key. And if we were to print
that out, run it, build it. And we'll see down there at the bottom, we're going
to get just that. We're gonna get the
values of everything. So what if we want to
run through all of these and maybe may want to print out all the
keys that are associated to each of these values, right? So if we want to
print out like name, health level, we want to see all that stuff that
we have access to. Or maybe we want to add
a new object in to it. Well, what we're doing here
is running a for loop, right? So we can get all of our, essentially all of
our keys, right? We have our Goblin and as well as the value associated
to them in our case, we're just going
to need the key. And what we can do is
we can then run a, another loop inside of our loop. And we say for item in Monster. Sure. There we go. We're going to use
mosque but not there. So for item in and we'll get
our Beastie airy square. I am a little too far
on that keyboard. Square brackets here. And we can call monster
in here, dot key. And make sure that we get
this as a Gato dictionary. There we go. Now, let's go
ahead and print this out. Let's see what we get now. For item semicolon
to end it off, save it, create a new build
that she'll get. There we go. Now we're getting
the key value pairs here of name is Work Health 106, and then name goblin
health 50, level ten. You see now that we're getting
everything set up there and slowly but
surely loop by loop. We're going through every item
inside of our dictionary. Now of course, depending
on what it is, I can depend on what it is you're trying to
do. In our case. We're getting each
individual section here to put something. Whereas if we wanted to
just add a new item, we could have just stopped
at the first for-loop. Alright, And we kinda just
got be scary monster qui, treated as a dictionary and
then add a new monster. In F1, I put a bunch of placeholders in
e.g. There you go. There's how we can access
our are looped through the each item for our
monsters in the beach, Jerry. And then we went another
level deep and we went inside of each monster and we got all
the items inside of there. So the name House level. And again, we'd come
in here and treat it as or even just
print out the key, or we can just print
out the values. Whichever route you wanted to go with that perfectly
fine up to you. But the point is, now
you know how we can loop through a dictionary
at a base level. And if you have something
even deeper within there, such as more dictionaries
or some arrays, or however you have
everything set up, you know how to loop through
another level below. And with that you should,
between those two, you can loop through as many levels as you
need at that point. But alright, narrows how we can. There's the last bit of dictionaries I
wanted to cover here. And again, that's just being
able to loop through them. All right, next we'll take a look at how we
can actually start getting input from the user. And maybe, maybe
we'll jump into some, a new type of loop. In the next part. We'll see you in
the next section.
28. Input: Alright, in this video, we're actually going
to take a look at how we can get input from the user on their keyboard. And we're actually
going to create our first graphical interface. The user can interact with, seeing as we cannot actually
type in a console or terminal or anything during this engine or while
using this engine. So what we're going to need
is we're actually need both are ready block
and this process block. We're going to use
the ready blocked and create our interface. And we're going to use
the process block for when we're checking
for our users input. Alright, so let's test this out. And the first thing we're gonna do is we're going to check if the user presses the enter
key on their keyboard. Now, keep in mind this
is different from the Enter on the numpad. These are seen as two different buttons, two different inputs. And if this is pressed, we're just going to print
out to the console. Nothing, Nothing too terrible. Just want to get
something out there, make sure it's working. So we're going to take f and our practices and
our curly braces. Alright, so we're
gonna do if input is he pressed open and
close parentheses. And inside of here is
the Keeney want to put. So that's gonna be
p with a capital K. I always do dot. And there's all of our keys that are on
a typical keyboard, including the numpad,
launch buttons. Everything is there. Now for our case, I'm just going to type
in enter a capital E. And if you have the auto-completes or
suggestions popping up, you'll see KP Enter. That'll be the Enter
key on your keypad. If you have a keypad
on your keyboard. I do not. I'm still going to
see the options, but I wouldn't be
able to use it. Right. So qi dot
enter and all we're gonna do, TD dot print. And what we're
going to print out, we're just going to say
Enter key, pressed. Alright, save that and run that. We see when we hit
the Enter key, there it is, it pops up. Now it's popping
up more than once. But in our case, that's not that big of a deal. That's just because we're checking when the
key is pressed, which means it is held down. It's going to
continuously trigger the whole time It's held down. Whoops. Here we can see other
options such as our key. We have keys there, but we
also have var, mouse buttons, the physical bugs, which are just the physical
location on the keyboard. Which helps if you
want to support different keyboards
from all regions, I guess you could say
with different layouts. We have action just
pressed and just release. Unfortunately, key
does not have this. We can only get the
key is pressed. If we could use just press. That would mean this
would only target the onetime as soon
as it's pressed for the first time
and released means it will only trigger
when we lift up off of the key and
the term in terms. But since we are using a
key that we're getting, we have to just kinda deal
with it being pressed, right? The only way we could do
that is if we want to code in some kind of cool down for ourselves to
prevent that from happening. Which wouldn't be too hard. It's just not that big of
an issue for us right now. Alright, so we have the Kigali. Now, let's go ahead and create an input field for
our user to work with, something that our user can
click on, start typing with. Then we can work with that data that's
been submitted to us. So inside of our 3D block, we're going to create
a new line at it. This may also hear
as a text field. So let's go ahead. And I'm going to create
a new text field is gonna be on the
line edit class. I'm just going to
call it TextField. This will be a new line at it. Now this text field, I'm going to go ahead and
change its name property. That way when we grab it, grab this node later, and it'll be a lot easier
for us to reference. And I'm just going to
name this text field. Alright, we're going to adjust
the size property of it. Gonna be equal to a new vector, to a vector to just gives us a, an x and y. So it just gives us two numbers. And in the terms
of the size here, that's going to show how
big it is on our screen. Now, the x is going
to handle the horizontal or the width of it. So I'm going to set that to 500. And then the second number
will be how tall it is. And I'm just going
to set that to ten. Now we're working in 2D, so
these should work in pixels. Next we're going to
set the position because default of
course going to be zero. So it's going to be up
in the top left corner. And we don't want that position not as also
going to be a new vector two. And I'm going to set mine at 200.300 for my spot
on the screen. And now we have to
actually add this into our scene so that it can be drawn on the screen for us. So we're just going
to type in this, meaning this object that
our script is on dot. And we're going to call add child and add our text
field here as a child. Alright, so if we go ahead and save that and then run that, you should now see a
text input field on your screen that we can
click on and interact with. If we hit Enter, we can still see our code is working down. There at the bottom,
is key pressed. Now, while this is running, what I want to show
you is if we click on the remote tab here, and this only shows up while our program is still running. Who click on that? And we open up our little
control here, right? Click the drop
down arrow. We can see our text field
is right here. So when I say node, these are the pieces that
we are referring to. We're referring to all of these little pieces that create what we're looking
at on the screen. I'm gonna go ahead
and stop running that and bring the code
back up again. So now this is running. Gonna go ahead and crop this and are ready
right at the end. What does ask a question? Simple question will
say, what is your name? Simple question
for the user to be prompted with anti villain. I now what are we going to do? Well, we need to, we're going to need to get the
information from our user. When the user presses Enter. We can go ahead
and get some data. So we're going to create a new variable in the
form of a string. We're going to call it my name. And we'll just leave it at that. Yes. Then we can set
my name is equal to and now we need to get
that TextField that we shown, that I showed you
earlier, that node that we created and
it's inside the scene. So to get that, we're
going to say get node. We have our two carrots are
greater than, less than, and we have to tell it what type of node we're looking for. This is a line at it, open
and close parentheses with a pair of quotes. And this is gonna
be the name of it, which is text field. And want to get
the text property. So we say dot txt and end
our line with a semicolon. Now this variable
of my name now has whatever we typed
into our text box. Now I'm going to
get our text box. I'm going to start equal
to an empty string, so just a pair of quotes. And that's going to
clear our textbox out. Alternatively, instead
of doing that, we could just do dot clear, like that with no open, close parentheses
and the semicolon. And that is also going to
clear the textbox out. So either way, however way you want to go
about doing that, they'll both accomplish
the same thing. And then we're just
going to print out the user little statement here. Boom, Let's go and
we'll say your name is, and we'll use our
variable. We go. If we go ahead and run that now, we can see in the
bottom of our screen, what is your name? And we can type in
whatever we want. Hit enter, and there it is. Fills in. The first time is fine the
other times just because it's triggering multiple
times, remember? And those are happening
after it's already blank. Now what we could do is we can actually fill all of this in, into an if else
statement here, right? Here we say f and we
come in here, right? So we'll go ahead and get the, actually, we'll
get the set the R, my name variable. At the top. Books. I'll say if my name is equal
to after the case of this, let's say two lower too, so we can keep it consistent. This way if the user
types in quick. Aside, here, these two types of quick
we're just gonna do. We're gonna get tree, open and close parentheses. Got Wait. What's that? What that's gonna do
is close our program. So if we were to run that now, we typed in the word
quit, our program closes. And our user now has a
way to escape after that. From the input that was put in. We could do an else block here. We want to do that. Yes.
Let's go with alpha F. And we'll just move
the rest of the code that we had up inside. And we'll say else,
if my name not equal, an empty space, right? So an empty string. So if we actually have
text inside of our box, this is what we're going to do. I'm just going to call
capitalize on our variable here. Just so that it looks
proper because this is a name after all
that we're typing in. Now we can open
it, we can run it. And we can come in because
say, yes, Joey Styles, we hit Enter and we
see that only pops up the onetime, right? So we go ahead and type
in their front like Joey Styles hit the Enter
key and there we go. Everything types
up perfectly fine, no matter if we're typing in
all uppercase, lowercase. All going to come back just
fine, just as it should. And we notice that
only shows up one time because we had that
else-if check, if it is not blank, then we're actually going to
run the code that we want. And if it is blank, which would
fall under the else case. At our situation, we do
not have an else block, so we're just not
doing anything because none of these
statements become true. Of course, we can type
in quick, hit Enter. And there we go,
our program quits. There we go. We've created a basic user interface for
user to interact with. And we work with getting
inputs from the user. For this. Now I do want to
make one note here. C-sharp is very important
in your naming structure. So if you named your name of
your script input, like so. Then you're going to
run into this issue where this is underlined in red is key press and it's going to error
out, have this issue. And that's because it
is being confused. It thinks that
we're talking about the script that we're currently running and not the
one attached to Gato, not the input class and there, so if you did named input, then all you have to do
is just do one extra step and that it says goto dot input. Just to clarify in your
code that you want the input class and not
the script or working on. Just be aware if you do
rename your script here, you have to rename it in your renamed the actual
script file itself. All right, so that'll do it. Take care, Have a good one. And she got the next one where we'll go over some while loops.
29. While Loops: Alright, let's take a
look at while loops. Now, a while loop is
different than a for-loop. And the way that
it continues to, sorry, it continues to
execute a block of code. As long as the
condition remains true, it's just going to
keep looping through that over and over
and over again. It doesn't have a limit. Like a for-loop. For each loop would have
where after it runs out of items in a list, it's done. While loop will continue to keep going until you tell it to stop, until the condition it
has is no longer true. Now, let's take a look at how we would write a while loop. Well, as you might
assume to start off, we say while with our parentheses open and
close and our curly braces. Now it's just saying while true is the most basic form
that we can have in there. However, true is always
going to be true. So that might cause an infinite. I've run it infinite
block for you. Well, we could go in and
we can print it out. Alright, ged dot print. And we could just say hello. If we were to try and run this. Well, we can see it's just
gonna get stuck on the screen. It's not going to actually
load because it's, It's basically
crashing instantly as soon as we tried to run it. The reason for that now is
because we don't have an exit. There's no way for this to
exit at any way, at any point. Now, we can come in and use
this keyword called break. And if we try to run that, it's actually going to run. It's gonna say hello onetime
and it's going to end. Now the reason why
it ended and did not completely did not continue looping through is because we have
this break keyword, which it's like an emergency
exit out of your wild loop. And you don't have to use
this inside of a while loop. We can use this inside of
other situations as well. N, one that we're
going to use here is none of these situations actually because we're going
to create a new variable. It's gonna be an int,
just going to call it my num and set it equal to one. To get started. From my
true condition here, I'm gonna say my
num less than five. Now, again, if we
try and run this, we're going to hit an
infinite loop again. And the reason for this one will always be less than five. That's just a matter of fact,
it's just the way it is. One will always be
smaller than five. So this is, though it's
written different, is the exact same situation
as if we just say while true. So what we would need to
do is we would need to cause a way for this
to break itself l, which again, we could
use that break keyword, but then we won't
be looping at all. So what we could do is we
can have this num plus, plus my num plus plus, which simply just
adds one to it. Which at this point it is
now similar to a for-loop. And the way it's going
to keep looping through until our variable here, my num reaches five. Because five is not
smaller than five, it is equal to five. So what that is going to print
out a multiple times here. And we can take a look and
you can see that there we go, this all four of our
happening down there. Now there's obviously
going to be a lot more complicated situation where you would
want a while loop. And that's just one of
the things you're asking. I have to ask yourself
if you want to have a while loop or if you
want to use a for-loop. Not depending on the situation, one is going to be
easier to processor, might be easier for
you to write out for your own personal
situation and the other. And that's fine. Now what we can do
is we can also run, there's another keyword
called continue. If we wanted, we
can come in here. And when you say f, If
my num was equal to two, then all we're gonna do is
we can come in here and use this keyword called continue. And let's take a look at
what's going to happen before we have hello
written out of four times. So we can see down
there, Let's make a new build and see
what happens now. I guess it helps if I
actually saved my code first. Let's run that again. You see we're not running
into any issues. Well, what I'm running into
anything being printed, but we're running into an issue. And that issue being we
have a complete lockup. Now again, why is this? Now? Why is this? Have you guys figured it out? We're running into
here and we're hitting our mind,
I'm here, right? It's going to
eventually up to two. It's going to meet this
requirement here of our f check. And then we run it to continue. We hit this continue keyword, which it's going to
move on with our loop. But whenever actually getting
our new num passed in, we're not getting this plus
at all. It's all right. Okay, so what if we go ahead and we add that and
then we continue? And then of course, you wouldn't have to keep
that afterwards. But the times when it's not
equal to, then what happens? We're getting all
four of our hellos being printed out there. So what kind of because we're
also printing hello forum, you think? Move it after. We see Hello only gets printed out with the
three times now. Because if it's, remember, if it's too, we're not
printing anything, we're just going to hit
continue and move on to the next loop in our chain. Now, you could use these in and while loops for things such as if we have a list of items, then you could run
a loop of wire. This list has more than or while this list
has any items in it. If it has more than zero, then we're going to
keep doing something. So you could have a
situation like that. Which again, that's simple. We just come in and change
our condition here. At all we would do is we'd get a list and get the count of it. That way we can
get the amount of items that are inside of it. Now where something like
this could come in handy for a while loop and not
necessarily a for-loop. For an example, is if we just come up here and
I'm just going to bring in our God oh,
collections again. Just give me one moment
here to bring that in. Gato collections. And then let's say we have, I guess we can know
where GC dot array, we'll call it, say animals
equals new one, a new array. And the items in here we're
going to pass in when Jake, lizard, capitals, alligator, a legit again. Dog. Yeah. You know, you can just keep going with all
these different things. So one way that we could use a while loop that wouldn't
use a for-loop war. As we come in here and we say
while animals dot contains, we can pass in Blizzard. Now remember we are dealing with uppercase and
lowercase here. So you got to make sure
we get that correct. And we can just call remove your type in lizard. So what this is going to
do is this is going to run that simple code to
remove the lizard item. What it's gonna do
it continuously, it's going to do over
and over and over. So if we do, we print
this out. Here we go. Make sure that's saved. And we printed out before
we remove it every time. We can see we have lizard in there twice and then we
have it in there once. And then we don't print it anymore because it's
not in there anymore. So this is no longer
a true statement. That's one situation where
you may run into where you want a while loop
versus a for loop. So it is going to
come down to being a situational decision
for you as a developer. But those a while loop and another loop that you
now have in your possession.
30. Functions: All right, Today we're going
to talk about functions. And specifically,
you'll be learning to create your own
functions for your code. Now, what are functions? Well, we've already
been using them and I may have caught
him that previously. But these blocks
here that we had with the process and
the ready block, these are the ready function
and the process function. Now, everything from
the word public to the final curly
brace. One function. Now there are different kinds of situations here where e.g. with the two items
here in the middle. This the first one
here override, may or may not be
there depending on the function and the avoid here, bay or may not be void or it may be something
completely different. But those are things that we
need to pay attention to, need to know beforehand. Now this isn't anything difficult for you
to keep track of. So what we're gonna
do is we're gonna go ahead and create
ourselves a function that allows us to just
print out a statement greeting the user
to our program. So to create a
brand new function, we're going to put in public. And we put in the word public here
because we want to be able to call it possibly from
outside the script. Now, there can be situations
where you may want to use keyword private instead. But for the most part, you can probably get away
with public just fine. Next up is override, and in our case, we're
not going to need it. But in instances where
you do need override is going to be situations where
if I hit underscore here, hopefully you'll see a bunch of pop-ups on your end
or some autocomplete. We have things like draw, enter tree, exit tree, input, notifications,
process, ready, and so on. These are default
built-in functions. And whenever you
use one of those, you will need to use
this override keyword here when declare your function. But since we're creating
a brand new one, we're not going to need that. Now, we can go ahead
and type in a void. And we can use this keyword because this signifies
what type we use, the return keyword width. And since we're not going to
use return with anything, we can use void, which means expect nothing to be returned. So we'll go ahead and create our function called greetings, open and close parentheses, and our pair of curly braces. If we were to come in here
and saying, use return. Where Hurley I currently
the semicolon in there, returning, but we're
not returning anything. So this is perfectly fine
with that being void. However, if we had, say, turn the number two, now we have an issue here. And this could be a little
misleading because we have a red squiggly line
under the word return. Well, the issue here is
because we are returning A2, which is an integer. And because we tend to avoid up here and our function
when creating it, it's, it's expecting
nothing to be returned. So if we need to return that
to for whatever reason, it's an avoid, we're
just type int. And now our issue is fine. Now it's expecting an integer to be returned back
to us at some point. Likewise, if I remove that
too and would sit like this, you can see the red line appears again because now
we're not returning anything. We're expecting nothing here. So I'm gonna go ahead and
change it back to void because we're not going
to need anything. Like I said, all we're
gonna do is we're going to print out
for a counselor. And this is something we've
done many times before. We're going to print out. Welcome to my awesome program. Like so. And just like that, we've created our
very own function. Let's see, basic function and all it does is
print out for us, but we have created a
function for ourselves. Now in order to use this
function, we have to call it. Now what that means is we
have to use this right here, the name of our function with
open, close parentheses. And in our case, you can call it inside
of our function. Because remember when the
ready function triggers, that's when the
game or application is first opening up the scene. So all we do here is
type in the name, Greetings, open and close
parentheses, and the semicolon. And save our project. Now if we go ahead and run this, notice down in the
output. Here we go. There is the print
statement coming from our brand new
function we created. Alright, now these functions can have arguments also known as
parameters get passed in. And in our case, instead of saying
Welcome to my program, rock them to my awesome program. Let's welcome the user
to our special program. So for here, we're going to have a user as an argument here. Now, the problem here, we have our wavy underline here because it doesn't
know what user is. So we have to tell
it user is gonna be a string that we
have to pass in. And we want to welcome the user. We want to use this
inside of our print so we can put our dollar
sign at the beginning. And we'll say welcome. Use it. So now we can
try and run this, but we're going to
run into an issue. And that error is
actually going to be our right under light up here. And asked because we have
nothing passed in to our greeting function here
when we're calling it, we need and require a string of some sort
to be passed in. So we can come in here
and we can say Jimmy. Alright, let's pass Jimmy in, save it, and run our program. And now we'll see in the output, welcome Jimmy to my
awesome program. Same thing if we change this to Johnny and save that, run it. When I see now we can get a
new name, put it in there. Right now, familiar
with this concept. So this is perfectly
fine for us. Shouldn't be anything too new. The actual creation of
the function itself here. Now, what if we wanted to have multiple arguments in here? Well, what we'll
do is we just put a comma space and we
put it in the next one, the next argument that we want. So welcome to my
awesome program. Let's see. Let's use a descriptor. So it's gonna be a string. We'll call it the scripter. And it's an awesome. We're going to use
whatever we pass in, whatever word as our descriptor. And now of course we have our
error at the top end comma. And we want a
second string here, because that's what we set
for the second argument here. And our greetings function.
And we say, Amazing. Now keep in mind,
you can still use title case to upper, to lower. What have you need specifically for that word in that sentence just to make sure
it makes sense and the way it's printed
out. But there you go. We can see, welcome Johnny
to my amazing program. I see now we're
starting to get to some of these situations. We can really start
customizing our function to take these other parameters passed in to make
it a little more. How can we say customizable, reusable, and different ways without having to write
specific functions or writing the same
print statement, e.g. in our case, over and over with different different
things passed in. So you do have to keep
in mind these positions do matters so we don't want
to write amazing first. And then Johnny
similar would say, welcome, amazing to
my Johnny program. And that wouldn't really
make too much sense. Now, what do we want to set a default value to
our items here? Well, a default value
is something that can be basically set as the default. So if you don't pass an
argument in that position, then it will fall back and use the default, whatever it is. So I'm going to say
the default here. So after descriptive, when I say equals a string and I'll say, awesome, just like
we had. Begin with. And now you see I can
come in here and I can remove the second argument
when we call our function. And we're not going
to have any errors. So because I did not pass
a second argument to their descriptor is going
to automatically be filled in with it's default
parameter that we added. There's default
value of awesome. And again, we could do this
with the user as well. So go ahead and put on Don veto. And if we keep the Johnny
in there as our argument, you can see we're printing
out, welcome Johnny. Still. Which is great because
now we have that option. Or if we delete Johnny and
don't pass in any arguments. And we print this out. We see welcomed on veto
to my awesome program. So with these. Default values put in there. We have these
optional parameters that we don't have to fill in. But if we want to change
them, we certainly can. So there we have it. We've created a function. We have taken multiple
different arguments. We have some default value set. And we don't need to
add anything in there, but if we wanted to,
we certainly can. So let's go ahead
and put everything together from last
chapter or last video, I suppose we could say add this one, Let's put
it all together. So let's create a, a visual for us to be able
to type in our text field. And when we process our
text with the Enter key, what we're gonna do,
we're gonna check, I'm going to say if
the user has typed in queue or if they've
typed in quit, then we'll close
out the program. Otherwise, you can get the text and we can
pass that in to our function of printing. We can print it out
to our screen or use it inside of our
statements here. So let's go ahead and what
I'm gonna do with that, I'm going to keep greeting here, but I'm not going to call
it, sign up ready at all. We're not going to
need to anymore. And I'm going to create a
function called public void. I'm going to call this great UI. And this is where I'm gonna get that same information
that we did last time. So I'm just going to
copy and paste it in, save some time where I'm
creating a line at it. I'm naming it TextField, placing it on the screen
and giving it a size, and then adding it to our screen that way you
can be rendered out. So if we were to run this now, at the current situation,
we're going to see anything. We're just going to have
this gray box till now. The reason for this is
we haven't called it. So inside of our ready function, call our Create UI
that we created. And if we run it now, you
see that block of code runs. And we now have our
line type inside of. Fantastic. Now we can do
is our process function, again that we had in here. Previously. We are public. And remember, this is
a built-in function, so we have to use override then void because we're not expecting anything
to be returned. Underscore process C, the default if you
get built in there. And what we're gonna do in here, we're going to check
if our input, alright. Say if input is t and the key
we're going to check for. Of course, there's going
to be our energy qi dot enter inside of our if check will check if text equals
I'm just going to use q. Wrap it around there. And now
we don't know what text is. You haven't set that. So
let's go ahead and do that. I'm going to set our
variable context. I set it equal to no wine edit. That's what we
created. I lived it. Text field. And we're gonna
get the text property of it. Alright, So if that text
is called, is just q. Will go ahead and get
the tree of our program. And just call quit. Else. Go ahead and dive in. And you can go ahead and call our print statement
here if we wanted. What I'm gonna do Is everything that we
have here and stick it inside of a nother function. Or I do that. I'm going to grab my print
statements, remarked reading. And I just cut it
out with Control X, paste it into my else block. We don't need the script at all. And for the user, remember this is now text. Be created there. And I'm just going to
take that whole f-block and take it out
and just stick it inside of my greetings function. Actually keep the
variable outside of that. We only need one argument here. And I think I'll just keep user asks what I'm asking there. All right, so now what do we do? We're going to take
our text property that's going to equal whatever has been typed into
our line at it. And I'm going to pass that in as the argument or my
readings function. And then my greetings
function is going to take that first argument that we passed in can be represented
by the name user. And if user is equal to
Q, we're going to quit. Otherwise, we'll go
ahead and print it out. Now, what I should do is I
should do user.name to lower, just to be safe in this case. Alright, so let's
go ahead and check everything that we've gotten
done here. Let's go over it. All we have are ready function that'll be called
when our program lobes, we immediately call our Create UI function
that we created. And all that's doing
is creating that line, Edit Text, Edit line that
we have on the screen. Now we're going to type it in, setting the name,
position and size, adding it to our
screen to be rendered. We then use the built-in
process function, which is going to
run every frame to check what we're doing. And if we press the Enter
key on our keyboard, we're going to get
the text out of there that is inside
of our line at it. And we're going
to pass that text into our greetings function. And then our greetings
function takes that text represented by the word
user, checks if it's q. And if so we quit. Otherwise, we print
out our statement, welcoming the user
to our program. So if we go ahead and run this, should now see all that work. We go, we have our
box here to type in. And we go in with,
let's call sunny. Enter. There we go, being printed out
to our console. Right now if you wanted,
we can come in here and you could set the tax, you can empty it out like
we did last time as well. But there you go. We've put our lessons from the last video and this
video together into one. And now we're really getting something together
that's starting to visually within our code look like in application
program that we can run.
31. Classes: Alright, in this video, we're gonna go ahead
and look at creating our very own class. That we can do is to keep things organized within our projects. And what we're gonna do for
this is we're going to create our own pet class that can hold information such
as the pet's name, the pet's age, maybe the pet type and know
whether it's a dog, cat, fish, et cetera. And we can add some
functions in there, such as being able
to rename our pet. If we wanted. Maybe you want
to add some commands to it. That printout to
the screen saying a pet name is sits
down beside you. Maybe if you call the sick man. Now, whatever you
want to put in there. I'm going to show you
how we can create our very own class as
well as using that. You've had an
introduction to this already when we were
creating our functions here. And when we're creating
our UI in general, the line at it, e.g. we see we do blind at it. We declare what class
this text field is, and then create a new
object of that class. And we've been doing that earlier with arrays
dictionaries, where we create a new array
and a new dictionary. And when we add
in our own class, it's going to follow
that same pattern. So what we're gonna do
is we're actually going to come on down into
our file system, just right-click
anywhere, go down to the new option
and select script. And we should now see
the following screen. And what we're gonna
do language C-sharp. Yep. Because you can only use a C-Sharp class inside
of a C-sharp scripts. It's going to inherit from, we can leave it a note
if you wanted to. Um, depending on
what your class is, you might want to base this
off of a specific node type. But for now we can
just leave it as node. That's fine. For the template. We can do an empty one or
you can do a note default. It's not going to
matter too much. So I'm just going
to go ahead with the empty objects just so
we can get a good look. And for the name, I'm just going to call this
pet class and hit Create. Now it's not going to open
automatically for us. So we're actually gonna have
to go into our file system here and double-click on it. And now we can go ahead and
take a look and see it here. And when should we have a
completely empty thing here? You don't have
already we don't have processed, we don't
have any combat. It's completely empty
and that's fine. This is what we need to start off with
creating our class. Now, if you use the
default template, you can easily just
delete those things. The process function, ready
function and the comics. It's no big deal. Alright, so let's
go ahead and add in a string for our pet name, which will be linked by default. Let's add an integer or the age, which would be zero by default. Let's go ahead and add in
a shrink or the pet right? Now if you want to add
other data in there, you can certainly go
ahead and do that. And just for our example here, I'm gonna go ahead and
create a function. There'll be a public
function, public void. I'll call it rename our open and close parentheses
and our curly braces. And this rename function
will take in a, an argument of a string
called new name. And what we're
gonna do inside of this function is we're
gonna say pet names. So you can just type in name
equals new name like that. Or if you want to get a
little more specific, you can say this dot
pet name. Either way. They'll have the same result. It's completely up
to you if you want to include this
at the beginning. But just by doing that, we now have a class. We have some variables, some properties of our class, as well as a function
that we just call it. Let's go ahead and
take a look at that. I'm just going to go inside of our function script that
I was using last time. And now if we go
ahead and we can create a brand new class, just make sure that
you've saved your script. Otherwise this will pop up. So it's like Pet class, and we can call this,
let's say Bill. There'll be equal
to a new pet class. Alright. Now Bill, we can
come in here and we can call our
function Rename on it. And we could pass in a
string of some type. We'll call it bill as well. And we were to go
in and run this. We'll see we're not
gonna have any errors. Everything runs through
perfectly fine. If you wanted to. Make sure this is working, you can go ahead and print out the pet name whenever
we renamed the pet. So you should see
this now inside of our output console is Bill. So we know it's going through
and it's running just fine. Now, what if I called
this a private function? And we try to run this. Now. You can see we've run
into an error here. And that error is because the rename function is inaccessible due to
its protection level. And that's what I was
talking about before. And we see renamed now has
this red underline for it. This is the difference
between private and public. So I can call it in here. If I wanted to write. I can call rename just
fine inside of the script. But because it's private, I cannot call it from
outside of the script. So this is something that you need to keep in mind what I was mentioning last time, that there may be things
that you want to keep private and keep
internal to your class. But for the most part, you'll probably get away with
just leaving everything as public if you wanted to
do with being public. Of course, now we have
access to it again and we can create a new
build and have no errors. So one thing I wanted to address here as
well is if you wanted to initialize your
script as an example. Since we can't call
a ready function. But you might want to set some default parameters
for your class. Here's what we can do. You create a public void. And let's call it initializer. And our curly braces. And we'll take in
some part here. Alright, so say new name. Haven't paid for new age. And then one more
string for new, tight. And then we can easily
just come in here and set our property. So pet name equals new name, pet age equals new age, and pet type equals new
type. Just like that. Now we can come in here and
we say Bill dot, initialize, our initializer, rather open and close
parentheses and our semicolon. Now we have to pass
these in an order. Bill. I will say three. And the tight it's
gonna be a dog. And then we go. Now we can
pass in some arguments. If you want to have
some way to initialize our class with some
default arguments or default settings that we
want to be able to have control of outside
of the class to make it a little more modular
on creation, myth. That's how we go ahead and
create a class or something. And remember, a class is a nice way to keep
things organized. And altogether for everything that is maybe related
to one specific thing. Such as we'll say a
coffee machine, right? Coffee machine S keep
track of its ingredients, the amount that it has. Each coffee machine
has to keep its stock. S keep track of its own
prices for everything. And all these things
are gonna be part of the machine itself. Whereas you pressing a button wouldn't be part of the machine. Hopefully that makes sense. But that's how we create
a brand new class. And that's how we can use that
class inside of our code.
32. Regular Expressions: All right, Today we're
going to take a look at something called
regular expressions. And regular expressions
allows you to take a special pattern
that represents a specific pattern of
texts and allows you to search through a large block for something very specific. Now you've probably
run into this if you've done any
shopping online, where maybe you put in
a credit card number and you see automatically
identify your card, has a Visa card, or maybe a MasterCard
or American Express. Behind the scenes, that's
regular expression. Identifying your car, what your card is based
on a set pattern. Then that pattern is compared to you have typed in that box. First, take a look at
this variable here. We're just going to call it add. And I've broken it down
into multiple lines. Just so we can see it all. And let's say somewhere
in here is the price. And we wanted that price. This could be maybe on a website if you want to
do a way price tracker. Or in this case, maybe
just going through a specific set of checks and
you're looking for this. Well, we could do this
without regular expressions. Like you see here. We have a fancy an
array of strings. And we're splitting this text
by this dollar sign here. So everything after
it, right here, what I've highlighted would be index1 and everything
before it would be index zero. And of course, we don't know what this is looking
like in any of that. So to get that, we then have to print it out to our console. Figure out what it looks like, what each of our items
look like, okay? Decided we want element one. Find out where we
want to split it. Which in this case is gonna
be this comment here. And we do a split again on that specific item
within that list. Send it back to the text again, printed out what should be the first item
should be our price. We do it anyway just to
make sure and double-check. Okay, It is, create a
variable comp price. Set our text to be a string, and then we can convert
it into a float in order to use it as our price. Now that's a lot to go through. And this factor may change. We don't know. Maybe
there isn't $1 sign. Maybe there is maybe there's
no comma after that price. And again, maybe there
is, we don't know. The point is this is not good enough to look for
something specific. This is done manually. And if you have something
that's not going to change, you can get away with
things like this. And and a lot of cases
you can get away with not knowing or using a
regular expression ever. But there may be a situation where regular expression
would save you some code or maybe
just faster to write. Now, what I've shown you here, then we've gone over here, is pretty much a
best-case scenario. And I've got, I could
show you some examples. E.g. if we go just come down here for a
moment, just paste this in. You see all this text here. Now, obviously I'm going to
have all these errors here. But all this text here is
taking a webpage that gets, that comes back for
making a price tracker. And this specific website. To have one or two results. We can have this situation here where we have to go
through all of these splits. And then we run a for-loop in order to quickly get our price. Or we may even get back a different page
template that they use. And you gotta go through this set of splits
that to get the price. So this is going to be a little more of a real-world
situation a little bit. This case for high price tracker
on one specific website. So I'm just going
to delete all that. And with a regular expression, we can write this three lines and have the price no matter
what website It's on. In that situation, regardless of which template gets
loaded up on that page. And it'll work is that
our block of text here? And it should work anywhere
to get that first-price. Alright, so at this point, you should hopefully get an idea of why we would
use a regular expression. Now, how do we use them
and how do we write them? Well, I'll tell you right now that patterns on a
regular expression, admittedly, kinda
look like you took a keyboard and smacked
it up someone's head. And somehow it just
works like magic. And you'll see what I mean
here in just a moment. So let's go ahead
and create ourselves a new regular
expression objects. So rejects, I'm going
to call mine RE. That'd be equal to
h new rejects item. Now to use this, all
we have to do is call compile on our rejects item. And all we have to do
is pass in a pattern. Now, what you're seeing
there on screen, what that auto-fill, I'll just hit tab to fill it in for me. This is a basic pattern that allows you to
search for a price. Now, in other languages, you may only need to have
one of these backslashes. But in when using Ghetto Gatto, We need to use two of them. And Alice has meaning to us, is that we're taking
this literal, so these two back slashes. And then the dollar signs
means we're looking for a literal dollar sign here. Alright. So we're looking for and then we have our two
backslashes again again, depending on your language
again to me on the new one, but in our case would be two. And looking for d plus. So we're looking for
one or more digits. Which makes sense because
even if something is $0.99, we still have that zero
that gets written out. So bulletin for at
least one number there. And that'll work if it's
a zero for some under $1 or it'll work if it's
maybe like $12 million. It doesn't matter
how many numbers are there as long as
we have at least one. We're then going to look
for a period after that. So, so far we looked for $1, sign at least one number,
and then a period. And then after that, we're
looking for another digit. But specifically we're
looking for to did. So now what we specified is dollar sign number
at least one period followed by two more numbers. This is the pattern
we're looking for. And if we look, that's going to match our
price that's up there. We have $1.05. We have at
least one number there. We have three of them
in our example there, 199, we have a period. And then two more
numbers after that, which is the $0.99. And this is a simple pattern. I'll show you some
more complex ones. Some more complex examples
here in a moment. But to continue with this, we're looking for and we can
call this just a string. If we wanted for
the price again, we can call it a float and convert it into a
float, a fluid like. But we can just
set this equal to our rejects item
that you want to perform a search using our, using our pattern here. So we're going to search what? We're going to search this
ad that we have at the top. And we're going to get
string. Just like that. We have our price. It's just three lines
creating our object. Tell it what pattern to
use and perform a search. And now we have the
price regardless. I keep wanting to use
the word modular, but it's not really modular. It just works with so many
different situations and pieces of texts that we
don't have to get specific. Like we didn't have to
do with the top example. I'm just gonna go ahead
and write this out here. And simply price
with the capital P. In this case. Sure. I use the right one. I don't want to use
the top one there. And you'll see the difference here because we
are splitting it. But this dashed line, save your code, run it. We see there's our top piece
with the rest of that text. And then we split
it by that common. So we'd have to watch that
dollar 99, sorry, that 19999. And we haven't, There
we go. That's fixed. Strangely with yelling at the top piece for making our
float from whatever reason, even though it was
just working at, we were seeing all
this but that's fine. But then you see at the
bottom we see we have the price. It's
perfectly written out. We don't need to add $1
sign on there and we're able to just search our piece
of texts no matter what. And to show you that this
works on all pieces of texts. Alright? So I have another
long piece of text here. We don't know what the price is. It's somewhere in here. Let's go ahead and add
the semicolon at the end. And all we're gonna do
is we're going to run our search on add to. And we'll see the
price, no doubt, come up somewhere right
here, right here. Go ahead and run that. They see with tweaking
nothing to our code. We now have the new price
already working right there. As expected. Sorry I said it just works with all code because
we're looking for a specific pattern characters. Alright, so that'll
do it for this. That's regular expressions,
how we can use them, and an example of
why they would be useful for these situations. Now, I told you, I
was going to show you a little more
complicated examples of some of these patterns. So if I were to, here, we've got a pattern like
the following. Like this. If you use, well, we have another one they're
doing in line with that. We see this top one here. This is used to check if you haven't input and email
into the field block. This next one here would be
making a comparison to see if it is a MasterCard type of card. All these companies that they
follow certain patterns. So if you wanted to make
identifications again, this is how a website would
identify your card is being a Mastercard because it would follow this
specific pattern. And you can see that,
like I said before, these do start getting a
little complicated looking. And even this is
still a, I would say, fairly basic, but
can certainly be intimidating to look
at as a new person. Especially this first
one here for emails. Again, kind of just looks
like you hit someone with a keyboard and somehow
it just works. Alright, so that's it
for regular expressions. And next, next thing
we're going to tackle, I think we're going to
take a look at how to save and load
different file types.
33. Saving and Loading Text: All right, Today we're
going to take a look at how we can save and load text files in case there's
some piece of data that we want to
store in that form. So for this, I've got a save and a load function that
I've created here. My text. I'm going to go ahead
and inside of my ready, I'm just going to
go ahead and create a text that I want to store. And I'm going to
just call this text. And let's see. This is my cool example. Two same ending
with a semicolon. Right? Now we haven't thing
we want to save. How do we actually save text? Well, in order to save text, we need to open that file up. Have the right the
right permissions for that file in order to write
it to our computer, save it. Then we have to store
our data inside of it. Now, in this,
personally for 0.0, here, we do not need to
close any files or anything. And we can actually perform
this in just two lines. Save our texts, will
just create a new var f. You can name this
whatever you want. I'm just using f. Yes, that's what I'm used to for a representation of a file. And I'm going to
set this equal to file, File Access class. And we're going to call
the open function. That takes two arguments. One is gonna be the
location of our file. And in this case, I'm just gonna put it
inside of our project here. I'm gonna use colon slash slash. So I can just stick it inside of my projects folder itself. And we can name this
my Bio dot TXT. We have a comma and
the second argument, which is just gonna
be vile, access, dot, mode, flags, dot, and then in all caps, right? I'm sorry, it's not an all
caps, not nasty sharp. It's all caps in TD script, but we want to do
that and we can set the flag to have our
right permissions. And we have the location of where we want to
save our text file. And you're a little confused. Rest is just this
default folder here that over things for
approximately stored in. Right? So if we now go ahead and need to actually
store our string now, so we'll say F because we
want to access that file. We just opened a store string. And that takes an argument
inside a pair of parentheses, and that argument is
what we want to store. My case, it's gonna be the
variable called I catched. And it does not exist
inside of this, which means I can either
create an argument here inside of my file, inside of my function here, or I can just move my
variable above my ready. Either solution will work here. I'm just going to
move mine out off the ready since I don't need to
make my Save File Export. All right, so inside the ready now we're just going
to go ahead and call our textFile function. We save and run it. A second to build. And we should now
find our text file. Right here inside of our File System. If
you double-click it. Here, we see, here's
our text file. Now, we had nothing
that was saved inside of the
interesting enough. At least nothing. See, here we go. I just rerun the program
for a second time. Now that the file
actually exists. And there we go. We have our text
file saved in here. So let's go ahead
and alter this. So we have something different
for when we load it, we can make sure
that it is loading. Instead of saying, this
is my cool example texts and say if we're
going to say This, text is now loading from
a file, say Control S. To save that. Now we can focus on how we
load a text file. I'm going to go like
that. I don't know. We don't need that weird
having a file save. Now, when loading a text file, something you can do is
check if a file exists. Though it is not necessary
for you to do that. I'm going to show
you how to do that. We'll say if file access, that file exists, and
that takes an argument. And that argument is just
gonna be the same path that we used when we see a string
of Rey's colon slash, slash I text file dot TXT. Then our open, close
parentheses inside of or to create our block
for our if statement. So if this file exists, what we're gonna do
is we're going to do something very similar
to what we did in our Save. I'm going to use f as
my file representation. And instead of using
the right permissions, I'm going to use read. And we're going to open up
the file from the same path. Of course, this time
we're reading instead of getting and we're just
going to set my text. Now. I'm going to change
the variable and set it equal to f dot yet as text. So we can go ahead and
load our file, load file. And just to see the difference, I'm going to go ahead and
write my text before we load. And after we load. Alright, save it and run it. We'll take a look down
here at our output sheet. This is my cool jumbled text. And this text is now
loading from a file. So you see the original
text as well as the texts that we loaded it. Alright, so that's how
you go about saving and loading using a
text file with C-Sharp.
34. Saving and Loading Json: Alright, so today we're
going to go over how we can save and
load a JSON file. And for my example here you see I'm using a dictionary here, and it is formatted
with two keys. And both of their values
are another dictionary, which consists of stats based on the weapon type. Alright. I then have my empty save
and load functions here. Instead of TXT. Jason. Alright. Alright, so how
do we save adjacent file? Well, to do this, we're going to
start out, I'm very much the same as we did before. Our f equals file
access dot open. Our file path passed in. And we're just going to
use rise again, of course. And then our right missions. And much like our text, we only need one more line here to actually
perform the Save. And here that route just going
to use f dot store string. Only. We're not just going
to pass in our items. My dictionary up there. We could, it's not
going to look great. It's all going to
be one long line. And quite frankly is going
to be ugly to look at. Hard to edit if you want
to edit the file at all. So what we're gonna do
is we're actually going to store a string. We're going to use store wine. And what we're going to
pass in is going to be a JSON dot stringify. And we're going to pass in named Mark dictionary,
which is items. And I'm going to pass
in a second argument, which is just gonna be this
a string of backslash t, which is just going to tap in our new lines to make it very
nice to actually look at. So let's go ahead and
call our save JSON file. Let's go ahead and run this. Nothing printed out, but
we share file down in our file system and
we open it up high. This, we see, there we go. We can see it nice
and neat and ordered. All nicely tabbed in. Just looks beautiful. Now, this is something that
we can obviously edit. We can look at it visually, see what goes where,
what's part of what. But if we did not e.g. use our stringify
to organize things, then everything would be on
one line, like you see here. Which as you would imagine, if you wanted to
edit this outside, it would be extremely annoying the longer and
bigger that your file gut. And you can see we just have two items each with
two stats each. And this is already starting to become a
pain for doing that. So I'm just going to go
ahead and close that without setting discard. And unfortunate. It kept that
structure. All right. Let's rerun this again just
so I can get that proper sitting there. Here we go. I just had a little issue. Gotta was confused on which Phi, which load between
the local disk and what was actually
here and here. Just a little confusion
that this is the mat. Just reloading the project should fix that if nothing
else. Well, there we go. Now, how can we
actually load this? Well, to make sure we're
load this properly. Also have a typo in here. I said I track
instead of attack. So let's start by fixing that. And then let's go ahead
and make the bot attack. Let's make it 60 crazy that
we can obviously pointed out. Alright, so when it comes
to loading our file, again, I'm going to start
out, but yeah, check if does this file exist. And if so, then we're
going to load it. And loading it is going to take. Quite a few more steps in
comparison to the Save, whereas the Save was very close to what we
do with text files. So when it comes to loading, we're going to
start out the same. I'll just copy that line down. And instead of right, of course we're going
to read instead. Next, we need to create
ourselves a JSON object and then just call mine j is
going to be a new Jason. Next we need to parse this
out to get the result. So we're gonna say var
result equals j dot. And what we're going to
pass in here is gonna be F. Alright, our file yet as text. And then two, we're
going to pass in, true just to keep the text. And now we can go ahead
and parse this out. So let's say we don't ruin
anything that already exists. We're going to create
a new dictionary where it has a temporary
piece of data. Welcome to data. That's gonna be equal to. We're going to set this to
open close parentheses. And the reason why
we're doing this is because we're going to convert our parsed section here
as a Gato dictionary, so to say Td, tc dictionary. Then outside of
these parentheses, you just use the Jason
class to parse string. And we're going to pass in an argument for that being j for the object that we used
earlier with our result. If we add no issues there, then what we're doing
is we're gonna do j dot yet as text. So not good affections. I wanna get parsed text. This point. Then the last thing we do
is we can set our items, which is the dictionary. Then we want to
load our data into our actual dictionary, right? So we'll say items equals
or temporary data here. That this point, after going through the
previous two lines, we shouldn't have any
issues with parsing it out. So now if we go ahead and
call our node patient file. And if we remember, if we go ahead and
print this out, we're going to see our bot
attack is six and the default, and I believe we
changed it to 60. And our edited text. Go ahead and print that out
before and after we load, save and let's run it. And we take a look
down in the output. We can see bow, the attract, because we had a typo that we had
to fix it set to six. And if we look at the
one after me loaded, we can see the typo
has been fixed and the bot attack is now
set to 16th set of six. Now, the order that the
skin doesn't really matter, the fact that it changes
around to be alphabetical. Simply because when we're
accessing a dictionary, remember we're putting
in the valley, so we're gonna be
getting the bow if we want the Bose stats. Remember, we're not looking
for like index one, index two, we're
actually typing in a string that matches that key. Alright, so there we go. There's a weekend load
and save JSON files. So now you can take this and keep your game data
nice and organized.
35. Blackjack UI: Alright, so we're gonna go ahead and we're going to
create a project with a visual
graphical interface, a simple one, but
we'll have one. And then if you would like to
advance further than that, you can go ahead and
add more features, add more at a better interface, revise it, whatever
it is you want to do. Perfectly fine. What we're
doing today is we're going to create the game blackjack. Now you may know this game
also by the name of 21. So the dealer can sell a car to the player and to
themselves. To each. The player can see their cards. Then you can choose
whether or not they want to get another card or if they want to stand
and keep what they have. Now the goal of this
is to get as close to 21 as you can
without going over. And the dealer. The rule for the dealer
is they have to keep drawing until they have at
least a certain number. This number is typically 16, at least 16 or 17. Before they have to, before
they stopped drawing. If you go over 21, you
automatically lose. This is called a bust. Let's go ahead and I just
have a new scene here. And by that, again, I'll click the plus button up here and I hit user interface. And I just renamed my control to be called blackjack,
and I saved it. All right, now for
our interface, what we're going to work
with here, I'm gonna go ahead and use a texture, right? And with that, and
just click that plus up here at the top. And that is just the child. Or again, you can right-click on that control and you haven't
add tone button there. And I'll go ahead
and open this up. And I'll just correct,
capture this real quick. And inside of here has basically every node type that
we can work with it here. You see the white ones are typically things like windows that you're able to use
and work with here. I blew. All of these blue items
are used for creating 2D things to the particles,
navigations, sprite. So if you're working with, if
you want to make a 2D game, that's typically
what's going to work with there on the blue area, the red area, the red items here are all of your 3D items. A lot of them are
the same as 2D. All of the green items, which is what we'll be
working with today, are all of the user
interface items here. These are things that
you would use to create the UI of your game, or in this case, our
entire application. And then you have some additional
few options down here. You see that don't really
belong to any specific section. So I went ahead and just
selected a texture wreck. So I just came up to
the cochlear duct in texture and we have
a texture right here. You can go ahead and hit
the Create button with that located right
down at the bottom. I'm going to go ahead
and close that now. With this texture. I'm
just gonna go ahead and rename this as background. And I just went on to Google and just search
for blackjack table. And I found this image. I'm just going to drag
into the texture section here in the inspector. There it is. Now, I can hold Shift and grab one of these orange handles
and just enlarge it. So fills up this blue box. So this blue outline
here that you see is what we will see
inside of our window. I'm just going to
make sure that covers the whole thing and that
should be good to be saved. I'm just going to quickly run the scene here and take a look. Check I'm not missing anything. Alright, now, that
looks good to me. I'll take advantage
of that right now to actually capture that. Alright, one moment,
there we go. And I can see every time I go ahead and open that
up, what do we see? We have a nice table here. Again, I just grabbed
this off of Google, just go into the image search. And we can see in this case, the dealer must draw it to
16 and stand on all 17. So as we see in this case, the dealer has to go to at least 16 before
they stopped drawing. Alright? So what are we going
to need for this? Well, at a bare minimum, we're going to need two buttons. So we're going to need
our player to be able to choose whether they want
a hit or stand, right? They want to take another
card or keep what they have. So we need two buttons. I'm just going to click on my default control here
that I named Blackjack. I'm going to hit the Add button and just go ahead
and six for button. And I'll name this
one the button. And Indian spectrum
on the right. I'll add the word hit. Then I'll add another
button into the scene. And this will be
my standard button and set the texts in the
inspector to say Stan. Right? Now we can grab these buttons
and just move them where we need them. We go. And we can zoom in and we could easily take
a look at them. I'm going to expand them
to be about the same size. And quick note when you're
doing UI like this. If you look up at the
top of the editor here, we have these magnets. One of these will activate
a grid for you to look at and the other one
will enable snapping. So we can go like
this and we can get and get ourselves an idea
of how big they are. Make sure everything is lined
up where they need to be. Make sure everything
is the right size. My case, they are indeed
both the correct size. And I'm just going to go ahead
and put hit on one side. Stand on the other. There we go. Now let's see. Where would our middle point B. With a. We call this
line or middle, and hit has come
in one more block. All right? And I'm just going to
turn off my grid now. Perfect. So if our hidden sandbox
now a user has an option. But if we're not adding
cards to this visually, because that's something
that she can easily add in yourself to advance
the visuals of it. We're going to work
with a few labels, one to show our players total and one to show
our dealers total. So I'm gonna go ahead
and add in two labels. This the dealer label. And we'll fill this in
with some text here. We're saying dealer total
that over and down a bit. And we'll do the same
thing for the player. This one now, what she says
to say, player cuddled. And this will be
the player label. Alright, so if we
go ahead and run that now we can go ahead and have a nice little look with ECR dealer total up there
at the top layer, down here at the bottom, right next door, I hit
and stand buttons. So we have a good idea. What we want to put here. Alright, our UI is
looking great for everything that we
need at a bare minimum to be able to play our game. And this point,
we can Let's see, do we need anything else? No, that's all we're going
to need so we can go ahead and start moving into the
code for this project.
36. Blackjack Dealing Cards: Alright, let's go ahead and
set up the code for this. So what I'm gonna do
is I'm going to select our main blackjack note here
and add a new script to it. And you've seen this before. I'm just going to go ahead
and do a C-sharp no default and hit Create. And it's opened up. We go, go ahead and
save those files there. Alright. Now we have exactly what we're
used to at this point. We haven't ready function, we have the process function and nothing else really
going on for us here. Well, what we're going to
need is we're going to need, let's set up our
variables first. And for this, we're going to
need to use a Goto array. So let's go ahead and bring in the photo collections to using GC equals 0 dot collections. So some space here. We can say books that did not autocomplete will
create an array. This will be the cards that
were able to draw from. And out of these, the simple numbers who
have is 1, 234-567-8910. And now when we have
a deck of cards, we also have a king, queen, jack, and in blackjack, these also count as tens. So to increase or to simulate
the odds of drawing those, we're just going to
go ahead and add in a couple of more tens, so on for the king, queen jack. And if you wanted to, you can go ahead and add in a, another one in there. And even an 11 if you wanted
to simulate drawing an ace. Well, I'm just going to
work with the one through ten and the additional tense
when the king, queen jack. Well, what else are
we going to need? We're going to need
a, another ray here. Because we need to keep track of our dealer hand and
our players hand. So let's go ahead
and dealer hand. And that's going to equal
to a new array here. So likewise, we need
one for the player. Player hand equals a new array. Last, but certainly not least, we're going to need hour. We're going to need
to keep track of the total that our player
and dealer currently have. So these are obviously
going to be an integer because we can't get any
half numbers or anything, no partial numbers or
floats or dealer total. This would be a zero by default and same thing with our player. And to make things a little
easier for ourselves, we're gonna go ahead
and we're gonna get our hidden stand button just to make things easier so
we don't have to type out that long string of
code every single time. So for that we're just going to declare a business, a button. And it's variable is
going to be called the hip button, and that's it. And we'll do the same thing
with the standard button. And let's go ahead and jump in. Well, what do we need to do? Well, we can set our bars here, are hitting stand button or set. So for that, I'll
create a new function. I do not believe
we're going to need our process function
at all here. So I'm going to create
a new one. Public. We don't need anything
sent backwards. So void, set vars. And we're going to
set our hip button. That'll be equal to node. And you should be
getting a button back. And the pathing to this is
just directly or head button. You don't have to go any deeper
than that. So hit button. And we'll do the same thing with the standard button
node that'll be gotten. And again, we don't have to go any deeper than the default of just our standard
button and the semicolon. And we can call set bars inside of our ready right
at the top. There we go. We should now just be able
to call Hit button and stand button whenever we need to
access those buttons there. Now, we want our buttons to
be able to do something. Or do we want a deal our
cards out first? Let's see. Let's deal our cards out. Alright, so to keep
things nice and clean, whenever we want to add
anything to our hand, we're going to create a function here called public void it. And again, it just means
to take another card. So we'll take hit and we'll
have an argument passed in. That argument has to
be a god or array. And we're going
to assign hand to that name at all we're gonna do is we're
going to take the hand. So whatever we pass in
there and we're going to add and not the hip button. Not quite right, auto complete. But we're gonna get our
cards and call pick random. And it ends with an
open close parentheses. And with that, that's
going to give us a random card added
into our hand. And that'll make it a little cleaner for our
deal cards method. Alright, now, above or
below doesn't matter. We can go ahead and
create our function for dealing out the cards at
the beginning of our game. If you wanted, you could just go into the ready function n, type out our hit four
different times here. But what I'm gonna do is
we're going to call create a function for deal cards. And this is the
beginning of the game. So what we're gonna do
is we're going to hit. And what we're going to
hit is the player hand. We're going to do that twice. Yeah. But in-between we rotate, right? But I keep thinks
it bears will go for the player and then the dealer, dealer hand. And then we do our player again. And then we don't know
what the dealer has for their second card because their second card
should be face down. So it would be a mystery to us. So what we're gonna do is
we're going to add this by, and let's say dealer
hand dot add. And what we're adding in there
is just a question mark. That way we don't
know what it is. And if you want to display
cards in your version, you can go ahead and just
display this question mark. Alright, so we don't have
our totals or anything, but we can go ahead
and call heel cards. And if we would like,
you can print these out. Ged dot print. And we can print
out the fire hand. And we'll print out
the dealer hand. Right? Now when we go
ahead and run this, we won't see anything happen visually because everything's
still on the backend. We're not displaying any totals. What if we take a look
down inside of our output? We can see the player got 10.10. So we're sitting at a
beautiful 20 right now. It's almost impossible
for us to lose. And the dealer is sitting on a ten and
a missionary number. Now, that mystery number we know in our deck
that at most NSA ten. So worst-case. We're going to tie
with our dealer. Worst-case, we're
going to tie with our tie with our dealer. Best-case, we went in. So right now we're in a
pretty good stance right now. What's wrong, those two tens. And if we go ahead and rerun this, we got two tests again. They got eight and a mystery. And we've got two times again. So we see this is happening
pretty consistent. There we go. We've got
a seven out of ten, so we got really lucky on
those first two draws. 1.9. Three and the 108.1, 5.6. So you get the idea. Alright. So we're gonna need
to know what these, what the total for
these currents are. So let's go ahead and
create a function to get the total or the
total number, right? See what we got from the
dealer's hand and the players. And let's create a public
horse function here. Now, we're not
going to use void. We're actually going to because we want to return the total. So this is going to be an int. So we're going to
return a number. We'll say total cards. And it's going to
take in Gato array. And again, we're just
going to call it. And inside this function. Alright? And you might see this
red squiggle underneath our total cards here.
Don't worry about that. It's just because we haven't
returned an integer yet. We will. We're going to go ahead
and we're going to create a new variable of an int, call it total, set it
to zero by default. And now what we're gonna do
is we're going to check for each card in the hand
that we're given. We're going to
increase the total. So say for each int c in, and c is going to represent the card or the number that is in the current hand
that we passed into it. And what we're gonna
do is we're just going to say total plus equals c. Now this is C-sharp, so we should
technically be using a capital C here just for the C-sharp come practices here. But when we're done
with our foreach loop, we're just going to
call return total. So now whenever we call this
dysfunction passenger hand, this is gonna give
us back the total. So what we can do, what we can do now inside
of our deal cards is, before we add this question
mark to the dealer, we can go ahead and
get the total for it. Let's say dealer total equals. And we'll just call
that new function that we have total cards. And of course that's
going to need a hand. So that's gonna be
the dealer hand. Alright, so now we have a dealer total that'll be
added up for us. While we're there, we
could go ahead and set our label that we're
showing on screen. So you know, I go up to my variables at the
top here again. And I think I'll create
another one for the dealer. Label. Dealer label equals yet known. And dealer label them, pretty sure is what I named it. I'm just going to look inside
a good old real quick. Yes. Dealer label. Capital D, capital L. Yes. Alright. And I'm
going to create a, another one for
the player label, label, label and
player. There we go. So once we get the total here, we'll go ahead and pass in. Or we're going to get
our dealer label. Want to get the text
property of it and set it to a string. We want to pass in
a variable to it. So we start off with
our dollar sign and we'll say dealer portal. Put a colon and pass in our dealer total
variable. Here we go. So now if we go
ahead and run this, we'll see we had our cards at the bottom six and a mystery, and we see our dealers
total right up at the top. Right at the top here is six. And if we go ahead
and keep running that we're going to
see that's going to keep adding up and give us a beautiful total right
there at the top. And this is working great. So we have the dealers total. We have no idea what
our player total ways. It's right now, we don't
know, but we drew because we don't have any graphics
showing our cards. So it would be nice if the
player knew how much they add. Well, we can, of course, go ahead and run this as well. And we can run this inside of a do we want to run it that way? No. Okay, So inside of our TO cards, after we do that,
we can go ahead and we'll do the same
thing for our player. Let's say player total equals total cards for the player hand. And then we'll get the player label the text property and set it just like we
did with our dealer. Passing the correct values here. All right, I now when
we go ahead and run this reaction, we got 12. Dealer currently has seven. Again, dealer has
ten, we have nine. Run it again. We have three. That is a very unlucky, an unfortunate dealer has
ten, so that's looking bad. Rush. And one more time
dealer has six, we have 12. So you can see,
you get the idea. We have our random cards
and we're seeing exactly how much our total is on
the screen at the moment. Alright, so next time we'll
go ahead and jump into, or rather we'll set up our
buttons to get them working. So we actually call hit
and we can call it stand for ending the game and
seeing who wins, who loses.
37. Blackjack Functional Buttons: Alright, so let's
connect our buttons. Because at the moment, if we run this and we click
on our buttons, they're not gonna do
anything whatsoever. Visually. They're going to blink and show us that
they're being clicked. But reality, they don't
really do anything. So what can we do about this? Well, we can connect this, our buttons through
something called a signal. And we can actually
do this through code or using the
interface here. And I'm going to show
you both of those. Alright, so if we wanted to
do it with the interface, we select our button or hit
or stand doesn't matter. Here. We go to the right and
Poseidon spectrum, we have a tab called node. Click on that. And then we see
all of our signals that a button can emit. What we will be looking
for is pressed. And you would just
double-click on that and you would get a pop up. That looks like this. Should we see we got the rest signal. It's coming for I hit
button because it's blue. We're going to connect
it to blackjack because that's what
has the script. And keep in mind this
right here is this method, also known as this function, is what we're going to need to create inside of our script. So if you're using that,
keep that in mind. The Advanced tab isn't going
to do a whole lot here to Scott to passage it in there. If you want to fill out
the extra argument calls, it doesn't really matter
for us here in C-Sharp, but right now, but there you go. That's how you would do
that. You would just hit the connect button and
then you have to go in and create the function
associated with it. Alright? And for us that
want to connect it through, again to keep everything
nice and clean, I'm gonna go ahead and
create a new function here. This is going to be a void. I could create this
as private because nobody ever needs to see
this except for our code. But that's fine. And I'm just going to
call it connects signals. And anytime I connect
a single eye, just going to pass
them all into here. And then I call my function
up here inside of my reading. That way I keep
everything clean. All right, so let's
connect our function. Well, if you want to
do it through code, what we're gonna do is we're
gonna get our hip button. And we can call the
connect function on it. Open and close parentheses
and it with a semicolon. Now connect here it
takes a few arguments. The first one is going to be the signal that we
want to put in. And that's gonna be
rushed like we saw. And the second one is
gonna be the function. So for this we go
New, colorable. And that has an open
and close parentheses here of its own. And we're gonna do this
because we want to connect it to this object that
has the script on it. So this is the first object, but as a first argument and the second one is gonna
be a string again, and this is the name
of the function. So I'm going to
call it hit breast. Now I can go ahead and
come in and go public. Void. Hit press. Make sure you spell it correctly just as you typed it above. And I'm just going
to print for this. And I'm going to say, let's go. Once a sprint the player total. Now remember if you use the
UI to connect your signal, you're going to have to
make sure that you have a function with that exact name, with all underscores and anything else, if
that's what you used. And I don't gotta we can
go ahead and run this. And I connected my hit. And anytime I press it, looks like we have
an error here. Let's see. Oh, that
is understandable. I called Connect signal
before we set the variables. So doesn't know what
our hip button is. So we call it set virus. And then the next
signal. There we go. That'll fix that error. There we go. Yeah, whenever I
hit the hip button, you should see the player
total being printed out to us. Here we go. Every
time we press it. Alright, so we know
our hip button is working just as we need it to. So we can go ahead
and do the same thing for our standard button. Going to be our standard
button Dock connect. We're going to connect
the price signal again. I'm just going to hit
this stand breast and make things easier
or a little quicker. I'm just going to copy
that function and rename it than breast. And if you want, you
can go ahead and run the code or run the program, hit the button, test it,
make sure it's printing out. That way. You know what's
connected properly. Again, if you want to
use the interface, just use that note tab on the right next to the inspector. And connected up. Remember the method name
or the function name, and create that
function in your code. Okay, so now we have our buttons hooked
up and ready to go. And whenever we do, whenever we're are
hit as crushed. What do you want to
do? We want to call our hip function, which has hit. And it's only the
player can hit this. So we're passing the players
and we run this again. And we hit, hit. And it doesn't look like
anything is happening. But if we were to
print out the players and this would update. So we can go ahead and
we could just copy those lines there for the
player total and player text. And call those right
after we use it. And let's run this
now and we should see our number now
updating. There we go. We're at nine to 152-02-5305. So when you see this is running, every time we hit the hit, we add to our player. Obviously this is way too high
for any game of Blackjack. We're going to have to
acknowledge that portion, but that'll come
when we look at the when enlarge portion of this. And our standard button for the moment won't do
anything because for that we have to be able
to declare a winner. And we would have a
dealer draw function. We can at least do the dealer draw and have them draw
to a certain number is. But let's go ahead and do that. So I said I was going to
call this dealer Draw. Realloc, expel it
one of these times. I'll create my function
for function public, void, dealer, draw. So the first thing we need
to do is we need to remove that question mark
out of that hand. Alright, so let's
go ahead and get the dealer's hand
and call, remove. And take that
question mark away. Not rubbing the player hand. Get that out of there. We're just going to remove
that question mark. And then we need to
immediately hit or dealer. So we can replace that
question mark for us. And then we can get our
dealers total dealer puddle. Now, not exactly. We'll set our dealer
total equal to total cards and pass
in the dealer hand. Alright, so now we
can check, alright, because now we know exactly
what the dealer has and we could check or rather run ourselves into a
nice little loop for us. So we'll say while the
dealer has less than 17, or again, remembering
some games, the role might be 16 for
the dealers minimum. Based off of the table
that we have here. Dealer must draw to 16. So we're going to use 16 as the SD number that we're
going to work with here. Alright, so right here,
dealer must Rhonda 16 and stand on all 17. Right? So we can go into a wild loop. So we'll say while dealer
total less than 16. If you want to go a
little more advanced, who say while the dealer
is total is less than 16.17 because according
to that table, if the dealer has 17 who
must stand but dealer, as long as the total is
less than 16, you draw. So that should be
perfectly fine for us. So while the dealer has
less than 164 total, we're going to
continuously hit on the dealer's hand and
then get a new portal. Dealer. Total equals our total cards
and pass in the dealer hand. There we go. Once all of that is
completely done, we'll just set our texts
for the dealer label again. We do that already. Right there. I'll just grab that and paste
it in there. Alright. So though we do not
have a win or loss, we should be able to hit stand. And our dealer is going
to keep drawing until he reaches a number over 16. This is interesting. Back then it keeps going. 24, 26. So why does it keep
going? Let's take a look. Okay. That's fine because we're
gonna go into a ah, declare winter after that. So the fact that
that doesn't stop or fact that we can't or doesn't stop when
we hit it. Fine. It's just a little interesting. That doesn't seem
to stop. With this. I'll write because we're
hitting an extra one anyway. Okay, Yeah, that makes sense. Remember where I calling you
hit here at the beginning. So we're always adding
at least one card. That's fine. The box wherever we
won't be able to use Spam, stand and wait. Stan wants to dealer draws and then we'll be able
to declare a winner. Um, so let's see what
do we need to do? We need to declare a winner. We know whether the
user wins or loses. And we need to be able
to check the player. The CFA, go over 21
with a drawing or not. And I think that's
all we have left. So let's go ahead and implement our wind conditions are
winning game over next.
38. Blackjack Declaring Winner: Alright, so let's
go ahead and get our win loss conditions. What in here? So what
we're gonna do first is we can create our
Check layer function. And what this is gonna do is it's going to take a
look at our total. And we're gonna say, if
the player is over 21, then that's what
we would call our, our declare, our winter. I jumped straight into that section and we could
disable our buttons. Would like this. There's no reason to be
able to press them anymore. I'll see where I
want to put this. I'll just add it down here
to the bottom, right. So say public void set, where we already have
our player totals here. So we'll say if our portal
is greater than 21, then what we're gonna
do is we're gonna take our hip button and
call disabled. And not false. We're going to set it to true. And we're going to
do the same thing with our standard button. Now, where do we need to call
this check player function? Well, we need to check this right at the top
inside of our ready. So after we've
dealt to our cards, we can go ahead and check layer. And the reason why
we're doing it there is because once
we deal our cars, we have our portal for
the player, right? So if we come down
and take a look, we've already got our total at this point and our
cards have been dealt out. So we can check the player initially right
at the beginning. Maybe they got lucky. And maybe they didn't.
Yeah, Who knows? If you have aces in
your game, you know, maybe they drew two 11s
and now you have to make special conditions or you just
go to a straight GameOver. All depends on how you want
your game to work there, but we're going to call that
rent at the beginning there. And where else are we
going to call that? Where we're going to
call it whenever we use hit the hit and use
our hip button. We can call check player because we have
a brand new total. So we need to update and
take a look and step. The only place we need
to check our player? I believe so, yes. So if we go ahead and
run our game, Go ahead. We see we got 15. And if we
do hit are probably going to go over and our buttons should disabled so we can
no longer press them. And we go solver at 23. And for some reason I can
still hit our buttons. Alright, why is this? Alright, maybe the
game just didn't. I didn't hit save on my script. Maybe how that's possible. But you see now we
go ahead and hit it. 16, hit again. We're over 21 and the
buttons are disabled. I can no longer press anything. No matter how much I try, I just get the box highlighted. Alright. So we know that is working
for our Check layer. Now the only thing we
would do after that is called Arctic clear winner. But we can't because well, we don't have that ability yet. We have to create that function. So Let's go ahead and
declare our winner, public void, clear winner. And with this we're
going to jump into. So we're going to start
by disabling both of our bonds because once
we stand, that's it. The games coming to an
end one way or another. So we get the dealer not doing that in there
because we've already done that wrong to check. If the player total is greater than
the dealer total, then the player
would win, right? Well, we have to make sure that the dealer total
is also not over 21. Otherwise, we have all the
dealers are the players at 24, which technically
gone over GameOver. But the dealers at 16, the player wins, but
we don't want that. We want the player to lose. It will say if player is
greater than the dealer tonal or dealer portal. Greater than 21. This where I'm just gonna
go ahead and write. And I'm just going to
print out, you lose here. I'm sorry, I got this mixed up. If you win on the
first one. All right. So if the dealer is over 21, or the player is greater
than the dealer, the dealer bus or the player is. Then we're going to take away. And next we're going to
run to another F here. And the one we're going
to run here is if our player modal is less
than the dealer portal, or right, we're
using those pipes. Or player total is over 21. And we can print out there. This is our you lose
situation, right? Because the player has gone
over 21, they've busted. Or the dealer is just
higher than them. And our last condition
then we can put in here is simply checking
if we have a tie. Say else-if, or we could just
do an if in here as well. We don't wanna do
that one equals. We'll just go ahead
and print out Ra. Alright, so where do we need
to call declare winter? Well, let's see. Do we need to do it when
the dealer draws? No, we need to do
it when we hit. Know that we need to do it
when we check the player. Yes, if the player
has gotten over 21 and we go straight
into a clear winner. Adding up our total cards? No. When we deal cards, no. However, when we sit, stand after our
dealer has drawn, then we can call
the clear winner. And then we're checking
player when we hit. And I think that's I think
that's everything for us. Now if we go ahead
and run this now. So you've got 13,
dealer has two. I'm gonna go ahead
and hit because the dealer has to
get to at least 16. I've got 19, I'm going to stand. Dealer hit 22 players at 19 and we're told
that we win and we lose. Well, that's not
very reassuring. So we should fix that. Let's see what happened here. Well, our dealer
is over 21. Okay? So we should win, but then we also
got Player total is less than the dealer coal. So we got both of our if
statements to come true. What we should do is
have an else if here. And let's try running
our game again. Go ahead and save that. And
we'll try playing this again. We're at nine. It's run work. Well, and again, 14, we're 18, we're going to stand
divorce at 20, we lost. Alright, Those are the
roles that makes sense. And you can run this again. We're sitting on for 14, 16. We're taking a gamble here at 16 because the dealer
has to get 16 or higher. So does a small
change, we will tie. So I'm just going
to hit any way. We hit 26 and it says we win. Well, we need to fix that. All right, so I'm
gonna show you how to use multiple conditions. I want to say, if
we want to say e.g. if this and this is true or this produced the result, right? So what we do is we wrap our first condition and
a pair of parenthesis. And then we can go ahead and put our multi statement details. So we say add player total
is less than or equal to 21. Software player has more than the dealer and it's 21 or lower. Or if the dealer goes
over 21, then we win. Else, if we say if the player
is less than the dealer or the player has gone
over 21, then we lose. And if they're the same, to draw, we can also, if we wanted to, we can
come in here and add a second condition
by wrapping these, wrap up this section with
a pair of parentheses. And we could do the
same thing as saying n, the dealer total is less
than or equal to 21. Let's go ahead and do that. This little extra dealer total is less than or equal to 21. We save that. And
now if we run it, or games should be
working perfectly fine. Now, 17 stand, we both got 17. We see printed out
at the bottom. We have a raw, beautiful it's exactly
what we would expect. Let's hit, I hit 30, I went over, I
automatically lose there. And if we run this again, 15, gotta go again,
16, that's risky. We hit again, 19 will stand. Dealer hit 16 and they stop
because that's the minimum, 19 higher than 16, and we win. Alright, so if you wanted, you could go ahead and
use another label. If you wanted to add some texts, show on-screen whether or
not the user won or lost. Stand there at 20,
they got 18, I went. You can do more graphically
if you would like. Advanced this project into
really being your own. At the moment, we have the
bare minimum here to work. We have the ability for the
player to hit and stand. We have the photos being
displayed on the screen. And we have a way to determine whether our player
wins or loses in this game with our dealer
automatically drying to the minimum required
for this game. Alright? And just like that, we've completed a
project in C-Sharp.