MV Turning Common Event Script into a Plugin

I wrote a small script to change the player's movement speed based on whether or not the current map type is Overworld:
JavaScript:
xDRAGOONx.setMoveSpeed = function(){
    if ($gameMap.isOverworld(this)) {
        $gamePlayer.setMoveSpeed(3);
        $gamePlayer.hideFollowers();
        $gamePlayer.refresh();
         } else {
        $gamePlayer.setMoveSpeed(4);
        $gamePlayer.showFollowers();
        $gamePlayer.refresh();
         }
}
xDRAGOONx.setMoveSpeed.call(this);
and after testing it as a common event called in a transfer event, I am wondering how I could add this in some way as a plugin so I don't have to add it to every single Overworld to Town transfer event.

I tried modifying:
JavaScript:
Game_CharacterBase.prototype.setMoveSpeed = function(moveSpeed) {
    this._moveSpeed = moveSpeed;
};
but I only managed to mess up the followers' movement, as I an not sure exactly what I am doing there.
i usually have 1 autorun event on every map anyways so wouldnt be any issue adding it into those.
these autoruns could range from setting/enable/disable a teleport and warp location,
playing different music because day/night or war/festival is going on,
to changing the entire party's actor images based on state(dead/undead) or class changes from the menu. edit: that 1 more of common event then map event.
but teleporting NPC based on time and daily schedule was also autorun>erase map event.
just 2 more lines for speed and followers is nothing
If you want to automatically invoke your method whenever the player changes map, you could try patching it in after the default setup method of Game_Map, e.g.
JavaScript:
(function() {
  var _Game_Map_setup = Game_Map.prototype.setup;
  Game_Map.prototype.setup = function() {
    _Game_Map_setup.apply(this, arguments);
    xDRAGOONx.setMoveSpeed();
  };
})();
You could put both that and your xDRAGOONx.setMoveSpeed method into a .js file (paste into text editor, Save As > File Type: All Files, Filename: whatever.js) and import as a plugin, I think that'll work. 🙂

(I'm not sure why you've passed isOverworld an argument...I don't see one in MV's code?)
i usually have 1 autorun event on every map anyways so wouldnt be any issue adding it into those.
these autoruns could range from setting/enable/disable a teleport and warp location,
playing different music because day/night or war/festival is going on,
to changing the entire party's actor images based on state(dead/undead) or class changes from the menu. edit: that 1 more of common event then map event.
but teleporting NPC based on time and daily schedule was also autorun>erase map event.
just 2 more lines for speed and followers is nothing

I don't disagree with your method, but I am starting to get to the point where I want to go one step further learning the code behind the system and this looks like a great place to start.


If you want to automatically invoke your method whenever the player changes map, you could try patching it in after the default setup method of Game_Map, e.g.
JavaScript:
(function() {
  var _Game_Map_setup = Game_Map.prototype.setup;
  Game_Map.prototype.setup = function() {
    _Game_Map_setup.apply(this, arguments);
    xDRAGOONx.setMoveSpeed();
  };
})();
You could put both that and your xDRAGOONx.setMoveSpeed method into a .js file (paste into text editor, Save As > File Type: All Files, Filename: whatever.js) and import as a plugin, I think that'll work. 🙂

(I'm not sure why you've passed isOverworld an argument...I don't see one in MV's code?)

@caethyril Thank you! I've already added my code as a plugin so I can just add this to it. Thanks again for pointing me to the specific code I was looking for.

Also,
JavaScript:
Game_Map.prototype.isOverworld = function() {
    return this.tileset() && this.tileset().mode === 0;
};
is in rpg_objects.js

-EDIT-
It worked! Thanks again, this gives me a foothold going forward.
@xDRAGOONx great! 😂

And yes, but by default isOverworld doesn't expect any arguments, i.e. it's function(), not function(stuff). It just means the following two lines are equivalent:
JavaScript:
$gameMap.isOverworld(this)
$gameMap.isOverworld()
Okay, I understand what your saying

Original thread: https://forums.rpgmakerweb.com/threads/turning-common-event-script-into-a-plugin.128960/