Plugin to enable creating arbitrary trigger zones

I've created a plugin, which is compatible with both MV and MZ, that enables you to create trigger zones using a single event rather than having to duplicate a bunch of events in order to achieve the same effect, this is useful for things like map transfers, interact-able counters, and cutscene triggers.

I've implemented it using the Event's note field to smuggle JSON into the plugin to be parsed with a very simple structure:
JSON:
{
    "extentX": 2,
    "extentY": 2,
    "triggerOnce":true
}

The extents are defined in tiles defining a rectangle that radiates out from the event's position (the event is the center), the way the trigger's area is calculated favors growing up-left first, so a 2x2 event will end with the event being in the bottom-right, while a 4x4 will have the event in the center, this allows you to create trigger volumes in a very predictable way that is easy to calculate, simply count the tiles to either side of the event you wish to use as the center.

I've released the code under the MIT license so you are free to use it how you wish, even commercially, just make sure to attribute me in some fashion

JavaScript:
/*:
 * @target MZ
 * @target MV
 * @plugindesc Allows events to be defined as a rectangle with extents relative to the event's tile position
 * @author Phillip "Antidote" Stephens
 *
 * @license MIT
 * @help AntiTriggerRectEvent.js
 *
 * This plugin enables the ability to create an arbitrary rectangle from an event's position
 * the extents are defined as the total number of tiles surrounding the event.
 *
 * To define a rectangle simply add this following JSON struct with the extentX/extentY values changed for the desired
 * rectangle to the event's notes field
 *
 * {"extentX":desired_width_in_tiles,"extentY":desired_height_in_tiles,"triggerOnce":desired_behavior}
 *
 * The following is a working example that defines a 2x2 tile strip starting from the event's x/y position,
 * it also only triggers once meaning the event will activate only once
 * {"extentX":2,"extentY":2,"triggerOnce":true}
 *
 * When calculating the rect we favor the building the rect upwards and leftwards,
 * so it'll add a tile up then down, and left then right.
 *
 * As a visual aide this is what the above rectangle would look like:
 *
 * +----+----+
 * |  1 |  2 |
 * +----+----+
 * |  3 | EV |
 * +----+----+
 *
 */

(() => {
`use strict`;

    const _Game_Event_initialize = Game_Event.prototype.initialize;
    Game_Event.prototype.initialize = function(mapId, eventId) {
_Game_Event_initialize.call(this, mapId, eventId);
        this._triggerExtentY = 0;
        this._triggerExtentY = 0;
        this._triggered = false;
        this._triggerOnce = false;
        const note = $dataMap.events[eventId].note;
        if (note !== "") {
let rect = JSON.parse($dataMap.events[this._eventId].note) || null;
            this._triggerExtentX = rect.extentX || 0;
            this._triggerExtentY = rect.extentY || 0;
            this._triggerOnce = rect.triggerOnce || false;
        }
    }

Game_Event.prototype.pos = function(x, y) {
if (this._triggerExtentX === 0 || this._triggerExtentY === 0) {
return Game_CharacterBase.prototype.pos.call(this, x, y);
        }

/**
         * When calculating the rect we favor the building the rect upwards and leftwards,
         * so it'll add a tile up then down, and left then right
         */
        const halfExtentX1 = Math.max(0, Math.ceil((this.triggerExtentX() / 2) - .5));
        const halfExtentY1 = Math.max(0, Math.ceil((this.triggerExtentY() / 2) - .5));
        const halfExtentX2 = Math.max(0, Math.floor((this.triggerExtentX() / 2) - .5));
        const halfExtentY2 = Math.max(0, Math.floor((this.triggerExtentY() / 2) - .5));
        const startX = Math.max(0, this._x - halfExtentX1);
        const startY = Math.max(0, this._y - halfExtentY1);
        const endX = Math.min($gameMap.width() - 1, this._x + halfExtentX2);
        const endY = Math.min($gameMap.height() - 1, this._y + halfExtentY2);
        //console.log("Trigger Rect startX %i, startY %i, endX %i, endY %i, halfX1 %i, halfY1 %i, halfX2 %i, halfY2 %i", startX, startY, endX, endY, halfExtentX1, halfExtentY1, halfExtentX2, halfExtentY2);

        if (this._triggered && this._triggerOnce) {
return false;
        }

return (x >= startX && y >= startY && x <= endX && y <= endY);
    }

const _Game_Event_start = Game_Event.prototype.start;
    Game_Event.prototype.start = function() {
_Game_Event_start.call(this);

        if (this.isTriggerIn([0, 1, 2])) {
this._triggered = true;
        }
    }

Game_Event.prototype.triggerExtentX = function() {
return this._triggerExtentX;
    }

Game_Event.prototype.triggerExtentY = function() {
return this._triggerExtentY;
    }
})();
Linux Demo
Windows Demo
Welcome!

Thanks for sharing your work. Just a couple of thoughts for posterity:
- This is commonly referred to in RPG Maker as modifying the event's hitbox.

- It seems best practice would be to leave the triggered portion in the event like always - if it should only happen once, the user has event commands to activate a second blank page with a self switch or uses Erase Event or some such. Adding your own property to the event which isn't accessible via the editor could create confusion and unexpected behavior.

- To keep things standardized with other plugins people would use, you can enclose your information in <angled brackets> and retrieve it with the event's meta property, rather than requiring the user to learn and use JSON syntax.

Thanks for the contribution.
Thanks for sharing your work. Just a couple of thoughts for posterity:
- This is commonly referred to in RPG Maker as modifying the event's hitbox.
This seems like a gross oversimplification of what my particular plugin is doing, since events don't have hitboxes in RPG Maker,
all events simply use their x,y positions in tiles not in pixels.

- It seems best practice would be to leave the triggered portion in the event like always - if it should only happen once, the user has event commands to activate a second blank page with a self switch or uses Erase Event or some such. Adding your own property to the event which isn't accessible via the editor could create confusion and unexpected behavior.
I think you're misunderstanding the purpose of the triggerOnce value, it's not for saving the state, it's there as a temporary, you can and should still use things like self switches, Erase Event, Exit Event Processing, and blank pages as per normal. The point of triggerOnce is to ensure it only happens once per map session, but if you think it should be, I can remove that behavior, it would simplify the plugin a little bit. However I was looking into implementing a way to reset that via event commands, and if I were to require erasing the event to prevent multiple triggers I couldn't implement that.

- To keep things standardized with other plugins people would use, you can enclose your information in <angled brackets> and retrieve it with the event's meta property, rather than requiring the user to learn and use JSON syntax.
I'm definitely open to this if you can give an example on how one would handle this, I'm a programmer so my immediate instinct was to use a standard way to structure data that's directly supported by the language, but I can definitely see where someone might be reluctant to mess around with JSON if they're not used to it.

Thanks for the contribution.
No problem!
This seems like a gross oversimplification of what my particular plugin is doing, since events don't have hitboxes in RPG Maker,
all events simply use their x,y positions in tiles not in pixels.
No one said anything about pixels, I'm telling you the common terminology for this is event hitboxes.


The point of triggerOnce is to ensure it only happens once per map session
That's the functionality of the Erase Event event command.

I'm definitely open to this if you can give an example on how one would handle this. I'm a programmer so my immediate instinct was to use a standard way to structure data that's directly supported by the language
The problem is it's not really supported by the software, as your method looks like it would fail from people using the actual intended method from other plugins 😉 (that is, people having notetags from other plugins in an event would cause your parsing the entire notes field as JSON to generate an error)

No one said anything about pixels, I'm telling you the common terminology for this is event hitboxes.
No need to be hostile, I was simply stating my position on the use of the terms, I'm used to Axis Aligned and Oriented bounding boxes where the hitboxes concretely defined as volumes of pixels or game units, not tile indices. I'm a C/C++ programmer normally, but got interested in an RPGM project recently and was developing something I felt was useful.

These are all good references, but as I note further down they're unfortunately not usable for me, the first is even a close match to my plugin, but definitely not quite the same, especially with the features I plan on adding in the future.

That's the functionality of the Erase Event event command.
I noticed you didn't quote the full text, so you probably missed my edit, sorry about that, but I further explain my reasoning about wanting to implement a way to re-enable the trigger, which requiring Erase Event would prevent from working.

The problem is it's not really supported by the software, as your method looks like it would fail from people using the actual intended method from other plugins 😉 (that is, people having notetags from other plugins in an event would cause your parsing the entire notes field as JSON to generate an error)
I really don't want to require another plugin for my plugin then, unless it's a core engine feature I can't use it. Am I misunderstanding what you're saying? Or will I need another plugin for notetags? I've read the documentation multiple times but I'm having trouble finding any material on how to support plugins beyond the basic 2 liner hidden away in a single page. And the examples aren't much help beyond the basics either.

EDIT:
I found out if it was core engine by searching in the source code, wow that was insanely obtuse, lol.

I'll update the plugin to use notetags instead, that's definitely the better way to do it.
No need to be hostile
I don't see how any words I used were hostile. I told you the term used is hitboxes and you started talking about pixels which I didn't mention instead of looking up "RPG Maker hitboxes" - so I provided you with links.

But if that's the way you want to be, then good luck with everything.
I don't see how any words I used were hostile. I told you the term used is hitboxes and you started talking about pixels which I didn't mention instead of looking up "RPG Maker hitboxes" - so I provided you with links.

But if that's the way you want to be, then good luck with everything.
I think we're talking over each other, all I did was explain my understanding based on previous experience, I'm not sure what you mean by "if that's the way you want to be," I was genuinely asking for help and further elaborating on my position. If you can't see how your words could be taken as hostile then that's an issue you need to resolve on your own.

I figured out what I needed to do and I genuinely appreciate you pointing me in the right direction, however you really need to reflect on how an outside party could interpret what you said in response to what I was saying. All I was asking for was to be pointed in the right direction, documentation, example code, anything that could help.

Original thread: https://forums.rpgmakerweb.com/threads/plugin-to-enable-creating-arbitrary-trigger-zones.173783/