Call Common Event on Map Entrance/Exit based on Mapnotes

Hey, I'm an absolute beginner and basically don't know anything about javascript.

I'm trying to get a plugin done that allows me to have common events fire off automatically on map entrance & exit based on mapnotes.

That...should not be difficult. In fact, there's a script that pretty much almost does exactly that:
https://forums.rpgmakerweb.com/index.php?threads/plugin-assistance.64866/#post-621453

And it works. There's just one problem: I don't want to enter some number & tag every time. I want something simple that's quickly entered and that's so simple that the others working on the project with me don't need a guide either. Something like <indoors> <outside> or <weather_off>, that's easy to remember and quickly done.

So I've tried to use that script as basis to somehow get it working with parameters and it failed miserably. Like, not even bones.

I've since tried writing an even simpler version from scratch using the code of the above plugin as orientation...and I still fail. Yeah. 0 javascript experience.
-----------------------------------------------------------------------------------------------------
This is how it should work:
1. The user assigns parameters to six parameter slots in the plugin with the ID of the common event that should be called.

So I set the values up via
Code:
 * @param Outside Common Event
 * @desc Set this to the Number of the Common Event you wish to call
 * @default 0

Basically everything beyond there starts to get sketchy.
Code:
var Imported = Imported || {};
Imported.SMTE_MapTransferCommonEvent = true;

var SMTE  = SMTE      || {};
SMTE.MAPCHECK = SMTE.MAPCHECK || {};

var params = PluginManager.parameters('SMTE.MAPCHECK')
SMTE.MAPCHECK.outside = Number(params['Outside Common Event']);
SMTE.MAPCHECK.inside = Number(params['Inside Common Event']);
SMTE.MAPCHECK.off = Number(params['Invisible Weather Common Event']);
SMTE.MAPCHECK.leave_outside = Number(params['Leave Outside Common Event']);
SMTE.MAPCHECK.leave_inside = Number(params['Leave Inside Common Event']);
SMTE.MAPCHECK.leave_off = Number(params['Leave Invisible Weather Common Event']);

Now that's me hoping I've got the plugin setup and the parameters loaded.

Code:
SMTE.MAPCHECK.Game_Map_initialize = Game_Map.prototype.initialize;

Game_Map.prototype.initialize = function() {
 
    this._MAPCHECKInterpreter = new Game_Interpreter();
    this._MAPCHECKOutsideEvent = SMTE.MAPCHECK.outside;
    this._MAPCHECKInsideEvent = SMTE.MAPCHECK.inside;
    this._MAPCHECKOffEvent = SMTE.MAPCHECK.off;
    this._MAPCHECKExitOutsideEvent = SMTE.MAPCHECK.leave_outside;
    this._MAPCHECKExitInsideEvent = SMTE.MAPCHECK.leave_inside;
    this._MAPCHECKExitOffEvent = SMTE.MAPCHECK.leave_off;
 
    SMTE.MAPCHECK.Game_Map_initialize.call(this)
I'm...just replicating what the other plugin does here, and I'm absolutely not sure what it is I'm doing.
I guess I'm sharing variables around.

Last part of the plugin:
Code:
SMTE.MAPCHECK.Game_Map_setup = Game_Map.prototype.setup;

Game_Map.prototype.setup = function(mapId) {

    if (($gamePlayer.newMapId() != $gameMap.mapId()) && ($dataMap.meta.exit_outside || $dataMap.meta.exit_outdoor) && (this._MAPCHECKExitOutsideEvent > 0)) {
        $gameTemp.reserveCommonEvent($data_common_events(this._MAPCHECKExitOutsideEvent));
    } else
    if (($gamePlayer.newMapId() != $gameMap.mapId()) && ($dataMap.meta.exit_inside || $dataMap.meta._exit_indoor) && (this._MAPCHECKExitInsideEvent > 0)) {
        $gameTemp.reserveCommonEvent($data_common_events(this._MAPCHECKExitInsideEvent));
    } else
    if (($gamePlayer.newMapId() != $gameMap.mapId()) && ($dataMap.meta.exit_no_weather || $dataMap.meta.exit_weather_off) && (this._MAPCHECKExitOffEvent > 0)) {
        $gameTemp.reserveCommonEvent($data_common_events(this._MAPCHECKExitOffEvent));
    }

    SMTE.MAPCHECK.Game_Map_setup.call(this, mapId);
 
    if (($gamePlayer.newMapId() == $gameMap.mapId()) && ($dataMap.meta.enter_outside || $dataMap.meta.enter_outdoor) && (this._MAPCHECKOutsideEvent > 0)) {
        $gameTemp.reserveCommonEvent($data_common_events(this._MAPCHECKOutsideEvent));
    } else
    if (($gamePlayer.newMapId() == $gameMap.mapId()) && ($dataMap.meta.enter_inside || $dataMap.meta._enter_indoor) && (this._MAPCHECKInsideEvent > 0)) {
        $gameTemp.reserveCommonEvent($data_common_events(this._MAPCHECKInsideEvent));
    } else
    if (($gamePlayer.newMapId() == $gameMap.mapId()) && ($dataMap.meta.enter_no_weather || $dataMap.meta.enter_weather_off) && (this._MAPCHECKOffEvent > 0)) {
        $gameTemp.reserveCommonEvent($data_common_events(this._MAPCHECKOffEvent));
    }

};
I've also tried it with removing $data_common_events and just using $gameTemp.reserveCommonEvent(this._MAPCHECKOffEvent); but it's not working out either.

I have a feeling what I'm trying to do is incredibly simple and can be done in a couple of lines, meanwhile I'm cluttering everything up, but I've really got no idea what's going on here.

Help would really be appreciated - I've pretty much wasted my entire day on this and I still can't get anywhere.
$dataCommonEvents, not $data_common_events
$dataCommonEvents, not $data_common_events

Thanks, I've changed the part to this:
Code:
SMTE.MAPCHECK.Game_Map_setup = Game_Map.prototype.setup;

Game_Map.prototype.setup = function(mapId) {

    if (($gamePlayer.newMapId() != $gameMap.mapId()) && ($dataMap.meta.exit_outside || $dataMap.meta.exit_outdoor) && (this._MAPCHECKExitOutsideEvent > 0)) {
        $gameTemp.reserveCommonEvent($dataCommonEvents(this._MAPCHECKExitOutsideEvent));
    }
    else if (($gamePlayer.newMapId() != $gameMap.mapId()) && ($dataMap.meta.exit_inside || $dataMap.meta.exit_indoor) && (this._MAPCHECKExitInsideEvent > 0)) {
        $gameTemp.reserveCommonEvent($dataCommonEvents(this._MAPCHECKExitInsideEvent));
    }
    else if (($gamePlayer.newMapId() != $gameMap.mapId()) && ($dataMap.meta.exit_no_weather || $dataMap.meta.exit_weather_off) && (this._MAPCHECKExitOffEvent > 0)) {
        $gameTemp.reserveCommonEvent($dataCommonEvents(this._MAPCHECKExitOffEvent));
    }

    SMTE.MAPCHECK.Game_Map_setup.call(this, mapId);
    
    if (($gamePlayer.newMapId() == $gameMap.mapId()) && ($dataMap.meta.enter_outside || $dataMap.meta.enter_outdoor) && (this._MAPCHECKOutsideEvent > 0)) {
        $gameTemp.reserveCommonEvent($dataCommonEvents(this._MAPCHECKOutsideEvent));
    }
    else if (($gamePlayer.newMapId() == $gameMap.mapId()) && ($dataMap.meta.enter_inside || $dataMap.meta.enter_indoor) && (this._MAPCHECKInsideEvent > 0)) {
        $gameTemp.reserveCommonEvent($dataCommonEvents(this._MAPCHECKInsideEvent));
    }
    else if (($gamePlayer.newMapId() == $gameMap.mapId()) && ($dataMap.meta.enter_no_weather || $dataMap.meta.enter_weather_off) && (this._MAPCHECKOffEvent > 0)) {
        $gameTemp.reserveCommonEvent($dataCommonEvents(this._MAPCHECKOffEvent));
    }
};
But it still isn't doing anything.
I've got three maps set up.
One has the mapnote <enter_outdoor>
another <exit_inside>
and <enter_weather_off>
.

I've got common events set up for them and defined their event id in the parameters inside the rpg maker mv plugin manager, so the connection should be there.
Still none of them trigger and I don't get any error messages that would help me understand what I'm doing wrong.

------------------------------------------------------------------------
I'd be fine with a modification of the original script, too, so long as I get to link parameters I can adjust to certain simple mapnotes.

I've found the original source of the script I'm using as guideline in another forum, which links to this github page of the orignal author .
The author is Exhydra, who now goes by @Ossra .

I've tried injecting a variant of my code into it to get that functionality, except it isn't working either.

The changes I've done to the original script by line of the original code...
Inserted at Line 14:
Code:
 * @param Outside Common Event
 * @desc Set this to the Number of the Common Event you wish to call
 * @default 0
 *
 * @param Inside Common Event
 * @desc Set this to the Number of the Common Event you wish to call
 * @default 0
 *
 * @param Invisible Weather Common Event
 * @desc Set this to the Number of the Common Event you wish to call
 * @default 0
 *
 * @param Leave Outside Common Event
 * @desc Set this to the Number of the Common Event you wish to call
 * @default 0
 *
 * @param Leave Inside Common Event
 * @desc Set this to the Number of the Common Event you wish to call
 * @default 0
 *
 * @param Leave Invisible Weather Common Event
 * @desc Set this to the Number of the Common Event you wish to call
 * @default 0
This should set up the parameters for the plugin manager & me as enduser.

At Line 62, I've inserted this:
Code:
var params = PluginManager.parameters('EXA.MTCE')
EXA.MTCE.outside = Number(params['Outside Common Event']);
EXA.MTCE.inside = Number(params['Inside Common Event']);
EXA.MTCE.off = Number(params['Invisible Weather Common Event']);
EXA.MTCE.leave_outside = Number(params['Leave Outside Common Event']);
EXA.MTCE.leave_inside = Number(params['Leave Inside Common Event']);
EXA.MTCE.leave_off = Number(params['Leave Invisible Weather Common Event']);
The idea is to draw the parameters into the plugin.

At Line 78 of the original script, I've inserted
Code:
    this._mtceOutsideEvent = 0;
    this._mtceInsideEvent = 0;
    this._mtceOffEvent = 0;
    this._mtceExitOutsideEvent = 0;
    this._mtceExitInsideEvent = 0;
    this._mtceExitOffEvent = 0;
These are not the same as above, since I figure that the interpreter used in the original code would otherwise always trigger the common event regardless of mapnote.
So by default, I set them to 0 and change them via the parameters above when the condition that the mapnote exists is true.

Lastly, I've replaced the original [Function] setup section with this one:
Code:
// ALIAS ─────────────────────────────────────────────────────────────────────────────┐
// □ [Function] setup
// └──────────────────────────────────────────────────────────────────────────────────┘

EXA.MTCE.Game_Map_setup = Game_Map.prototype.setup;

Game_Map.prototype.setup = function(mapId) {

    if ($gamePlayer.newMapId() != $gameMap.mapId()) {
        $gameMap.mtceRunCommonEvent(this._mtceExitEvent);
        $gameMap.mtceRunCommonEvent(this._mtceExitOutsideEvent);
        $gameMap.mtceRunCommonEvent(this._mtceExitInsideEvent);
        $gameMap.mtceRunCommonEvent(this._mtceExitOffEvent);
    }

    EXA.MTCE.Game_Map_setup.call(this, mapId);
    
    this._mtceEnterEvent = $dataMap.meta.mtceEn || 0;
    if ($dataMap.meta.enter_outside || $dataMap.meta.enter_outdoor) {
        this._mtceOutsideEvent = EXA.MTCE.outside;
    }
    if ($dataMap.meta.enter_inside || $dataMap.meta.enter_indoor) {
        this._mtceInsideEvent = EXA.MTCE.inside;
    }
    if ($dataMap.meta.enter_no_weather || $dataMap.meta.enter_weather_off) {
        this._mtceOffEvent = EXA.MTCE.off;
    }

    this._mtceExitEvent  = $dataMap.meta.mtceEx || 0;
    if ($dataMap.meta.exit_outside || $dataMap.meta.exit_outdoor) {
        this._mtceExitOutsideEvent = EXA.MTCE.leave_outside;
    }
    if ($dataMap.meta.exit_inside || $dataMap.meta.exit_indoor) {
        this._mtceExitInsideEvent = EXA.MTCE.leave_inside;
    }
    if ($dataMap.meta.exit_no_weather || $dataMap.meta.exit_weather_off) {
        this._mtceExitOffEvent = EXA.MTCE.leave_off;
    }

    if ($gamePlayer.newMapId() == $gameMap.mapId()) {
        $gameMap.mtceRunCommonEvent(this._mtceEnterEvent);
        $gameMap.mtceRunCommonEvent(this._mtceOutsideEvent);
        $gameMap.mtceRunCommonEvent(this._mtceInsideEvent);
        $gameMap.mtceRunCommonEvent(this._mtceOffEvent);
    }

}; // Game_Map ‹‹ setup
There are three parts to this.
The first part after if
Code:
if ($gamePlayer.newMapId() != $gameMap.mapId()) {
where I've added my lines underneath Exhydra/Ossra's to run the common event if the old and new map ID's aren't identical.

The second part is where I've added them underneath
Code:
this._mtceEnterEvent = $dataMap.meta.mtceEn || 0;
and
Code:
this._mtceExitEvent  = $dataMap.meta.mtceEx || 0;
where I check if the mapnotes exists and if so, I assign to what was previously set to 0 the parameter set by the plugin user.

The third and last part is underneath
Code:
    if ($gamePlayer.newMapId() == $gameMap.mapId()) {
where I added again my own entries to trigger if the newMapID and the mapID are identical.

The last part of the original script, the mtceCommonEvent function I've left untouched.

When I try to launch this modified code I get this error:
Code:
YEP_CoreEngine.js:1098 TypeError: Cannot read property 'list' of undefined
    at Game_Map.mtceRunCommonEvent (broken_map_transfer_common_event.js:184)
    at Game_Map.setup (broken_map_transfer_common_event.js:168)
    at Game_Player.performTransfer (rpg_objects.js:7480)
    at Game_Player.performTransfer (SRD_SuperToolsEngine.js:2614)
    at Game_Player.performTransfer (FELSKI_AUTOSAVE.js:239)
    at Scene_Map.onMapLoaded (rpg_scenes.js:569)
    at Scene_Map.onMapLoaded (YEP_X_ActSeqPack3.js:971)
    at Scene_Map.isReady (rpg_scenes.js:561)
    at Function.SceneManager.updateScene (rpg_managers.js:2018)
    at Function.SceneManager.updateMain [as updateMainFluidTimestep] (YEP_X_CoreUpdatesOpt.js:7951)
And yes, the modified plugin is currently called "broken_map_transfer" for obvious reasons.

The line maked in the sources where the error first appears is at
Code:
    var mtceEvent = $dataCommonEvents[cevId];
    this._mtceInterpreter.setup(mtceEvent.list);
down in the last part of the original section in the interpreter.
Somehow he isn't getting the correct values?
---------------------------------------------------------------------------------
I'd like either of these to work. The latter perhaps more, since the original script is so useful and more powerful, but the main thing is that I can use the mapnotes
<exit_indoor> etc. to setup the common events through parameters instead of using <mtceEx:commonEventId>.

I keep trying but I don't know enough of the basic javascript things to even know where to look for errors now.
I'd really appreciate the help.
@Anyone Hmm, well, that plugin version is rather old and outdated. I have since re-vamped the code, but I have not yet released the improved version.

Instead of inputting tags on each map, would it be acceptable to have options available within the plugin configuration to set certain common events to run on certain maps? That may be a faster, more consolidated option than entering tags on every map. If that sounds fine, I could adjust my current code and upload the plugin.



EDIT: Actually, I will set the plugin how you originally wanted and see how that goes - Transfer Event 1.18

To use the new feature of the plugin, go into the plugin configuration and enter as many 'Custom Map Tags' as you wish. Each custom tag can have an enter and exit common event set to it. The 'enter' common event is run when you first enter the map, and the 'exit' common event is run when you leave the map.

To use a tag in the map note field, simply place the tag name between an open and close angle bracket (e.g - <test>, <blarg>).

You can also use the default map note tags as well :
Code:
// 'eventId' is the ID of the common event you want to run.
<ossra EventOnTransfer enter eventId>
<ossra EventOnTransfer exit eventId>

Let me know if that works alright.



EDIT II: Updated to version 1.18 after coming across a bug that caused an infinite loop whenever the player is transferred within the same map.
That's straight up magic. Thank you so much. This is immensely helpful.

@Anyone Hmm, well, that plugin version is rather old and outdated. I have since re-vamped the code, but I have not yet released the improved version.

Instead of inputting tags on each map, would it be acceptable to have options available within the plugin configuration to set certain common events to run on certain maps? That may be a faster, more consolidated option than entering tags on every map. If that sounds fine, I could adjust my current code and upload the plugin.
I probably prefer the way you're currently doing, though I don't think it's a bad idea to also add that functionality. Right now, it's already an incredibly well-working piece of code.

I'm using it to control 3 types of maps, outdoor maps that feature full weather, indoor maps that only have sound and thunder lightmaps activating and a weather_off area where the player is basically so deep inside a structure that there's no weather visible or audible, even though the script in the background still stays aware that the weather exists outside.

Since that's bound to be on every map, throwing in a quick mapnote is a pretty fast way to go about it. But I can also see the utility of being able to have it run specifically on certain maps that you control inside the plugin manager without having to visit the mapnotes of all relevant maps.

Anyway, thanks once again. That's going to make things a lot easier for my team and me.
@Anyone No problem! Glad that the plugin is working nicely.

Original thread: https://forums.rpgmakerweb.com/threads/call-common-event-on-map-entrance-exit-based-on-mapnotes.114830/