Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

HBGames

Tutorial: Aliasing
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.

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]
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:

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]
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

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:

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]
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!

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

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top