Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Properly checking method definition and wether to alias if undefined?

Good day everybody!

Although I'm somewhat familiar with 'self.method_defined?()', or in other words I've at least seen it used, I was curious whats the proper way to use it, or what are the different ways it can be used? For instance, I always see people checking the alias name of a method instead of the origional name of the method, why is that? Are they checking if an alias from a previous script exists? I've never seen another scripter just checking for the origional method, I always see just an alias name being checked so I'm unsure.

Simply, what I'm trying to do is prevent overwritting SDK::Scene_Base methods in my scenes where, the method isn't by default defined, and its unclear if it would need to be aliased or not.

So, if I was to create 'main_window' for Scene_Gameover (since it, by default, doesn't use any windows it isn't created), how would I be sure that I'm not overwritting some other code somewhere previous to that call? Can I freely alias a superclass method, such as when Scene_Gameover inherits from the SDK::Scene_Base?

How do I work that situation, am I supposed to do something like...?

Code:
class Scene_Gameover < SDK::Scene_Base
  alias_method :exampl_scn_gmover_main_window, :main_window
  def main_window
    if self.method_defined?(:main_window)
      exampl_scn_gmover_main_window
    end
    # ...New code...
  end
end

I'm just asking because, although I've seen it used, I don't know exactly how it works and I'm learning more about superclasses too.
 
I tried to call that method and it didn't work. The only way to call it is from the class or module itself, for example
Code:
Scene_Gameover.method_defined?("main_window") (or with :main_window)

Go to http://homepage2.nifty.com/sakazuki/rde_en/ and download it, it is a Ruby IDE. You can easily test anything there.

Code:
class Base
  def something
    p "Base's somehing method called"
  end
end
class Child < Base
  if Base.method_defined?("something") then alias aliased_something something end
  def something
    if Base.method_defined?("something")
      aliased_something
    end
    p "Child's something method called"
    # ...New code...
  end
  
end
a = Child.new
a.something

And when you download it test that code. You'll note that if you change Base's something method's name, it will only call the Child's something method.

Oh and btw, whenever you have a problem with a method, class or whatever take a look at this:
http://www.ruby-doc.org/core/

later
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top