[SOLVED] How to overwrite a "vanilla" function?

Discussion in 'Javascript/Plugin Support' started by TheWarDoctah, Dec 15, 2018.

Thread Status:
Not open for further replies.
  1. TheWarDoctah

    TheWarDoctah Warper Member

    Messages:
    2
    Likes Received:
    0
    First Language:
    Portuguese
    Primarily Uses:
    RMMV
    Hi, I'm somewhat new to programming. I'm messing with mouse commands on MV, but I'm altering direct on the rpg_xx.js files, and there are changes that I even don't remember which line was. HAhaha.

    Well, how can I create a plugin that my function totally overwrite the original function (on rpg_xxxx files)?
    Example:
    Code:
    Game_Player.prototype.triggerTouchActionD1 = function (x1, y1) {
        if ($gameMap.airship().pos(x1, y1)) {
            if (TouchInput.isTriggered() && this.getOnOffVehicle()) {
                return true;
            }
        }
        this.checkEventTriggerHere([0]);
        return $gameMap.setupStartingEvent();
    };
    I want to change the whole function in my plugin file, how I can do this? Is there like an equivalent to the way that works on RGSS

    Code:
    alias :my_method :method
    def method
     my_method // if I remember this call the base method, I don't want to execute anything from the original JS function, just overwrite it.
     do_additional_stuff
    end
     
    #1
  2. TheoAllen

    TheoAllen Self-proclaimed jack of all trades Veteran

    Messages:
    4,451
    Likes Received:
    5,042
    Location:
    Riftverse
    First Language:
    Indonesian
    Primarily Uses:
    RMVXA
    If you're looking a way to make an alias, here is what you need to do.
    Code:
    (function(){
      var _aliasedTriggerTouch = Game_Player.prototype.triggerTouchActionD1;
      Game_Player.prototype.triggerTouchActionD1 = function (x1, y1) {
        // Your custom code here
        _aliasedTriggerTouch.call(this, x1, y1);
        // Or here
      }
    })();
    If you're looking a way to overwrite a method, you can just drop a file as a plugin.
    Then copy whatever methods you want to overwrite.
     
    #2
  3. Shaz

    Shaz Veteran Veteran

    Messages:
    37,807
    Likes Received:
    11,512
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    Easiest way to completely overwrite a method from the core scripts is to copy and paste it into your new plugin, take out everything except the first line and the last line, and put whatever you want in between.
     
    #3
    TheWarDoctah likes this.
  4. TheWarDoctah

    TheWarDoctah Warper Member

    Messages:
    2
    Likes Received:
    0
    First Language:
    Portuguese
    Primarily Uses:
    RMMV
    Thank you guys! This work pretty well! Solved.
     
    #4
  5. slimmmeiske2

    slimmmeiske2 Little Red Riding Hood Moderator

    Messages:
    5,676
    Likes Received:
    4,158
    Location:
    Belgium
    First Language:
    Dutch
    Primarily Uses:
    RMXP

    This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

     
    #5
Thread Status:
Not open for further replies.

Share This Page