 In episode 1.4, you're going to be learning what methods are, how to create your own methods, all the different types of methods you can make. I'll explain you what method arguments and parameters are. Hello everyone, it is CryptoGround here. Welcome back to another Unity idle game tutorial video. This is episode 1.4, and this is the 2021 edition. If you enjoy this video and if you learned something new, make sure you smash that like button, subscribe my channel if you're new, and also consider turning on the notifications for future notifications of videos and live streams. Anyways, let's get on with it. So in this video, we're just going to be doing stuff in our controller script. So any method I'm about to make here is purely for demonstration and not going to be used towards the actual game. So first of all, let's explain what a method is. Well, we've created quite a few. We've made three right here and we've made a special one right here, which this is called a constructor as I explained in the past, but we're going to be looking at these three specifically. So we first have our access modifier, which is public private internal stuff like that. And then we have void. So what does void mean? I'll explain to this in a second, but let's skip to the name now. So this is the method name. This is what you'll use to actually call the method. So when you're calling a method, you simply just type the name, generate Flas, and then the parentheses and a semicolon like that. Pretty simple, right? So now when we write a method, well, let's say we want to create a method that we want to display something or we want to do something like subtract Flas. Okay, for example. So when we subtract Flas, we're just going to do data Flas minus equals one, for example. I'm actually going to call this subtract Flas, but a void. So this is a void method. So what does void mean? Void means that we're not returning anything. So you can actually change this to a variable type such as public int get a number, for example. So what we're going to do here is print something in the console. I'm just going to say hello world. So in here, we can leave it as is, except there's one issue. However, over this red line return statement is missing. Oh boy. So what does that mean? Well, we need a keyword called return. However, you can't just do return. We have to return an int variable. Now you can either do this by creating a variable called int temporary variable. For example, I'm just going to set that equal to three and just return temp var. So this is one way of doing it. We're basically returning three. So if we do this, let's go to our start method and call get a number like that. So now on start, it should print three in the console. And actually what we can do here is that you see this is gray underlined. So this is Ryder telling me that I can make this private. Now most IDEs will probably not tell you that this is some benefit that Ryder would give you. It can tell you some cool stuff like this. So I'm going to make this into a private int method. Now, the reason why I'm making it private is because I'm only accessing this in this class. Now, if I was having another script, like in data or some other script, it was accessing get a number. This would have to be public. So we're just going to leave it as private. And since it's not being accessed by bun, we can also leave it as public. Let's run it and you can see it says hello world. Oh, one more thing. So it's it's printing out hello world. So we know that's being called, but we actually need to print get a number. So the cool thing about an int or variable method is that we can treat this as a variable. So if we sent, so if we make a var, which is where we can determine what the variable is, whatever we set it to. So like right here, an int temp var, we can just replace int with var. And we'll know that temp var is a integer. But we're going to leave it as an int just so you guys understand what that is. And in here, we're just going to set var number equal to get number. So we could set this to a number variable and we're just going to print that number in here. Also fun fact, print right here. This is a method right here. And we are putting in a variable in here and I'll explain what these are called. So as you could see it printed out three. So we are returning the number three in here. Cool. So now let's do something special. What is this in here? How can we put variables inside of a method? Well, here it's called an argument. How do you create an argument? How do you get it to put in a number in here like three? Like what if I want to get a number and set it to seven? Well, it's pretty easy actually. What we can do here is delete that go to our private int method right here and we're going to create a parameter. So a parameter is the is the variables that we are going to be using inside our method here and that we will need to take in as an argument. So in here I'm just going to do int number and we're going to set our temp var equal to number and actually what we can do is just return the number instead because that's just a bit useless unless we are doing some funky things with this variable here. So now we are returning the number that we actually put in. So that seems kind of boring. Let's actually add it by one. So we're going to do is do number plus equals one just for example. So now we can do is set var number and we're going to get a number and we're just going to put in six. So now this is a parameter, which is number, which is type integer and six is an argument. So this is what we're putting in. Hopefully that makes sense. Those terms are not as important, but if you ever if you ever hear these in like a coding chat or some forms online, you may see that type of language. So now we run it should print out seven because we're adding one to six, which is what we put in very nice. You can do this stuff with string as well. So let's create a new method called private string modify name. So in here, we're going to take in a string as a parameter and we're just going to just call this original. And what we're going to do is we're going to modify this string in whatever way we want to. So what we're going to do here is return a space plus original plus some funky stuff, whatever we want to hear just some random characters, I guess, and I'm just going to put this is a string and then just actually, yeah, we'll just do this is a string and then it's going to show the original string that we put in. And now instead of get a number, we're actually going to replace this with we're actually just going to delete this variable altogether. And now we're going to print modify name and then we're just going to put hello world. So now what it's going to print is this is a string space hello world. So what we're doing is that we're just joining two strings together inside our method. Remember, this is the argument. This is one of the parameters. Cool. So now it says this is a string hello world. So you can actually have more than one parameter in argument. So what we're going to do here is add an int parameter and I'm just going to put count. Let's say we want to print this more than one time just for random example here. I'm going to create a I'm just going to create a var which will be a string and this is just going to call this is going to be called str which is short for string and we're going to set this to this is a string space semi colon and in here we're going to use our count to add this to string new more than one time. So we're going to use a for loop. So for loop is very easy. It uses the key term for and you just do int i equals zero. So basically we're starting at zero and it's going to iterate until a certain limit like if we put I is less than three. It's going to run three times because it goes zero one two three and then once it goes to three, it'll immediately stop and not run the code inside the for loop because three is obviously not less than three. So we're going to do so my colon I plus plus. So basically we're incrementing I by one every time we loop and I can also do a print I just for demonstration and what we're going to do is do str plus equals original and then plus a space just like that and you can make this into a var because zero is an integer. So we're just going to set I to zero and replace this three with count. Okay. And then we're just going to return str instead neat. So now what we're going to do here is we're going to put in a string here. I'm just going to put CG as the string and I want to display five times. So there you go. We have our two arguments here CG in five and our two parameters original which is a type string and count which is a type integer. So now when we run it, we should see this is a string and then CG five times cool. This is a string CG CG CG CG CG CG. So I also explained it earlier. It goes zero one two three four and that is five times. It starts at zero and it ends at four and our count is five because four is less than five and five is not less than five. However, what we can also do is start at one and do I is less than or equal than the count and then it will go from one two three four five. However, in the future, when you start using arrays, which I will be teaching in a future episode, this will mess things up because again, I'm going to explain in the future, but basically arrays work based on zero. So it starts like a zero one two three four for a size five array. And again, I'm going to get into that in the future. So anyways guys, I hope that made sense. And if you learned something new today and if you enjoyed this video, make sure you smash that like button, consider subscribing if you're new and turn on the bell for future notifications of videos and live streams. Anyways, I hope you guys have a great one. I'll catch you guys in episode one point five, which will be a very special episode, which is break infinity and that should be fun. Thank you guys for watching. I'll catch you guys in the next one. Peace.