Submit Your Article


 
RPG Maker

Welcome Guest ( Log In | Register )


  Games Resources RPG Maker VX RPG Maker XP Scripts Tutorials Downloads

 
Closed TopicStart new topic
> [Scripting]Alias - What Is It & How To Ver.1.0, A guide on aliasing.
YanXie
post May 3 2008, 12:53 AM
Post #1


Because Tomorrow Will Surely Come...
Group Icon

Group: Revolutionary
Posts: 1,137
Type: None
RM Skill: Skilled




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

What is alias?

An object that created as a pseudo name 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 great inconvenience, 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's 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 that 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 a 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 it's 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 a stack error, like in some scripts 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 from happening.


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

Special Thanks:

Trickster : Stack error problem fixes.

me™ : Reference for his aliasing tutorial.

With that said,

cheers,puppeto4. :)


__________________________
how make teleport to graveyard then your character die?

AWAY FOR VACATION.
NOT HERE UNTIL JAN/FEB 2010 -w-/
Go to the top of the page
 
+Quote Post
   
woratana
post May 11 2008, 03:08 AM
Post #2


Looking for scripter to hire? PM me *O*
Group Icon

Group: +Gold Member
Posts: 1,038
Type: Scripter
RM Skill: Undisclosed




Nice tutorial smile.gif I'd been waiting for this to be approved a long time~^^

By the way, for the stack problem, you can also do this:
CODE
alias wora_lol_old lol unless $@

or...
CODE
alias wora_lol_old lol unless method_defined?('wora_lol_old')


For many lines of alias:
CODE
unless $@
alias wora_lol_old lol
alias wora_lolol_old lolol
alias wora_pwn_old pwn
end

smile.gif


__________________________
Check out my NEW blog!!!



MVP (Most Valuable Poster) Award 2008


Go to the top of the page
 
+Quote Post
   
YanXie
post May 11 2008, 03:10 AM
Post #3


Because Tomorrow Will Surely Come...
Group Icon

Group: Revolutionary
Posts: 1,137
Type: None
RM Skill: Skilled




Yeah,apparently I still didn't encounter any stack error with VX script though(Checked more than 50 VX script for that).

Most likely it only happen to RGSS only.

EDIT : Nope,it happen to VX too...someone need to write F12 fix script for VX nao sad.gif


__________________________
how make teleport to graveyard then your character die?

AWAY FOR VACATION.
NOT HERE UNTIL JAN/FEB 2010 -w-/
Go to the top of the page
 
+Quote Post
   
woratana
post May 11 2008, 11:15 AM
Post #4


Looking for scripter to hire? PM me *O*
Group Icon

Group: +Gold Member
Posts: 1,038
Type: Scripter
RM Skill: Undisclosed




Yeah, it happens to VX too. >_>'

Check in my mouse system, or Shun's input script.
It will get stack error when you remove 'unless $@' from the script, and press F12. smile.gif

Anyway, I'm not sure how to stop [F12] TwT'


__________________________
Check out my NEW blog!!!



MVP (Most Valuable Poster) Award 2008


Go to the top of the page
 
+Quote Post
   
YanXie
post May 11 2008, 04:34 PM
Post #5


Because Tomorrow Will Surely Come...
Group Icon

Group: Revolutionary
Posts: 1,137
Type: None
RM Skill: Skilled




I tried XP version of F12 Fix script,but it didn't work >_>


__________________________
how make teleport to graveyard then your character die?

AWAY FOR VACATION.
NOT HERE UNTIL JAN/FEB 2010 -w-/
Go to the top of the page
 
+Quote Post
   
woratana
post May 11 2008, 04:54 PM
Post #6


Looking for scripter to hire? PM me *O*
Group Icon

Group: +Gold Member
Posts: 1,038
Type: Scripter
RM Skill: Undisclosed




It didn't work if you leave nothing in rescue Reset TwT

try put something, like:
CODE
rescue Reset
p 'Don't press F12!!!!!!!!!!!!!'
end

smile.gif


__________________________
Check out my NEW blog!!!



MVP (Most Valuable Poster) Award 2008


Go to the top of the page
 
+Quote Post
   
cometstar
post Oct 8 2012, 01:24 PM
Post #7


Level 5
Group Icon

Group: Member
Posts: 60
Type: None
RM Skill: Undisclosed




Thank you for this topic
I has seen alias in many scripts I got ,but I didn't understand why is there
and now I know its work and can use it in my script ,where it's very useful and spares time.

This is a huge necropost. Please look at the date before you post. Thanks ~Jonnie19
Go to the top of the page
 
+Quote Post
   

Closed 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 - 09:19 PM
RPG RPG Revolution is an Privacy Policy and Legal
eXTReMe Tracker