Jump to content
New account registrations are disabed. This website is now an archive. Read more here.
  • 0
Sign in to follow this  
Foxkit

How aliasing works

Question

this is pretty straightforward, I've been having trouble understanding how aliasing in scripting works, I know that it 'changes' the name of a method within a class, how does that help anything if you need that method to work the script at all? wouldn't that make it not possible to update that method instead?

Share this post


Link to post
Share on other sites

8 answers to this question

Recommended Posts

  • 0

Uh, aliasing doesn't change the name it just adds to an old methods with having write the whole method. With alias a method can be easily what could have been a reduction in compatibility with other scripts means edit without having to override it. The original code runs before, after, or between your own code.

 

Something like this:

 
class example
def foo
 draw_something
 draw_more_stuff
 draw_anything_else
end
end

This is the orginial made up code and here's a alias

 

class example
alias ace_foo foo
def foo
 ace_foo #calling old method
 drawing_new_stuff end
end

as you can see here in the second example class I use alias to call the old class without writing the whole thing. Hope that helps

Share this post


Link to post
Share on other sites
  • 0

In a way, what interests me is this from your example.

def foo
 ace_foo
 drawing_new_stuff END
end

 

why do you have that end stuck there? is it supposed to be there?

Share this post


Link to post
Share on other sites
  • 0

alias, does exactly what it sounds (alias being another name for something) alias merely creates a copy of the element that was defined.

 

Consider this:

class Foo
 def bar
puts "Hello, World!"
 end

 alias hello bar
end
end

f = Foo.new
f.bar
# "Hello, world!"
f.hello
# "Hello, world!"

 

As you can see, they called the same method.

 

Traditionally, alias is used to create various ways of calling the same thing like:

class Point
 attr_accessor :x, :y
 def initialize(x, y)
@x = x
@y = y
 end

 def to_s
"#{x}, #{y}
 end

 alias to_str to_s
 alias to_string to_s
end

 

Now, Point can be formatted as a string with #to_s, #to_str or #to_string

 

Now, traditionally, in the RPG Maker XP world; alias is significantly more popular, and much more significant.

 

Since for the most part, a lot of makers put a bunch of scripts together (written by different people). This is fine, but what happens when one script redefines a classes initialize method, and another script does the same thing?

 

Well, only the one later in the script editor is gonna be called, as the method is redefined (overwritten completely).

 

So, how do we get around that?

 

1. copy the old method into a new method.

2. redefine the OLD method

3. call the new method (which is the OLD method)

 

class Scene_Map
 def initialize
# default initialize
 end
end

# I want to do some more stuff with initialize, and share it!

class Scene_Map
 # initialize is copied to my_new_init
 alias my_new_init initialize

 def initialize # initialize is overwritten
# do some new stuff
my_new_init # calls old initialize (from my_new_init
 end
end

 

Now, different programmers code is less likely to interfere; as they aren't overwriting each others work.

 

Note, inheritance doesn't work here because it MUST be Scene_Map (for it to work in the RMXP engine)

 

Hope that makes sense

 

EDIT:

 

@bigace: you have a good demonstration of it's possible usage, but *technically* it doesn't let you add more to the method; that's just one thing you CAN do with it.

 

It just copies the method (conceptually, there are more to the internals, but you can look at it this way, because redefining one of the symbols, does not redefine BOTH symbols).

Share this post


Link to post
Share on other sites
  • 0

Uh, aliasing DOES change the name of a method, hence the keyword being "alias".

 

def method_A
p "Method 1"
end

alias method_A_newName method_A
def method_A

end

 

Here we renamed "method_A" to "method_A_newName". Since we do not call do not use it in the new method_A, the method does absolutely nothing. If we change it to do this:

 

 

def method_A
p "Method 1"
end
alias method_A_newName method_A
def method_A
method_A_newName
p "Method 2"
end

 

It calls the renamed method, and then prints "Method 2".

 

EDIT:

Kell beat me to the post, but yeah. :)

Edited by ForeverZer0

Share this post


Link to post
Share on other sites
  • 0

Lol, you posted within a minute of me tongue.png

 

Or not even a minute really, I went back to the forums after posting and my name wasn't there haha

Share this post


Link to post
Share on other sites
  • 0

In a way, what interests me is this from your example.

def foo
ace_foo
drawing_new_stuff END
end

 

why do you have that end stuck there? is it supposed to be there?

 

That's a typo, the end was supposed to go below

 

@bigace: you have a good demonstration of it's possible usage, but *technically* it doesn't let you add more to the method; that's just one thing you CAN do with it.

 

It just copies the method (conceptually, there are more to the internals, but you can look at it this way, because redefining one of the symbols, does not redefine BOTH symbols).

 

I knew there was more, but as you can see from my first example. I'm bad at explaning or didn't know how to say it and I knew one of you would probably say it better if I miss a chunk.tongue.png

 

Uh, aliasing DOES change the name of a method, hence the keyword being "alias".

 

Ops, I don't even know why I said that.huh.png

Share this post


Link to post
Share on other sites
  • 0
I'm bad at explaning or didn't know how to say it and I knew one of you would probably say it better if I miss a chunk.

 

Hehe, I struggle with plain explanations a lot. I've been trying to practice lately, but sometimes it's easier to just use the VCR than tell someone how it works, if you catch my drift ;)

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...