how to check if a method has been aliased?
#1
What is the proper code to avoid that pressing F12 in rmxp an aliased method is aliased again causing the system to crash?
I understand that this happens when one aliases one of the methods in the hidden classes, like Sprite for example.
I'm looking for the code that checks out if the alias has been done, so that i can use it as a condition to decide if aliasing or not.
Reply }
#2
It isn't just hidden classes. No - no - no . :cheery:

Place a condition to check an @instance value around your aliased method in question, and immediately set that @instance value to something other than 'nil'.

Code:
#--------------------------------------------------------------------------
  # * Fallen Pose
  #--------------------------------------------------------------------------
  if @derv_anim_bat_stack.nil?
    @derv_anim_bat_stack = true
    alias mnk_collapse collapse
    def collapse
      if @battler.is_a?(Game_Actor)
        mnk_collapse if DEFAULT_ACTOR
        mnk_collapse if DEFAULT_COLLAPSE_ACTOR
        if DEFAULT_ACTOR_ID != nil
          mnk_collapse if DEFAULT_ACTOR_ID.include?(@battler.id)
        end
      else
        mnk_collapse if DEFAULT_ENEMY
        mnk_collapse if DEFAULT_COLLAPSE_ENEMY
        if DEFAULT_ENEMY_ID != nil
          mnk_collapse if DEFAULT_ENEMY_ID.include?(@battler.id)
        end
      end
    end
  end

As you can see (and thanks to SephirothSpawn for showing me this), I wrap an entire method in an if...end block that has a condition....
Code:
if @derv_anim_bat_stack.nil?
    @derv_anim_bat_stack = true

    ---the whole aliased block--

  end
which right after it, I set the @derv_anim_bat_stack value to 'true'.

It will only perform this method if the @instance value is nil, whereupon I set the @instance to something else. And, yeah. This part of Animated Battlers did create the dreaded F12 crash years ago until this fixed it.
Reply }
#3
I see. This is an option, thank you very much. I was wondering if there is something in Ruby that has been introduced specifically for this purpose. Something involving a defined? call, you know, this method that tells you if a name corresponds to a class, a variable, a method, a constant or nothing. But I had no luck trying to do it this way.
I guess I'm going to go with the solution you suggested but I'm still interested in knowing if there's some other way to do it. So if anyone has other inputs... :)
Reply }
#4
Hmm, I'm not sure if this would work. Check out method_defined? @ ruby-doc.org. :P
Reply }
#5
method_defined?
That sounds more like a call to see if a method exists within a class rather than checking to see if one has been aliased.

If that is the case, the statement would be useful to see if someone created a new method in their kickin' script for an existing class and you (as a scripter) was performing a compatibility check for that kickin' script.
Reply }
#6
There's a simpler way - there is actually a method for this in fact.

method_defined

When you alias you define a method, such as - - alias wy_main main

So we can do:

unless method_defined?(wy_main)
alias wy_main main
end

Or simpler still just

alias wy_main main unless method_defined?(wy_main)



I can't believe that a couple of years ago Trickster made a "disable F12" script and everyone used it as a fix for this :x



Edit: wha- where did those posts come from o.O
Reply }
#7
Interesting.

So by using:
method_defined?(wy_main)

... we can perform a check for an aliased method assuming we know that the actual alias statement looks for the 'new' method name rather than the name of the original method.

Nice.
Reply }


Possibly Related Threads…
Thread Author Replies Views Last Post
   Array to arguments for a method? PK8 2 4,954 07-08-2011, 10:47 PM
Last Post: PK8
   Check player facing direction desbrina 5 7,623 06-20-2010, 09:58 PM
Last Post: deValdr
   New Encryption Method Test Legacy 2 4,779 05-22-2010, 07:17 PM
Last Post: Monster Buffer
   How to check if the value of a number is an even or odd number? PK8 5 7,581 01-04-2010, 10:45 PM
Last Post: PK8
   How to check if actor has certain stat(s) with RGSS? Twin Matrix 4 9,756 01-04-2009, 05:35 PM
Last Post: Twin Matrix



Users browsing this thread: 1 Guest(s)