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.

[TUT] Aliasing with ease.

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
 
Good Tutorial Me(TM) I would like to add this (since everyone told me to)

Here is something you need to remember about alias. When you alias a methods from a hidden class do something like this, to prevent a stack error when F12 is pressed, since they will get aliased again if you press F12.

Code:
  if @new_stack.nil?
    alias new_title_name title_name
    alias new_title_bgm title_bgm
    alias new_start_map_id start_map_id
    alias new_start_x start_x
    alias new_start_y start_y
    alias new_party_members party_members 
    @new_stack = true
  end

As you can see here I wrap the alias listings in an instance variable when It runs through the first time the instance variable is nil so the methods will get aliased, then it sets the instance variable to true (well any object except nil will do)

Not I don't know all the technical details about this, but when you press F12 it starts back at Game_Temp again and runs through all of the classes so you can say it starts over, but hidden classes aren't created again when they hit the alias again its aliased again, but it uses the method you rewritten the first time you aliased it which calls itself and when the method is called in game *POW* stack error.
 
Sadly, though a nice tutorial: The entire first paragraph, is useless, as I managed to get more information from this part:
Meâ„¢":1yln7aat said:
Aliasing will help you to ADD things to methods instead of rewriting them.

Also, though, you might as well add in this:
For C/C++ programmers:
Aliasing is like inline functions. ^_^

Anyways, I also found that aliasing is a bit of a simple subject to spend so much focus on, but these little tutorials get done, and getting done always helps *slaps self for not finishing anything*
 
Nice tutorial ^_^ . For beginners, so I didn't learn anythin from it :p .

Anyways, I wanted to ask: what are aliases used for in the real Ruby? Because it seems unnecessary to alias your own code... (Except if you're using a framework or something maybe?)
 
There are default classes in ruby to, file loads and requires, you could have team prokects, own addition on a earlier script (an optional addition). I think you can think of many reasons ;)
 
It helped me a lot, but I still don't understand what to DO with the alias once it is created. I did exactly what you said, and nothing was accomplished. You also failed to tell the viewer how to make it. I already knew though, overall, nice tutorial! :thumb:
 
Despite what the others said about this tutorial, I, a complete noob in Ruby's scripting environment, found this tutorial very useful!! Thumbs up for Me(tm)!
At last a understandable alias tutorial... :yes:
 

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