MV Call a common event on map change

A few days ago, I was searching for a method to invoke a common event whenever the player transitions to a different map. After some time, I discovered this thread:


This does exactly what I was looking for. I utilized the abridged version of the code posted by @Soulrender :

JavaScript:
//=============================================================================
// MapChange.js
//=============================================================================
/*:
 * @plugindesc Calls a common event 75 on map change
 * @author Soulrender
 */
var alias = Scene_Map.prototype.start;
Scene_Map.prototype.start = function(){
    alias.call(this);
    $gameTemp.reserveCommonEvent(75); // common event
}

However, I recently realized that all the skills in my game that call a common event on the map (outside of battles) aren't functioning as intended. It appears that closing the menu also initiates "Scene_Map", and as you know, only one common event can be reserved.

Therefore, I am in search of a solution to ensure this script operates solely when the map changes.

One potential method could be to store the last map in a variable and then verify whether the current map differs from the stored one before reserving the common event. But I am unsure of how to implement this, or even if there might be a better approach.

Thank you!
Okay, I managed to solve it. It's not the most elegant solution, but it gets the job done.

JavaScript:
//=============================================================================
// MapChange.js
//=============================================================================

/*:
 * @plugindesc Calls a common event 75 on map change
 * @author Soulrender
 */

var _globalLastMap = null;

var aliasStart = Scene_Map.prototype.start;
Scene_Map.prototype.start = function(){
    aliasStart.call(this);
    if(_globalLastMap!==$gameMap.mapId()){
        $gameTemp.reserveCommonEvent(75); // common event
    }
    _globalLastMap=$gameMap.mapId();
}

I stored the map in a global variable, and then on every event call I check the current map is different to the stored one.

I hope this helps someone!
It works because it seems to be helping you right now, but I gotta say that it's a terrible approach.
Why should it be run EVERY SINGLE TIME the map is called?
Fine, you cleverly used a condition. Yet, that's not supposed to happen in the RM series.
I can tell you that every single installment of the engine ever since RMXP until RMMV at least allows you to go to a very specific place to check such things if ever needed. It's called...

Game_Map's setup method or function depending on the language currently in use.

JavaScript:
var gm_map_start = Game_Map.prototype.setup;
Scene_Map.prototype.setup = function(mapId){
  gm_map_start.call(this, mapId);
  $gameTemp.reserveCommonEvent(75); // common event
}

EDIT: I had to pass an additional parameter to prevent it from crashing.

Why is this the best approach?
It's quite simple. Game_Map's setup function is only called once per map transfer. Scene_Map's start function gets called from MANY PLACES or plugins so it's not reliable at all.
I would argue that the above response is a little demeaning, considering you came to the right place and asked the right question, even giving evidence of having already done at least a modicum of research, then did the work to find an answer yourself, showing all the evidence of productive learning...but I believe they're right; if you used that Game_Map function you would circumvent problems like what you were seeing.

Could I ask what your common event does? It sounds like you've got a decent grasp of what you need to happen, and they covered eventing vs. coding in the post you linked - you're probably fine on your own from here - but sometimes it does make more sense to use map events or a plugin more specific to what you're trying to do than using a plugin and a common event together.

In any case, I'm glad you've got something working and some guidance to help you get it working better. Good luck!
Just asking for a small favor, never call me they. Like never. It's he because I am male. Thanks. (It just happens that I find it insulting to be called they.)
I'm sorry for not assuming your gender. Thank you for the correction.
Game_Map's setup function is only called once per map transfer
This is just what I was looking for, I didn't test it yet, but it sounds like the correct event for me, thank you so much!

Could I ask what your common event does?
Well, I believe your assumption is correct. This is a workaround for a more complex issue I've been trying to resolve for months, related to a bug in Yanfly's Event Spawner plugin. For some reason, I'm unable to remove spawned events on other maps using the plugin commands. I have to be present on the map to remove these events. Therefore, I created this script to call the DespawnEventID when the player transitions to a different map.

I know it is a horrible way to do it, but it seems there is no other way.

Thank you for helping me with this issue, guys.
In that case you'd just need to use this version instead. It'll clear the events BEFORE creating any new ones or even loading the typical map events.
JavaScript:
var gm_map_setup = Game_Map.prototype.setup;
Game_Map.prototype.setup = function(mapId){
  Yanfly.ClearSpawnedEvents();
  gm_map_setup.call(this, mapId);
}
oh, you are right, I can move all the content of the common event 75 to the Scene_Map setup event.

Lets see how it works 😂

---------- EDITED -----------

It doesn't work I tried using DespawnEventID() and ClearSpawnedEvents(), before and after gm_map_start.call().

JavaScript:
var gm_map_start = Game_Map.prototype.setup;
Scene_Map.prototype.setup = function(mapId){
    Yanfly.ClearSpawnedEvents();
    while($gameMap.FirstSpawnedEventID()){
       Yanfly.DespawnEventID($gameMap.FirstSpawnedEventID());
    }
    gm_map_start.call(this, mapId);
    //$gameTemp.reserveCommonEvent(75);
    Yanfly.ClearSpawnedEvents();
    while($gameMap.FirstSpawnedEventID()){
       Yanfly.DespawnEventID($gameMap.FirstSpawnedEventID());
    }
}

It is like the Yanfly object is not working here.

---------- EDITED 2 -----------

Oh no, the Scene_Map.prototype.setup is not being called at all when I move to another map!

This doesn't work:
JavaScript:
var gm_map_start = Game_Map.prototype.setup;
Scene_Map.prototype.setup = function(mapId){
    console.log('test');
    gm_map_start.call(this, mapId);
}

---------- EDITED 3 -----------

I found the error it's Game_Map, not Scene_Map, this works:
JavaScript:
var gm_map_start = Game_Map.prototype.setup;
Game_Map.prototype.setup = function(mapId){
    gm_map_start.call(this, mapId);
    if($gameVariables.value(57)!=mapId){
        Yanfly.ClearSpawnedEvents();
    }
}
It'd be better to clear the events BEFORE it calls the aliased function that will create all the new map events.

I missed the old Scene_Map#setup alias. 😛

Original thread: https://forums.rpgmakerweb.com/threads/call-a-common-event-on-map-change.159604/