Using a method in a script
Methods are where the action is and where the tasks are performed. Great, that's really nice to know but what is a method?
What is a method?
When we write a script, we are making lines of code that the computer is going to execute, one line at a time. As we write our code, there will be things we want our game to execute more than once. For example, we can write a code that adds two numbers. Suppose our game needs to add the two numbers together a hundred different times during the game. So you say, "Wow, I have to write the same code a hundred times that adds two numbers together. There has to be a better way."
Let a method take away your typing pain. You just have to write the code to add two numbers once, and then give this chunk of code a name, such as AddTwoNumbers()
. Now, every time our game needs to add two numbers, don't write the code over and over, just call the AddTwoNumbers()
method.