Submit Your Article Guild Wars 2 Forum RPG Maker VX.com
 
RPG Maker
 

 Username:
 Password:
   Not a member? Register!



Home > Tutorials > Ruby Game Scripting System > Alias and YOU

Alias and YOU


[RGSS/2] Alias - What Is It & How To?

What is alias ?

An object that created as a pseudoname of the aliased method(at least that what I can understand).By using alias method,you can cut down the numbers of line of the code that you write,since you don't need to write the entire method again for adding a new variables,constants etc.But,you can't alias constants(variables,classes and modules),local variables and variables.You can only alias existing method or global variables,but that is more than enough.Usually,people who are new to scripting and didn't know the use of alias method will :
a) Edit a default script directly.
b)Create the same method and add things to the method,thus rewriting the default script.

Both of them aren't really recommended,since it might cause errors for undefined method,especially the second one.While for the first one,it is a greatly unconvinience,especially if you want to distribute the script,since you'll need to point out what to change on default script,rather than copy pasting the written script in one section(plug n play).

And by using aliasing,it will help you to add things to the method instead of rewriting them.

Why aliasing?
Let's start with this little script example. You can put it into a new game, or just read on what will happen.

Code:
class Scene_Map < Scene_Base
#--------------------------------------------------------------------------
# * Start processing
#--------------------------------------------------------------------------
def start
super
$game_map.refresh
@spriteset = Spriteset_Map.new
@message_window = Window_Message.new
@hud_window = Window_HUD.new
end


See the "@hud_window = Window_HUD.new" ? I add it into method "start" in "Scene_Map"

Then,after that,I use someone else script,which also didn't use alias method :

Code:
class Scene_Map < Scene_Base
#--------------------------------------------------------------------------
# * Start processing
#--------------------------------------------------------------------------
def start
super
$game_map.refresh
@spriteset = Spriteset_Map.new
@message_window = Window_Message.new
@time_window = Window_Time.new
end


In this script,"@time_window = Window_Time.new" has been added into the "start" method of the "Scene_Map"

If I were to put the second script below the first script in the script editor,the second script will overwrite the first one and thus my "@hud_window" code isn't read by the game interpreter.Isn't it's a bad thing?

To put it simply,aliasing increase the compatibility between scripts,so there won't be frequent error pop up when you use certain script.

How to?

Adding code to the methods :

First,look at default Scene_Map,line 27-41 :

Code:
#--------------------------------------------------------------------------
# * Termination Processing
#--------------------------------------------------------------------------
def terminate
super
if $scene.is_a?(Scene_Battle) # If switching to battle screen
@spriteset.dispose_characters # Hide characters for background creation
end
snapshot_for_background
@spriteset.dispose
@message_window.dispose
if $scene.is_a?(Scene_Battle) # If switching to battle screen
perform_battle_transition # Execute pre-battle transition
end
end


Now,I want to add this to the code above :

Code:
@hud_window.dispose


Without using aliasing method,it will be something like this :

Code:
#--------------------------------------------------------------------------
# * Termination Processing
#--------------------------------------------------------------------------
def terminate
super
if $scene.is_a?(Scene_Battle) # If switching to battle screen
@spriteset.dispose_characters # Hide characters for background creation
end
snapshot_for_background
@spriteset.dispose
@message_window.dispose
if $scene.is_a?(Scene_Battle) # If switching to battle screen
perform_battle_transition # Execute pre-battle transition
end
@hud_window.dispose
end


thus,rewriting default script's method and adding unnecessary long lines of code.

Now,we'll try to use alias method:

Code:
#--------------------------------------------------------------------------
# * Termination Processing
#--------------------------------------------------------------------------
alias puppet_hud_terminate terminate
def terminate
# The usual.
puppet_hud_terminate
# The new code.
@hud_window.dispose
end


See?By using aliasing method,the code lines will decrease significantly.


Now,you 'll ask me,how to use alias method right?

Well,the format is like this :

Code:
alias new_name old_name

or

alias_method :new_name, :old_name


Like in the example above:

a) new_name = puppet_hud_terminate
B) old_name = terminate

Simple isn't it?

Stack Error Issues

Apparently,when you alias a hidden class,like Bitmap,for example,if you press "F12" there will be stack error on the interpreter.

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 it will cause stack error,like in some script that I used before.To prevent that,list your alias like this :

Code:
alias puppet_bank_terminate terminate # Alias terminate method
alias puppet_bank_update_basic update_basic # Alias update_basic method
alias puppet_bank_update update # Alias update method


And change it to this :

Code:
if @new_stack.nil?
alias puppet_bank_terminate terminate # Alias terminate method
alias puppet_bank_update_basic update_basic # Alias update_basic method
alias puppet_bank_update update # Alias update method
@new_stack = true
end


This will prevent the stack error to happen.


Well,that's pretty much the tutorial is for now.

Special Thanks:

Trickster : Stack error problem fixes.

me(tm) : Reference for his aliasing tutorial.

With that said,

cheers,puppeto4. :) 
Details
Tutorial: Alias and YOU
Date Listed: 2008-06-08
Author: puppet04
Total Hits: 3119


Embed
Short URL:

HTML:

BB Code:



RPG RPG Revolution
RPG RPG Revolution is your #1 stop for game development and console RPG games, as well as those created by people like you. Link to us to support us, so we may grow to be better website community for you.

RPG RPG Revolution is an Privacy Policy and Legal