Submit Your Article


 
RPG Maker

Welcome Guest ( Log In | Register )


  Games Resources RPG Maker VX RPG Maker XP Scripts Tutorials Downloads

 
Reply to this topicStart new topic
> [Scripting][Series]The Scripter’s Journey Series, 5. Methods!
The Law G14
post Jul 21 2012, 09:15 AM
Post #1


Scripter FTW
Group Icon

Group: Local Mod
Posts: 1,348
Type: Scripter
RM Skill: Skilled
Rev Points: 5




The Scripter’s Journey Series

5. Methods!


Table of Contents
I. Methods and Functions!
II. Kernel and the Built-in-“Functions”…(oooh..!)
III. Method Construction!
IV. Returning Values and Naming Conventions!
V. Arguments and Parameters!
VI. Conclusion!


Methods and Functions!
Today were going to be covering methods (functions)! What are these you ask? Well, let’s get a quote all the way from lesson 1 “To put it simply an object is an entity or a data structure (also known as a class) that has certain properties (variables) and methods (functions)”. First of all, Objects and Classes will be discussed much more extensively next chapter, but you should already have a very basic sketch in your mind on what they are based on the last four tutorials. Everything is well...an object! And if you’d like to relate this to language, an object’s properties (or variables) would be the nouns, and the objects “actions” or “methods” would be the verbs (Don’t worry if you don’t get what I just said lol, just understand that methods are like “actions” for objects and allow them to “do” things). Now, what’s the difference between methods and functions? There really isn’t that much of one, all methods are functions, it’s just that methods are functions that “belong” to an object or class or are “made” inside of a class. However, since like I said earlier, everything is an Object and nearly everything is contained in a class here in this RGSSX Scripting Language, we generally call everything a “method” and don’t usually make such a hassle over the differences between methods and functions. But, there are some methods that act very much like functions would in other programs, and the Kernel Module is responsible for these methods in RGSSX.


Kernel and the Built-in-“Functions”…(oooh..!)
Sidenote: This little section isn’t too important, so if you don’t get it or just feel like skipping it, it’s okay, I promise it will make more sense within the next two tutorials!

Let’s go over functions (and their difference with methods) one last time. To make it more clear, you can think of functions as global methods, or methods that can be used anywhere in the program. Methods can only be used by the class or object they “belong” to. Now, with that said, RGSSX does not come with any “natural” functions, but methods that are created in a Module (we will go over what modules are in two lessons, don’t worry about them too much for now) which can be accessed anywhere.

Go in your script editor and at the bottom right click “Help”, then go to “Index” at the top left corner and scroll down to “Object” and double-click it. At the top, it should read something like this “The superclass of all classes. Defines the general behavior of objects”, I’m getting a bit ahead of myself, but I just wanted to show you guys that everything is literally an Object, everything comes from this class. Also, I’d like to show you that below this sentence is something like “Included Modules” and “Kernel” is under that. Now, when a module is included in a class, all its methods and variables can be used in the class as long as everything that branches from or inherits from that class (In RGSSX everything inherits from the Object class). Let’s click on Kernel and it should basically explain how Kernel houses several methods and also click on the “built-in-functions” link to see what methods these are.

Now let’s put everything together. Everything is an Object and everything inherits from the Object class, and the Object class “includes” a module called “Kernel” which houses several methods. When a module is “included” in a class, that class and all the classes that inherit from it or come from it have those module’s methods and variables. Do you understand what this means? That means the Kernel module essentially created functions by making these methods “global” and accessible everywhere. If you didn’t notice, our good friend, the “print” method is inside this Kernel Module which is why we use that method anywhere we want! To summarize, here’s a quote from the Help File in the “built-in-functions” page, “Strictly speaking, they aren't functions in Ruby, but since methods defined in the Kernel module can be called from anywhere, they can be used like functions are in other languages.” Hope that was some nice information for you to learn!


Method Construction!
So, from here on out, I’ll not be using the word “function” because like I already explained, RGSSX uses pretty much only “methods” so that’s the term we’ll be sticking with. Alright, let’s get to the meat of this tutorial.

Open up your Script Editor and make a new script page and call it “Methods Tutorial”. Methods have a pretty simple outline:

CODE
def method_name(parameters, parameters, etc.)
method_body
end


Let’s go over this code. Every method needs to start out with the “def” keyword, which you can think of as “define”. After this keyword you have the method name which should always be lowercase, however I’ll go more into naming conventions in the next section. Finally, contained within parenthesis we have the method’s parameters. We’ll be going over parameters and arguments in the section “Arguments versus Parameters”, for now just know that having parameters in your method construction is completely optional, but when you do make them they must be contained in parenthesis and separated by commas.

The method body is simply the block of code that this method “defines” as its own (blocks in their most simple definition are just a group of related lines of code, but there is a lot to talk about beyond that which is why an Advanced Tutorial will be dedicated to them, for now, just think of them as groups of related code lines). Basically, in the method body, you “define” what happens when this method is called or invoked.

Finally at the end of every method (and nearly everything actually like “if” statements, for loops, etc.) you put the “end” keyword. Let’s put this all together and make a simple program:

CODE
def print_eight
# Remember to comment your code ! This line “prints” the number 8
print 8
end


You should understand what this does, just a simple method that when called prints the number 8. Now one last thing to cover in this section which I may not have made clear; methods do not “activate” or “run” until they are “called” or “invoked” (like I’ve been saying for a little bit now). This is a difference between “method construction” where you tell the method what to do when it’s called and “method activation” which is where it runs the block of code you defined for it. It’s as simple as writing down the method name:

CODE
def print_eight
# Remember to comment your code ! This line “prints” the number 8
print 8
# “Call the ‘print_eight’ method
if 1 == 1
print_eight
end
end


Side-note: There is more to “method activation” that involves Classes and objects, but that would go out of the scope of this tutorial. So for now just know this simple way of calling methods.


Returning Values and Naming Conventions
There is a keyword known as “return” which adds several very useful components to method construction. Firstly, through “return” you can treat a method as a variable, so instead of printing information or doing everything within the block of the method, you can return the information calculated or resolved in the method and send it back to wherever it was called. Let’s use our original method, and let’s name it instead “return_eight”:

CODE
def return_eight
return 8
end
# Call the ‘return_eight’ method
if 1 == 1
print 8 + return_eight
end


The output for this simple program would be “16” because 8 plus 8 is 16. See what happened is like last time, we did our method construction except instead of “print 8” we had “return 8” so were telling our method “return this following value” making our method much like a variable in which it is equal to the value ‘8’, get it? So now when we do method activation for return_eight we receive back the number 8 which we can now use in our overall program. With that, I added the returned 8 to another 8 and printed that value to the screen to get 16.

You can have several return statements in your method construction. Keep in mind though that the first one encountered will be the only one used. This can be used to have differing options. You can also just have return without a value next to it, which acts as a way of exiting the method.

CODE
def monkeys
for i in 0..8
return if i == 5
print i
end
end


Last thing to note on returning (credits to PandoraShock/Azrith001 for teaching me this), RGSSX automatically returns the last item to be returned, so even if you don’t explicitly return something, RGSSX will do it for you. However, it’s best to try to have a “return” explicitly stated at the end of your method construction if you don’t have one in your method already to reduce lag (Lag is a topic that you don’t really need to worry about until you’re a more advanced scripter but it’s best to get into good habits early). So an example would be the following:

CODE
def fun
lines_of_code
lines_of_code
lines_of_code
lines_of_code
return
end


Now on to some naming conventions that you should abide by. First of all, always keep your method names lowercase and no spaces in between words.

CODE
# Bad
def MONKEYS
def MON KEYS
# Good
def monkeys
def mon_keys


Next, it is common practice to add a “?” mark at the end of your method name to notify that the method “returns” a value that is usually a Boolean (true or false). You can also add a “!” mark at the end of your method name to signal that it is a “dangerous” method (we’ll get into this more when we discuss objects and Classes). Regardless, these are just some common practices and you don’t have to do them. The first two rules though (lowercase and no spaces) are a must.


Arguments and Parameters!
We discussed earlier that in method construction, you have the option of adding parameters after writing the method name. Well basically to keep it simple, parameters allow you to “add in” variables from outside the method into the body of the method.

CODE
def print_number(number)
print number
end
# Call method
x = 49
print_number(x)


The output of this program would be 49. First, in method construction if you want to add a parameter, you first make parenthesis and for each parameter you would like to have, you separate it by commas. You can think of these parameters that are stated here as variable names that will reference to the values you pass in later in the program (in this case, the x or 49). Once you write the parameter name, you can now use that variable in your method body, like in the example (print number). Once the method is done though, this variable can no longer be used, in short, parameters can only be used in method construction (unless you “return” that parameter). Now later, in the simple program we created above, we did method activation or we called our method, but now we had something in parenthesis besides it, this is called the argument. (Normally, ‘argument’ and ‘parameter’ are used interchangeably, but for the sake of this tutorial, I’m trying to be specific to help you guys better understand). By supplying the argument in parenthesis when you call the method, the parameter you defined at method construction now is a variable that equals or references to the argument you supplied.

CODE
method_construction
method_activation
parameter = argument


(Hope this has made sense so far, if not feel free to ask questions!) Now you can also allow a parameter to have a default value meaning you do not need to supply an argument.

CODE
def print_number(number = 4)
print number
end

print_number[/code]

Because I provided a default value, it was optional for me to add in arguments (if I don’t provide arguments though it will always print 4). Remember back in our Variables tutorial, making variables was done like this:

CODE
variable_name = value


See the similarities? The parameter is the variable name and the argument is the value!


Conclusion
Well that was a pretty long tutorial, hope you were able to catch everything, and if not, don’t be afraid to reread a section or the whole tutorial. We learned that methods allow us to call blocks of organized code that can return values and accept arguments for parameters. We also made a difference between methods and functions, and arguments and parameters. Next time we finally will learn about “Classes”, be ready! (Also, try looking up some methods in the default scripts, they’re everywhere and you’ll see all we learned here displayed over there!)

The order of how these tutorials will progress: http://www.rpgrevolution.com/forums/index....showtopic=54953

~Law


__________________________

To put in sig, copy this link:
CODE
[url="http://www.rpgrevolution.com/forums/index.php?showtopic=51540"][img]http://img40.imageshack.us/img40/6504/conceptthelawbanner.png[/img][/url]


Sig Stuff


"When you first come, no one knows you. When help them out, they all know you. When you leave, they all love you. When you come back, they've already forgotten you." -- copy into your sig if you think this quote speaks true!

If you are one of the very few teenagers that know what real rap is and don't blindly listen to the hate statements (rap is crap), then put this in your sig. I say this in the name of Common, Mos Def, Lupe Fiasco, 2Pac, Nas, Talib Kweli, Eminem, and many others. -Exiled One

My Project Thread: Gai's Hunters


Go to the top of the page
 
+Quote Post
   

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 19th June 2013 - 08:25 AM
RPG RPG Revolution is an Privacy Policy and Legal
eXTReMe Tracker