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.

Some questions about alias (><)

Khoa

Member

I'm trying to learn RGSS, and I don't know exactly about alias (><).
If can, someone please answer some of my questions:
  • alias add code on top or bottom of the method?
  • Does it allow me to add code in the middle of the method? And how to do that?
  • Does it allow me to replace instead of add? And how to do that?
Please help me if you know (^^).
And thank you for reading my topic (^^).
 
Yes, that is exactly what an alias does!

I presume that it could, haven't tried it before. It'd probably be carried out like so...

Code:
class Example
  alias_method :your_initialize, :initialize
  def initialize
    # Some code...
    your_initialize
    return
    # Some code...
  end
end

That raises a question to me, is it possible to call more than 1 alias without a stack level too deep?

No, to replace a class's method, you'd just re-write it entirely. As long as the script is below the script you're aliasing to, it should work.
 

Khoa

Member

thank you, Kain Nobel (^^)
Code:
class Example
  alias_method :your_initialize, :initialize
  def initialize
    # Some code...
    your_initialize
    return
    # Some code...
  end
end
Could you please tell me what is below def initialize and what is below your_initialize?
I get a headache when trying to understand it by myself (><)
 
im not sure what the difference between:

alias old_initialize initialize

and:

alias_method :your_initialize, :initialize

is but i use the former... its seems easier.
What it does is store the original data that was under the definition initialize (for instance) and puts it into a new command (old_initialize).

in other words you can store an old method:

alias old_main main
alias old_bgm_play bgm_play
alias cookies initialize
alias newname oldmethodname

as anything and then call it under its new name wherever.

alias old_main main
def main
  #mycode
  old_main
end

i always call the new method old_originalname but it can be anything (as shown with the cookies reference)... does that help?
 

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