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.

Aliasing with 'loop do' stuff added.

Is there a certain way to alias a method with a 'loop do' command, especially a 'loop do' with 'if' and 'else' statements in it? Basically, I'm editing a script that copies and re-writes 3 or 4 default scripts, with additional information added into the methods.

I've prettymuch got alias_method down, but if you're trying to modify a 'loop do' with an alias method, how do you do it?

Code:
def method
    # Some code before the loop
    loop do
      if @somecode == 1
        # Do Something 1
      elsif @somecode == 2
        # Do Something 2
      else
        # Do Something 3 (Added to the method)
      end
    end
  end
 

khmp

Sponsor

alias_method is only able to add code to the end of, or the beginning of the method being aliased. You can't alias a method and modify the code of the alias method. A risky maneuver would be getting the result and modifying it as necessary. I say risky because you are probably depending upon the result to equate to a certain range of answers. If anyone is using this same methodology they can alter the answer before it reaches your alias if the other one is encountered first.

Example:
Code:
class Test
  def alpha
    return true
  end
  alias beta alpha
  def alpha
    answer = beta
    return !answer
  end
end

But I suppose that risk is better than just overriding the method. Anyway back to the most important point and the question you actually asked. You can't modify the functionality of the alias_method directly. But if the resultant is returned you are more than welcome to it with some risks involved as well.

Good luck with it Kain Nobel! :thumb:
 

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