Author: Meâ„¢
Version: 1.1
This Mini-Tutorial will cover the wonderfull power of aliasing. I am writing this tutorial because I think this is one of those small easy things wich everyone keeps asking. Now, enough talk. Lets explain this already:
An object that consists of a reference to another object. An alias saves space, since the alias object is small, and can be used to reference very large objects. Resolving an alias refers to retrieving the object that the alias references. Now, what does this mean: In RMXP and RUBY/RGSS you can rewrite methods. At start-up the scripts are being stored (names and method names) and will be stored form the very first one in the list till the very last in the list. Aliasing will help you to ADD things to methods instead of rewriting them.
Let's start with this little script example. You can put it into a new game, or just read on what will happen.
Now, I just wrote the MyFirstScript class. If call this script using this syntax: MyFirstScript.new, a pop-up will appear with Hello world. If you press ok, another one appears with This is a Test Script.
Oké, we put in a new page below this one with this script in it:
Let's call this script again using the .new syntax. One single Pop-Up with Roxors will appear. Congrats, you jsut rewrote the initialize method. Now, if you want to prevent this, why will need to get started with aliasing!
Conclusion: If you twice name a method in a clase the same, it will rewrite that method instead of combining/adding something to it
Code:
[color=blue]class[/color] [color=darkblue]MyFirstScript[/color]
[color=blue]def[/color] [color=darkblue]initialize[/color]
p [color=purple]"Hello world"[/color]
do_message
[color=blue]end[/color]
[color=blue]def[/color] [color=darkblue]do_message[/color]
p [color=purple]"This is a test script"[/color]
[color=blue]end[/color]
[color=blue]end[/color]
Oké, we put in a new page below this one with this script in it:
Code:
[color=blue]class[/color] [color=darkblue]MyFirstScript[/color]
[color=blue]def[/color] [color=darkblue]initialize[/color]
p [color=purple]"Roxors"[/color]
[color=blue]end[/color]
[color=blue]end[/color]
Conclusion: If you twice name a method in a clase the same, it will rewrite that method instead of combining/adding something to it
Oké, so we want the following thing to happen: We want 4 pop-ups to pop up:
Yay, it works == Hello world == this is my first script == roxors.
We could do this by doing the folowing:
Now, you probably understand that this is not what we wanted. we REWROTE the init method, instead of ADDING code to it. So Let's Alias!
Whoaha, lets see what we just did!.
1: class MyFirstScript
2: alias the_old_init initialize
3: def initialize
4: p "Yay, it works"
5: the_old_init
6: p "Roxors"
7: end
8: end
Line 2. Here we created the alias. The name of the alias is the_old_init and the reference object (the method to alias) is called initialize.
Line 4. Here we made a print object. We wanted to pop-up yay ot works as first.
Line 5. So we called the alias: the old init. To include the original code, you simply put in the alias name, like it was a method name, wich it is in fact. At line five the originial content of def initialize is included.
Line 6. Another Print Object, this time with roxors.
Yay, you just made your first alias. :lol:
Yay, it works == Hello world == this is my first script == roxors.
We could do this by doing the folowing:
Code:
[color=blue]class[/color] [color=darkblue]MyFirstScript[/color]
[color=blue]def[/color] [color=darkblue]initialize[/color]
p [color=purple]"Yay, it works"[/color]
p [color=purple]"Hello world"[/color]
p [color=purple]"This is my first script"[/color]
p [color=purple]"Roxors"[/color]
[color=blue]end[/color]
[color=blue]end[/color]
Code:
[color=blue]class[/color] [color=darkblue]MyFirstScript[/color]
[color=blue]alias[/color] the_old_init initialize
[color=blue]def[/color] [color=darkblue]initialize[/color]
p [color=purple]"Yay, it works"[/color]
the_old_init
p [color=purple]"Roxors"[/color]
[color=blue]end[/color]
[color=blue]end[/color]
Whoaha, lets see what we just did!.
1: class MyFirstScript
2: alias the_old_init initialize
3: def initialize
4: p "Yay, it works"
5: the_old_init
6: p "Roxors"
7: end
8: end
Line 2. Here we created the alias. The name of the alias is the_old_init and the reference object (the method to alias) is called initialize.
Line 4. Here we made a print object. We wanted to pop-up yay ot works as first.
Line 5. So we called the alias: the old init. To include the original code, you simply put in the alias name, like it was a method name, wich it is in fact. At line five the originial content of def initialize is included.
Line 6. Another Print Object, this time with roxors.
Yay, you just made your first alias. :lol:
- Alias
Code:alias aliasname method_reference_name def method_reference_name # Do FOR org. code aliasname # Do AFTER org. code end
- print
Code:p value print(value)
Questions and feedback are appreciated :D