Ruby Question

Started by Jaiden, October 28, 2017, 05:39:25 pm

Previous topic - Next topic

Jaiden

I am having a hard time figuring out what a method with an "=" is. I'm sure it's really basic Ruby stuff, but what is the difference between these two methods? Rather, what does it mean to attach "=()" at the end of a method?

#--------------------------------------------------------------------------
  # * Get Top Row
  #--------------------------------------------------------------------------
  def top_row
    # Divide y-coordinate of window contents transfer origin by 1 row
    # height of 32
    return self.oy / 32
  end

#--------------------------------------------------------------------------
  # * Set Top Row
  #     row : row shown on top
  #--------------------------------------------------------------------------
  def top_row=(row)
    # If row is less than 0, change it to 0
    if row < 0
      row = 0
    end
    # If row exceeds row_max - 1, change it to row_max - 1
    if row > row_max - 1
      row = row_max - 1
    end
    # Multiply 1 row height by 32 for y-coordinate of window contents
    # transfer origin
    self.oy = row * 32
  end

KK20

These are basically like your get and set methods in other languages. Typically they're used for instance variables (start with @) where if you need to do some additional checking or something more unorthodox rather than just straight read/write.

def hp
  "Your health is at #{@hp}"
end

def hp=(n)
  @hp = [n, @max_hp].min
end

There's a few keywords in Ruby that you've seen that start with attr_ then followed by an instance variable name. These setup the get and set methods so you don't have to write them out for every single variable you need. The important ones are

  • reader = get method

  • writer = set method

  • accessor = get and set methods


Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

Jaiden

Excellent, thank you. I was guessing on "attr_accessor", that really helps.

Another thing I am not very clear on (basic ruby stuff) is code that looks like this:
alias other_name a_method
  def a_method
    ...code...
    a_method
  end

Is putting the original method name at the end of the method necessary for an alias? It looks like the method is calling itself? I've seen it in some scripts I've looked through, but I've had a hard time finding any details on it.

KK20

Alias is the same line of thinking as "let's make a copy of this method and call it something else". Its main purpose in RM scripts is for compatibility with other users' scripts. If you rewrite a method that another script uses, the script closest to Main takes priority since scripts are loaded in order top to bottom. By aliasing and calling the method again, you're adding onto the method rather than rewriting it completely.

def foo
  p "this is last!"
end

alias bar foo
def foo
  p "this is first!"
  bar
end

foo #=> "this is first!" then "this is last!"

This is essential when you need to modify core classes in the default scripts.

Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

Jaiden

Perfect! You even partially answered my last question, which is script order / stack. Thanks!

If I have two scripts that make calls to Window_Base, and they both define "draw_actor_hp", for example, does RGSS process it from the bottom up, with the last script taking priority or the first? I understand if the same method is called twice on the stack, it causes a "stack level too deep" error, but I'm not quite sure how multiple method definitions work together and how RGSS knows which definition to call.



KK20

As I said, scripts are loaded top to bottom, meaning the last script to be read takes priority. You could easily confirm this yourself by making two identical methods that print different values.

Stack error typically occurs when you call a method that calls itself i.e. unintentional recursion that has no base case. This generally happens when two authors alias the same method with the same aliased name.

Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

Jaiden

That helps a ton, thanks again!