Can you overwrite an aliased method?

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,204
First Language
Binary
Primarily Uses
RMMZ
is it possible to overwrite an alias ?

for example...

class Test_Class  # // Used to print data when intialized  def initialize    method  end   def method    p "Initial Method"  end   alias :alias_method :method  def method    alias_method    p "Aliased Method"  end endclass Test_Class   def alias_method    p "Overwritten Alias Method"  end endTest_Class.new 
This is the exact code I tried. It does as I intended and overwrites the aliased method; however, it doesnt recall the original method, which it should.

Any ideas?
 

Mouser

Veteran
Veteran
Joined
Aug 19, 2012
Messages
1,245
Reaction score
264
First Language
English
Primarily Uses
Why not just re-alias the original method?
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,204
First Language
Binary
Primarily Uses
RMMZ
cause i have to overwrite the alias that has already been created. :(

It was simply to avoid having to give explicit instructions on where to go in the script (to delete old alias) as my modifications dont allow for both alias to function.
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
Well because clearly, the alias now calls the overwrite method... and the overwrite method clearly doesn't call the original one since the overwrite method only has the p line...


but then again if you do this, your new script will still need the old one since it overwrites the alias... but also because of that, I think it's safe to just call the original method from the overwrite...
 
Last edited by a moderator:

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,204
First Language
Binary
Primarily Uses
RMMZ
Nah, when you call the orig method it causes a stack error as it just starts looping.

I tried that already ^_^

Edit:

the only way I can think of, is by basically copying all the code from the orig method, but this completely defeats the purpose of what I was trying to achieve.

I feel this could be a question for its own thread...
 
Last edited by a moderator:

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
Now that I think of it, yeah it will cause an infinite loop since when you call the original method it calls the alias, which then invokes the overwrite method which calls the original method...
 

Mouser

Veteran
Veteran
Joined
Aug 19, 2012
Messages
1,245
Reaction score
264
First Language
English
Primarily Uses
This is the 'dark side' of aliasing that none of the books or tutorials tell you about while they're touting how great aliasing is...

It can be a useful tool, but artifact code is a very real potential pitfall.
 
Last edited by a moderator:

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,204
First Language
Binary
Primarily Uses
RMMZ
lol the 'dark side' of aliasing...

I just pictured a darth maul looking mother funker writing code whilst doing the 'classic' evil laugh...
 

Mouser

Veteran
Veteran
Joined
Aug 19, 2012
Messages
1,245
Reaction score
264
First Language
English
Primarily Uses
lol the 'dark side' of aliasing...

I just pictured a darth maul looking mother funker writing code whilst doing the 'classic' evil laugh...
I've had that picture (or one like it) more than once when I had to troubleshoot other people's code. People who thought writing in spaghetti logic would give them job security, or that crap the 'visual' code generators create when people do the drawing arrows between methods and classes to 'connect' their code.

There is no "best" computer language (though C is pretty darn close). They all have their strengths and foibles. Aliasing is a very peculiar control structure. Personally, I don't use them in my code, for exactly the reason Dekita is up against: there's always a chance some method or class somewhere will call the original method instead of yours.

That is the _WORST_ kind of bug to try and track down, because everything seems to work, but you know something is off somewhere - either numbers don't add up right, or the placement of something is off - and it generally happens somewhere far away from the actual cause of the logic error.
 
Last edited by a moderator:

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,204
First Language
Binary
Primarily Uses
RMMZ
Yea I tend to alias as little as possible as well. Some methods I alias because i know other scripters like to rewrite the method but mostly I alias my own methods. This way I am able to keep a mental note of all the methods i've aliased.

My most aliased method is probably apply_variance from Game_Battler.

Anyway, this seems to have completely strayed from the point at hand, which is now irrelevant (for me - at least for now).

I wonder if it can be done though. Like, without having to recreate the original method in your new code...
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
is it possible to overwrite an alias ?

for example...

[..]

This is the exact code I tried. It does as I intended and overwrites the aliased method; however, it doesnt recall the original method, which it should.

Any ideas?
I don't understand the problem, nor do I understand what you are trying to accomplish, or why you feel that it "should" recall the original method.

Have you tried calling `alias_method` directly without modifying it?

You presented a problem, but you don't explain what you want to do, or what the output should be, or why you need to do it this way. For all that matters, your approach may be wrong, and given the circumstances, that is probably the case.

`alias_method` is the original method.

`method` has already been changed.

If you change `alias_method` then you've effectively lost the definition that prints out "Initial method"

I don't see anything wrong with the alias method in this example.

What you're doing is not much different (behavior-wise) from this

Code:
def method  p "Initial Method"end def alias_method  p "Initial Method" # or whatever `method` currently doesenddef method  alias_method  p "Aliased Method"enddef alias_method  p "Overwritten Alias Method"end
Can you point out why this *should* print out "Initial method"?
 
Last edited by a moderator:

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,204
First Language
Binary
Primarily Uses
RMMZ
I wrote a small script to highlight why I was attempting this..

#===============================================================================class Stuffz#===============================================================================  #-----------------------------------------------------------------------------  # // Creates initial data  #-----------------------------------------------------------------------------  def initialize    @data     = []    @new_data = []  end  #-----------------------------------------------------------------------------  # Creates initial method 'data' which much be kept.  #-----------------------------------------------------------------------------  def data    @data  endend#===============================================================================class Stuffz#===============================================================================  #-----------------------------------------------------------------------------  # // Creates alias method I want to change.  # // I wish to keep the initial data, and replace the @new_data array with  # // another array...  #-----------------------------------------------------------------------------  alias :alias_data :data  def data    alias_data + @new_data  end  end#===============================================================================class Stuffz#===============================================================================  #-----------------------------------------------------------------------------  # // Creates @more_data array to use in place of @new_data  #-----------------------------------------------------------------------------  alias :init_stuffz :initialize  def initialize    init_stuffz    @more_data = []  end  #-----------------------------------------------------------------------------  # // Attempt at overwriting the aliased data, to replace with my own methods.  # // The initial data method must be retained.  #-----------------------------------------------------------------------------  def alias_data    data + @more_data  end  end
Keep in mind, I was trying to do this without simply overwriting the data method -
and using the code from the initial method before my aliased code.
 It wasnt really anything important, I was just wondering if it would be possible. :)
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
have you tried to alias the alias instead?

Code:
class Test_Class  # // Used to print data when intialized  def initialize    method  end   def method    p "Initial Method"  end   alias :alias_method :method  def method    alias_method    p "Aliased Method"  end endclass Test_Class    alias alias_method2 alias_method  def alias_method    alias_method2    p "Overwritten Alias Method"  end endTest_Class.new
or alias the original method again

Code:
class Test_Class  # // Used to print data when intialized  def initialize    method  end   def method    p "Initial Method"  end   alias :alias_method :method  def method    alias_method    p "Aliased Method"  end endclass Test_Class    alias alias_method2 method  def method    alias_method2    p "Overwritten Alias Method"  end endTest_Class.new
PS: IDK if that makes a difference though, probably not.
 
Last edited by a moderator:

Zeriab

Huggins!
Veteran
Joined
Mar 20, 2012
Messages
1,268
Reaction score
1,422
First Language
English
Primarily Uses
RMXP
is it possible to overwrite an alias ?

for example...

class Test_Class  # // Used to print data when intialized  def initialize    method  end   def method    p "Initial Method"  end   alias :alias_method :method  def method    alias_method    p "Aliased Method"  end endclass Test_Class   def alias_method    p "Overwritten Alias Method"  end endTest_Class.new This is the exact code I tried. It does as I intended and overwrites the aliased method; however, it doesnt recall the original method, which it should.

Any ideas?
I like how you ask whether it is possible to overwrite an alias just before you show us an example that does exactly that XD

Some of the confusion seems to come from what it means to alias a method. You misunderstood Tsukihime so I will do some rephrasing. Let us look at this bit of code

  alias :alias_method :methodConceptually this means that test_class.method can also be called test_class.alias_method. From a technical point of view, consider test_class.method as pointing to a specific method. (Heck, just try paying around with object.methods)Then you are just saying that test_class.alias_method also points to the same class. If it helps consider that it kind of works like this.

# We define our original methodtest_class[:method] = OriginalMethod# We create an alias, an extra name pointing to the original method.test_class[:alias_method] = test_class[:method]# Now we want to create a new methodtest_class[:method] = NewFancyMethod   # Calls test_class[:alias_method]# Yup, we still have test_class[:alias_method] == OriginalMethod# Now you do thistest_class[:alias_method] = AMethodToReplaceNewFancyMethod# Yup, not really what you wanted. Yet the aliased method is definitely overwrittenStructure-wise your code has a bad smell. Mouser already talked about this.Nevertheless, you can the code so that it works. How so? Consider what I have told you. What must you change?

I suggest you also analyze your code. Is this the correct way of expression the intention? Is the bad design needed for hotfixing old code?
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,204
First Language
Binary
Primarily Uses
RMMZ
@Zeriab,

It would seem that you are suggesting a better way of handling it would be to simply create a new 'copy' of the initial data which would be called within the method overwrite.

Maybe i misunderstood though :/

Also, badly designed code should be avoided wherever possible and code optimization and efficiency are always top priority.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
Please remember this thread is for script questions that don't deserve their own thread - ie, quick and easy to answer. If it's something difficult or involved, please start a new thread where there can be in-depth discussion without the confusion of half a dozen different conversations happening at the same place.


Also, this is not the place to ask "Is there a script that will do ..." questions. We have several Script Requests forums for that :)
 

Zeriab

Huggins!
Veteran
Joined
Mar 20, 2012
Messages
1,268
Reaction score
1,422
First Language
English
Primarily Uses
RMXP
Also, badly designed code should be avoided wherever possible and code optimization and efficiency are always top priority.
It is a good dream and can serve as a useful heuristics, but at the same time it is impractical to fully implement.

@Zeriab,

It would seem that you are suggesting a better way of handling it would be to simply create a new 'copy' of the initial data which would be called within the method overwrite.

Maybe i misunderstood though :/
I'm trying to make you understand aliasing. Why indeed does your code fail.

Read my previous post again and then consider the following code:

class Stuffz  def data    alias_data + @more_data  endend
Does this help you understand the problem? What impact would that have for your code?

@Shaz:

Do you mean I cannot try to teach? ;_;

The answer is a simple one in this case. Simple answers can still be difficult, true.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
I'm thinking more of the questions that require a couple of pages to answer ;)
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,204
First Language
Binary
Primarily Uses
RMMZ
class Stuffz  def data    alias_data + @more_data  endendDoes this help you understand the problem? What impact would that have for your code?
That would cause errors in this particular case due to the @new_data. For example..

    @new_data = $data_items    @more_data = $data_armorsYou seem to be saying that I should simply add the arrays together while keeping the other aliased data. This could be done, very easily. It seemed too simple at the time, but on reflection, it would have been very easy to do this and simply change the other pieces of code to match.

The reason I didn't do that is because, at the time, I felt that the @new_data array shouldnt be kept due it causing conflicts with @more_data array.

Maybe I was just over-complicating it to start with :/
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
Here's what I think is the point:

If this is what `data` looks like right now

def data alias_data + @new_dataendwhere alias_data refers to the old* data method, and you want to make it look like this

Code:
def data  alias_data + @more_dataend
What do you do?*old means everything that was in it before you aliased it
 
Last edited by a moderator:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

so hopefully tomorrow i get to go home from the hospital i've been here for 5 days already and it's driving me mad. I miss my family like crazy but at least I get to use my own toiletries and my own clothes. My mom is coming to visit soon i can't wait to see her cause i miss her the most. :kaojoy:
Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD

Forum statistics

Threads
105,868
Messages
1,017,078
Members
137,580
Latest member
Snavi
Top