Chaos Project

RPG Maker => RPG Maker Scripts => Script Troubleshooting => Topic started by: Jaiden on November 08, 2017, 01:56:38 pm

Title: Understanding Aliases
Post by: Jaiden on November 08, 2017, 01:56:38 pm
I've done some reading on aliases and understand (somewhat) how they work, but there is still something I don't understand:

When looking at an alias method, I've noticed the method appears within the alias, but I'm not sure I correctly understand it's function in relation to its placement, as I've seen it both ways. For example:

alias superduper_update_target update_target
def update_target
   #some code
   #goes here
   superduper_update_target
end


vs

alias superduper_update_target update_target
def update_target
   superduper_update_target
   #some code
   #goes here
end


Does this determine where in the original "update_target" method the code is placed? In this case, the first method places the new code above the original method, and the second method places the new code below the original method, or is it visa versa?

Thanks guys.





Title: Re: Understanding Aliases
Post by: KK20 on November 08, 2017, 02:09:15 pm
QuoteIn this case, the first method places the new code above the original method, and the second method places the new code below the original method

This is correct.
Order is everything:

def foo(a, b)
  @a = a
  @b = b
end

# Works
alias bar foo
def foo(a, b)
  bar(a, b)
  @a += 5
end

# Syntax Error
alias bar foo
def foo(a, b)
  @a += 5 # @a is currently nil
  bar(a, b)
end
Title: Re: Understanding Aliases
Post by: Jaiden on November 08, 2017, 02:23:39 pm
Awesome. Thanks.