Run Common Event when event stay in specific region

Hello!

I'm creating a very complicated game full of simultaneous actions and events (avoiding as much as possible using parallel processes).

To perform some functions and facilitate the gameplay, I would ask (please) for someone to create a Plugin.
That when an event with a specific comment is on a region id designated by the comment in the event, a common event is executed.

For example: Event 001 has the comment <regionid: 21: 2> and when this event touches the Region ID 21 it will run the common event 2.

A plugin of this type would help me a lot and give a bost to my game progress time!
Try the below code. You can either save it to a file yourself, or download it from here. Instead of a comment in the event page, it makes use of the Note box at the top (next to the Name). The format is <regionCommonEvent: x, y>, where x is the region ID and y is the common event ID.

Here's an example I made:
Spoiler: Sample
When it moves onto the first tile with region ID 21, it calls CE 2, which displays the text "I'm calling a common event!". It then moves onto another tile with the same region, so it doesn't execute again. When it moves to a tile without a region, then back onto a region 21 area, it calls the common event again.
XCtW8Vp.png

UnrulyAnnualLeafhopper.webp

Code:
/*:
* @plugindesc Allows events to have notes that indicate if they move to a region Id, they should call a common event or act as a page condition to auto-execute.
* @author Zevia
*
* @help In the event's note box (next to its name in the top), make a comment with
* the format <regionCommonEvent: x, y>, where x is the region ID and y is the
* number of the common event to call if the event moves onto the specified
* region ID.
*
* IMPORTANT: By default, this functionality is tied to Switch 15, so that it
* only works if that switch is ON. Ensure that when you want the common event
* functionality to work, you have turned the corresponding Switch ON. You can
* change the switch number in the Plugin parameters.
*
* Alternatively, if you put a comment at the top of a page's list of commands
* with the format <regionCondition: x>, where x is the region ID, then that
* page will execute whenever the event moves onto a tile with that ID.
*
* You can optionally add comma-separated regions to indicate that the page
* should run for any of those regions, such as <regionCondition: 1, 2, 3>
* indicating that the page condition applies to any of those regions.
*
* Note that a comment with the text "<regionCondition: x>" must be the first
* command at the top of a page. When using regionCondition, all other
* conditions for the page must also be met in order to execute that page's
* logic.
*
* In both cases, the event will only fire the first time the event enters
* an "area" of that region. As an example, if there are 5 tiles in a row
* with region ID 2 and the event steps on tile 1, the common event or
* page commands will run, but tiles 2, 3, 4, and 5 will not execute any
* logic until the event steps off a tile with that region ID and steps
* back on another tile with that region.
*
* If using comma-separated regions for a single page, each unique
* region ID is considered a separate "area" for the above functionality.
*
* @param switchNumber
* @text Enable Region Common Events
* @desc If this switch number is ON, then the common events for region IDs in the note box of an event will run.
* @type switch
* @default 15
*/

(function(module) {
    'use strict';

    module.Zevia = module.Zevia || {};
    var EventRegionCommonEvent = module.Zevia.EventRegionCommonEvent = {};
    var COMMENT_CODE = 108;

    var regionCommonEventSwitchNumber = parseInt(PluginManager.parameters('EventRegionCommonEvent').switchNumber);
    if (isNaN(regionCommonEventSwitchNumber)) {
        throw new Error(
            'Configuration Error: EventRegionCommonEvent requires a valid switch number to be' +
            ' specified in the setup parameters'
        );
    }

    EventRegionCommonEvent.initialize = Game_Event.prototype.initialize;
    Game_Event.prototype.initialize = function(mapId, eventId) {
        this._executedRegionEvents = {};
        EventRegionCommonEvent.initialize.call(this, mapId, eventId);
    };

    Game_Event.prototype.hasRunRegionEvent = function(regionId) {
        return this._executedRegionEvents[regionId];
    };

    Game_Event.prototype.regionCommonEvent = function() {
        if (!$gameSwitches.value(regionCommonEventSwitchNumber)) { return; }

        var regionCommonEvent = $dataMap.events[this._eventId].meta.regionCommonEvent;
        if (!regionCommonEvent) { return; }

        var regionAndEventIds = regionCommonEvent.split(',');
        if (regionAndEventIds.length !== 2) { return; }

        var regionId = this.regionId();
        if (regionId === parseInt(regionAndEventIds[0])) {
            if (!this._hasRunCommonEvent) {
                $gameTemp.reserveCommonEvent(parseInt(regionAndEventIds[1]));
                this._hasRunCommonEvent = true;
            }
        } else {
            this._hasRunCommonEvent = false;
        }
    };

    Game_Event.prototype.doesNotHaveRegionCondition = function(page) {
        var firstItem = page.list[0];
        return (firstItem.code !== COMMENT_CODE) || (firstItem.parameters[0].indexOf('regionCondition') === -1);
    };

    EventRegionCommonEvent.meetsConditions = Game_Event.prototype.meetsConditions;
    Game_Event.prototype.meetsConditions = function(page) {
        return EventRegionCommonEvent.meetsConditions.call(this, page) &&
            this.doesNotHaveRegionCondition(page);
    };

    Game_Event.prototype.regionConditions = function() {
        var pages = this.event().pages;
        var hasRegionPage = false;
        var regionId = this.regionId();
        for (var i = pages.length - 1; i >= 0; i--) {
            var currentPage = pages[i];
            if (!EventRegionCommonEvent.meetsConditions.call(this, currentPage)) { continue; }

            var firstItem = currentPage.list[0];
            if (firstItem.code !== COMMENT_CODE) { continue; }

            var regionCondition = firstItem.parameters[0].match(/<regionCondition:(.*)>/);
            if (!regionCondition) { continue; }

            var regionIds = regionCondition[1];
            if (!regionIds) { continue; }

            var allIds = regionIds.match(/\d+/g);
            if (!allIds) { continue; }

            for (var j = 0; j < allIds.length; j++) {
                if (regionId === parseInt(allIds[j])) {
                    hasRegionPage = true;
                    this._pageIndex = i;
                    this.setupPage();
                    break;
                }
            }
        }

        if (!hasRegionPage) {
            this._executedRegionEvents[regionId] = false;
        } else if (!this.hasRunRegionEvent(regionId)) {
            Object.keys(this._executedRegionEvents).forEach(function(id) {
                this._executedRegionEvents[id] = false;
            }.bind(this));
            this._executedRegionEvents[regionId] = true;
            this._interpreter = new Game_Interpreter();
            this._interpreter.setup(this.list(), this._eventId);
        }
    };

    EventRegionCommonEvent.update = Game_Event.prototype.update;
    Game_Event.prototype.update = function() {
        if (!this.isMoving()) {
            this.regionCommonEvent();
            this.regionConditions();
        }
        EventRegionCommonEvent.update.call(this);
    };
})(window);
Hello @Zevia! Many thanks for the production of the plugin!
It works exactly as expected!

But, I would love to know if there is a way to make a variation of the plugin or add one more command.

For example, when an event with the comment <regionid: 12> is on top of a Region ID 12, the event page where the comment is run.

It would be another resource that would help me a lot!
Thank you, I await your response!
And again, thank you!
So, as an example, to clarify:

An event has 3 pages. You put a comment at the top of page 2 that says "<regionid: 12>". If the event steps onto a Region ID 12 tile, then page 2 of the event runs.

Is that correct?
Hello @Zevia!

Yes, that's correct.
Thanks for the great support.
I've modified the link in the original post (the Github one) with the new code. You can also copy-paste from below.

You should now be able to add a comment to the top of a page with the format "<regionCondition: x>", where x is the region ID you want to be a page condition for that page to run. I think I've gotten it to work so that it should act like every other page condition, like switches, variables, etc., such that you can say, as an example, "This page will not run unless the event is on region ID 2 and switch 4 is on."

Code:
/*:
* @plugindesc Allows events to have notes that indicate if they move to a region Id, they should call a common event or act as a page condition to auto-execute.
* @author Zevia
*
* @help In the event's note box (next to its name in the top), make a comment with
* the format <regionCommonEvent: x, y>, where x is the region ID and y is the
* number of the common event to call if the event moves onto the specified
* region ID.
*
* IMPORTANT: By default, this functionality is tied to Switch 15, so that it
* only works if that switch is ON. Ensure that when you want the common event
* functionality to work, you have turned the corresponding Switch ON. You can
* change the switch number in the Plugin parameters.
*
* Alternatively, if you put a comment at the top of a page's list of commands
* with the format <regionCondition: x>, where x is the region ID, then that
* page will execute whenever the event moves onto a tile with that ID.
*
* You can optionally add comma-separated regions to indicate that the page
* should run for any of those regions, such as <regionCondition: 1, 2, 3>
* indicating that the page condition applies to any of those regions.
*
* Note that a comment with the text "<regionCondition: x>" must be the first
* command at the top of a page. When using regionCondition, all other
* conditions for the page must also be met in order to execute that page's
* logic.
*
* In both cases, the event will only fire the first time the event enters
* an "area" of that region. As an example, if there are 5 tiles in a row
* with region ID 2 and the event steps on tile 1, the common event or
* page commands will run, but tiles 2, 3, 4, and 5 will not execute any
* logic until the event steps off a tile with that region ID and steps
* back on another tile with that region.
*
* If using comma-separated regions for a single page, each unique
* region ID is considered a separate "area" for the above functionality.
*
* @param switchNumber
* @text Enable Region Common Events
* @desc If this switch number is ON, then the common events for region IDs in the note box of an event will run.
* @type switch
* @default 15
*/

(function(module) {
    'use strict';

    module.Zevia = module.Zevia || {};
    var EventRegionCommonEvent = module.Zevia.EventRegionCommonEvent = {};
    var COMMENT_CODE = 108;

    var regionCommonEventSwitchNumber = parseInt(PluginManager.parameters('EventRegionCommonEvent').switchNumber);
    if (isNaN(regionCommonEventSwitchNumber)) {
        throw new Error(
            'Configuration Error: EventRegionCommonEvent requires a valid switch number to be' +
            ' specified in the setup parameters'
        );
    }

    EventRegionCommonEvent.initialize = Game_Event.prototype.initialize;
    Game_Event.prototype.initialize = function(mapId, eventId) {
        this._executedRegionEvents = {};
        EventRegionCommonEvent.initialize.call(this, mapId, eventId);
    };

    Game_Event.prototype.hasRunRegionEvent = function(regionId) {
        return this._executedRegionEvents[regionId];
    };

    Game_Event.prototype.regionCommonEvent = function() {
        if (!$gameSwitches.value(regionCommonEventSwitchNumber)) { return; }

        var regionCommonEvent = $dataMap.events[this._eventId].meta.regionCommonEvent;
        if (!regionCommonEvent) { return; }

        var regionAndEventIds = regionCommonEvent.split(',');
        if (regionAndEventIds.length !== 2) { return; }

        var regionId = this.regionId();
        if (regionId === parseInt(regionAndEventIds[0])) {
            if (!this._hasRunCommonEvent) {
                $gameTemp.reserveCommonEvent(parseInt(regionAndEventIds[1]));
                this._hasRunCommonEvent = true;
            }
        } else {
            this._hasRunCommonEvent = false;
        }
    };

    Game_Event.prototype.doesNotHaveRegionCondition = function(page) {
        var firstItem = page.list[0];
        return (firstItem.code !== COMMENT_CODE) || (firstItem.parameters[0].indexOf('regionCondition') === -1);
    };

    EventRegionCommonEvent.meetsConditions = Game_Event.prototype.meetsConditions;
    Game_Event.prototype.meetsConditions = function(page) {
        return EventRegionCommonEvent.meetsConditions.call(this, page) &&
            this.doesNotHaveRegionCondition(page);
    };

    Game_Event.prototype.regionConditions = function() {
        var pages = this.event().pages;
        var hasRegionPage = false;
        var regionId = this.regionId();
        for (var i = pages.length - 1; i >= 0; i--) {
            var currentPage = pages[i];
            if (!EventRegionCommonEvent.meetsConditions.call(this, currentPage)) { continue; }

            var firstItem = currentPage.list[0];
            if (firstItem.code !== COMMENT_CODE) { continue; }

            var regionCondition = firstItem.parameters[0].match(/<regionCondition:(.*)>/);
            if (!regionCondition) { continue; }

            var regionIds = regionCondition[1];
            if (!regionIds) { continue; }

            var allIds = regionIds.match(/\d+/g);
            if (!allIds) { continue; }

            for (var j = 0; j < allIds.length; j++) {
                if (regionId === parseInt(allIds[j])) {
                    hasRegionPage = true;
                    this._pageIndex = i;
                    this.setupPage();
                    break;
                }
            }
        }

        if (!hasRegionPage) {
            this._executedRegionEvents[regionId] = false;
        } else if (!this.hasRunRegionEvent(regionId)) {
            Object.keys(this._executedRegionEvents).forEach(function(id) {
                this._executedRegionEvents[id] = false;
            }.bind(this));
            this._executedRegionEvents[regionId] = true;
            this._interpreter = new Game_Interpreter();
            this._interpreter.setup(this.list(), this._eventId);
        }
    };

    EventRegionCommonEvent.update = Game_Event.prototype.update;
    Game_Event.prototype.update = function() {
        if (!this.isMoving()) {
            this.regionCommonEvent();
            this.regionConditions();
        }
        EventRegionCommonEvent.update.call(this);
    };
})(window);
@Zevia
Thanks for making this!!
I will test, and edit this post with response.
@JulioManeiiro I discovered some bugs with the new regionCondition functionality conflicting with the common event note box functionality, as well as realizing that the original file was misnamed (had "commentEvent" instead of "commonEvent").

I've updated the raw file on Github, so you can re-download it here, or you can copy from the code block below.

I also updated the functionality for regionConditions to be able to work on a comma-separated list of regions, so that if you do something like
Code:
<regionCondition: 21, 22, 23, 24>
as a comment at the top of a page, that page will run for all 4 of those regions.

I additionally added a switch parameter to the Plugin parameters where the common event functionality (from putting a note in the note box) only runs if the specified switch is on, though it means the file must be named exactly "EventRegionCommonEvent.js" now.

Let me know if you run into any issues.

Code:
/*:
* @plugindesc Allows events to have notes that indicate if they move to a region Id, they should call a common event or act as a page condition to auto-execute.
* @author Zevia
*
* @help In the event's note box (next to its name in the top), make a comment with
* the format <regionCommonEvent: x, y>, where x is the region ID and y is the
* number of the common event to call if the event moves onto the specified
* region ID.
*
* IMPORTANT: By default, this functionality is tied to Switch 15, so that it
* only works if that switch is ON. Ensure that when you want the common event
* functionality to work, you have turned the corresponding Switch ON. You can
* change the switch number in the Plugin parameters.
*
* Alternatively, if you put a comment at the top of a page's list of commands
* with the format <regionCondition: x>, where x is the region ID, then that
* page will execute whenever the event moves onto a tile with that ID.
*
* You can optionally add comma-separated regions to indicate that the page
* should run for any of those regions, such as <regionCondition: 1, 2, 3>
* indicating that the page condition applies to any of those regions.
*
* Note that a comment with the text "<regionCondition: x>" must be the first
* command at the top of a page. When using regionCondition, all other
* conditions for the page must also be met in order to execute that page's
* logic.
*
* In both cases, the event will only fire the first time the event enters
* an "area" of that region. As an example, if there are 5 tiles in a row
* with region ID 2 and the event steps on tile 1, the common event or
* page commands will run, but tiles 2, 3, 4, and 5 will not execute any
* logic until the event steps off a tile with that region ID and steps
* back on another tile with that region.
*
* If using comma-separated regions for a single page, each unique
* region ID is considered a separate "area" for the above functionality.
*
* @param switchNumber
* @text Enable Region Common Events
* @desc If this switch number is ON, then the common events for region IDs in the note box of an event will run.
* @type switch
* @default 15
*/

(function(module) {
    'use strict';

    module.Zevia = module.Zevia || {};
    var EventRegionCommonEvent = module.Zevia.EventRegionCommonEvent = {};
    var COMMENT_CODE = 108;

    var regionCommonEventSwitchNumber = parseInt(PluginManager.parameters('EventRegionCommonEvent').switchNumber);
    if (isNaN(regionCommonEventSwitchNumber)) {
        throw new Error(
            'Configuration Error: EventRegionCommonEvent requires a valid switch number to be' +
            ' specified in the setup parameters'
        );
    }

    EventRegionCommonEvent.initialize = Game_Event.prototype.initialize;
    Game_Event.prototype.initialize = function(mapId, eventId) {
        this._executedRegionEvents = {};
        EventRegionCommonEvent.initialize.call(this, mapId, eventId);
    };

    Game_Event.prototype.hasRunRegionEvent = function(regionId) {
        return this._executedRegionEvents[regionId];
    };

    Game_Event.prototype.regionCommonEvent = function() {
        if (!$gameSwitches.value(regionCommonEventSwitchNumber)) { return; }

        var regionCommonEvent = $dataMap.events[this._eventId].meta.regionCommonEvent;
        if (!regionCommonEvent) { return; }

        var regionAndEventIds = regionCommonEvent.split(',');
        if (regionAndEventIds.length !== 2) { return; }

        var regionId = this.regionId();
        if (regionId === parseInt(regionAndEventIds[0])) {
            if (!this._hasRunCommonEvent) {
                $gameTemp.reserveCommonEvent(parseInt(regionAndEventIds[1]));
                this._hasRunCommonEvent = true;
            }
        } else {
            this._hasRunCommonEvent = false;
        }
    };

    Game_Event.prototype.doesNotHaveRegionCondition = function(page) {
        var firstItem = page.list[0];
        return (firstItem.code !== COMMENT_CODE) || (firstItem.parameters[0].indexOf('regionCondition') === -1);
    };

    EventRegionCommonEvent.meetsConditions = Game_Event.prototype.meetsConditions;
    Game_Event.prototype.meetsConditions = function(page) {
        return EventRegionCommonEvent.meetsConditions.call(this, page) &&
            this.doesNotHaveRegionCondition(page);
    };

    Game_Event.prototype.regionConditions = function() {
        var pages = this.event().pages;
        var hasRegionPage = false;
        var regionId = this.regionId();
        for (var i = pages.length - 1; i >= 0; i--) {
            var currentPage = pages[i];
            if (!EventRegionCommonEvent.meetsConditions.call(this, currentPage)) { continue; }

            var firstItem = currentPage.list[0];
            if (firstItem.code !== COMMENT_CODE) { continue; }

            var regionCondition = firstItem.parameters[0].match(/<regionCondition:(.*)>/);
            if (!regionCondition) { continue; }

            var regionIds = regionCondition[1];
            if (!regionIds) { continue; }

            var allIds = regionIds.match(/\d+/g);
            if (!allIds) { continue; }

            for (var j = 0; j < allIds.length; j++) {
                if (regionId === parseInt(allIds[j])) {
                    hasRegionPage = true;
                    this._pageIndex = i;
                    this.setupPage();
                    break;
                }
            }
        }

        if (!hasRegionPage) {
            this._executedRegionEvents[regionId] = false;
        } else if (!this.hasRunRegionEvent(regionId)) {
            Object.keys(this._executedRegionEvents).forEach(function(id) {
                this._executedRegionEvents[id] = false;
            }.bind(this));
            this._executedRegionEvents[regionId] = true;
            this._interpreter = new Game_Interpreter();
            this._interpreter.setup(this.list(), this._eventId);
        }
    };

    EventRegionCommonEvent.update = Game_Event.prototype.update;
    Game_Event.prototype.update = function() {
        if (!this.isMoving()) {
            this.regionCommonEvent();
            this.regionConditions();
        }
        EventRegionCommonEvent.update.call(this);
    };
})(window);

Original thread: https://forums.rpgmakerweb.com/threads/run-common-event-when-event-stay-in-specific-region.109579/