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.

Modifying methods

Ok, I'm developing a script: Let's say I want to modify a single line (or a single one of those 'when' statements if it makes it easier) of the "move_type_custom" method in Game_Character 2.

Now since the bit I want to modify is right in the middle, I can't alias the method (as far as I know). I'd have to copy/paste the entire thing, and modify that single line (therefore overwriting the entire thing) - wouldn't I?

Or is there an easier way to do it (or one that involves less lines that haven't changed at all >_<)?
 
I don't know if this work, but it's worth a try.
Code:
class Game_Character 
  alias old_move_type_custom move_type_custom
  def move_type_custom
    old_move_type_custom
    while @move_route_index < @move_route.list.size
      if command.code >= 27
        # Branch by command code
        case command.code
          when 41  # Change Graphic
            @tile_id = 0
            @character_name = command.parameters[0]
            @character_hue = command.parameters[1]
            if @original_direction != command.parameters[2]
              @direction = command.parameters[2]
              @original_direction = @direction
              @prelock_direction = 0
            end
            if @original_pattern != command.parameters[3]
              @pattern = command.parameters[3]
              @original_pattern = @pattern
            end
          end
        end
      end
    end
  end
end
 
The problem is what is that when 41 is within a loop block (while blah < blah). Any command with codes of 27 and 45 will be executed multiple times, possibly changing your @move_route_index instance multiple times before the original method is done executing.

So intead, seeing as how code 41 is modifing the instances : @tile_id, @character_name, ..., @pattern, it is better to make a tempory duplicate before the original method is called, call the original method, then compare changes.

For instance, lets say you were going to detect for f_(n) out of character name, and store n into a instance variable and remove the f_(n). ;)

Code:
class Game_Character
  alias_method :dirtie_command41_gmchr_mtc, :move_type_custom
  def move_type_custom
    chr_name = @character_name.dup
    dirtie_command41_gmchr_mtc
    if @character_name != chr_name
      @character_name.gsub!(/f\_(\d)/, '') do
      if $1 != @character_name
        @frames = $1.to_i
      end
    end
  end
end

The regexp may be not be all right (I just took if from you :p), but you can see how I just checked if @character_name was different from what it was before the method was call and then executed our code.

Something like that what you are looking for?
 
It's not that complicated :P

Your example is a little confusing. If you would be so kind, try giving me a simpler example where the "@character_name = command.parameters[0]" line is just replaced with a simple call to another method. Something like: "my_method(command.parameters[0])"
 
I am not entirely sure if this is possible, but I did have an idea, though I have no clue how to implement it. I'm not even sure how alias works in the first place. anyway, here was my idea....

Alias the method
store the aliased method(if possible) into a string.
split the string to extract what you don't want and insert what you do want.
convert the string back to an aliased method
call the aliased method inside a defined method.

So Trickster, would that be doable(or something similar)? and if so, how?
 

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