MZ Check if the player has entered/is staying/has exited an event tile.

Hello, this is the first plugin I have developed for RPG Maker MZ.

the Idea:
to return true or false if the player entered/is staying/has exited an event tile.
with the support of checking multiple events.
with this plugin, you have the ability to check OnEnter, IsStaying, and OnExit against the players x and y.

currently, it does not save those data to the save files. not sure if will be needed or not...

it is a STAND-ALONE script and should work with any plugin.

remakes:
I haven't tested it out yet, but in theory, one could check on an event while on a different map, so it might be useful for someone or not. however, I have provided a command call that will "release" all those memories.

also, I'm not sure if you need to check the event against another event... but as long as the object has x and y, it could be checked... in theory.

currently, considering this in the final testing phase, but feel free to give more ideas or suggestions.
thank you!

I know there are sensors plugin for the door and range, but the way onEnter
and onExit you used for the door that is, looks nice and can be helpfull.

there is no link, but I would love to test on the MV version if it works or modify
for my own needs in there.

Looks helpful in some ways.
I know there are sensors plugin for the door and range, but the way onEnter
and onExit you used for the door that is, looks nice and can be helpfull.

there is no link, but I would love to test on the MV version if it works or modify
for my own needs in there.

Looks helpful in some ways.
sure thing!, I can share the code but it is still WIP. check the spoilers, it shouldn't be hard to set up I think. but not sure if could work on MV or not, it is basic stuff, so I think you would be fine. modify it if needed.

Spoiler: setup images
the sensors are the 4 tiles around the door, the one next to the door is just there to block the player, both of them have no event or script.

Screenshot 2023-11-15 140453.png
Screenshot 2023-11-15 140538.png

Spoiler: The Script
JavaScript:
//=============================================================================
// CheckPlayerAndEventCoord.js (ver.0.0.1)
//=============================================================================

/*:
 * @target MZ
 * @plugindesc Check if the Player is sharing the same X and Y coordination
 * with the event on three flags. OnEnter, OnStay, and OnExit. will return true or false.
 * for any questions, contact Dayner on RPG Maker Forums or daynerkurdi on Discord.
 * @author Dayner Kurdi
 *
 * @help
 * Check if the Player Enter the same "tile" as the event on each frame, there are three flags available to check.
 * it is possible to check for multiple events.
 *
 * the data is not saved!
 *
 * ***************
 * CHECKINH FLAGS:
 * ***************
 *
 * CheckEventCoordFlag(eventId, flagId)
 *
 * ============================================================================
 * How To Use
 * ============================================================================
 * SIMPLE
 * on the "Event Commend" --> on "Flow Control" --> "Conditional Branch" --> "Scrpit"
 * on text prompt, type the following:
 *
 * CheckEventCoordFlag(eventId, flagId)
 *
 * where flagId is:
 *
 * 0 for Player Has Entered the event tile, it will return true ONCE on the frame the player entered.
 * afterword will return false.
 *
 * 1 for Player is staying on the event tile, it will return true as long as the player is standing on the event.
 * otherwise false.
 *
 * 2 for Player Has exited the event tile, it will return true ONCE on the frame the player exited.
 * afterword will return false.
 *
 * ADVANCE
 * vie Script
 *
 * [----- NOTE ------]
 * 1- on eventId, entering unregistered eventId WILL RETURN FALSE.
 * 2- on flagId, entering numbers other than 0,1,2 WILL RETURN FALSE.
 *
 * ***************
 * COMMANDS
 * ***************
 *
 * StartOnEnterCheck (initialOnCheckEventId)
 * --------------------------------------------
 * start checking at the event using an EventId.
 *
 *
 * EndtOnEnterCheck: 
 * ---------------------------------------------
 * Stop checking for the player, and release the ALL events FLag memories.
 *
 * [----- NOTE ------]
 * must be used when moving to a different map, we need to clear the flag memories since they still exist and can be checked even on a different map.
 *
 *
 *@command StartPlayerOnEventCheck
 *@text Start Player On Event Check at the event.
 *@desc Start checking if the player enters the event X and Y coordination.
 *
 *@arg initialOnCheckEventId
 *@type number
 *@min 1
 *@text Initial On Check Event Id
 *@desc the Event Id where the checking for the player will occur.
 *
 *
 *@command EndPlayerOnEventCheck
 *@text End Player On Event Check at the event.
 *@desc Stop checking for the player, and release ALL events FLag memories
 *
 */

 // code
 
 // Struct Factory
 const KurdiFactoryStruct = (...keys) => ((...value) => keys.reduce((obj, key, index) => {obj[key] = value[index]; return obj} , {}))

 const eventCoordStruct = KurdiFactoryStruct('EventId', 'OnEnter', 'OnStay', 'OnExit')

 var playerCoordCheckFlag = [];

 //exposing the code

 function CheckEventCoordFlag(eventId, flagId)
    {
        for(i = 0; i < playerCoordCheckFlag.length; i++)
        {
            if(eventId == playerCoordCheckFlag[i].EventId)
            {
                switch(flagId)
                {
                    case 0:
                    {
                        return playerCoordCheckFlag[i].OnEnter;
                    }
                    break;

                    case 1:
                    {
                        return playerCoordCheckFlag[i].OnStay;
                    }
                    break;

                    case 2:
                    {
                        return playerCoordCheckFlag[i].OnExit;
                    }
                    break;

                    default:
                    {
                        return false;
                    }

                }
            }
        }
      
        return false;
    }

//commend codes
 (() => {

    const pluginName = "CheckPlayerAndEventCoord";

    let playerLastFrameX = null;
    let PlayerLastFrameY = null;

    PluginManager.registerCommand(pluginName, "StartPlayerOnEventCheck", args => {

        CheckPlayerVsEventOverlay(args);
    });

    PluginManager.registerCommand(pluginName, "EndPlayerOnEventCheck", args => {

        ResetFlags();
    });

    function CheckPlayerVsEventOverlay(args)
    {
        var eventIdExist = false;
        var checkingEvent = eventCoordStruct(args.initialOnCheckEventId,false,false,false);

        for(i = 0; i < playerCoordCheckFlag.length; i++)
        {
            if(args.initialOnCheckEventId == playerCoordCheckFlag[i].EventId)
            {
                eventIdExist = true;
                checkingEvent = playerCoordCheckFlag[i];
                break;
            }
        }

        if(!eventIdExist)
        {
            playerCoordCheckFlag.push(checkingEvent);
        }

        //Get Event Data
        let eventId = checkingEvent.EventId;
        let eventX = $gameMap.event(eventId)._x;
        let eventY = $gameMap.event(eventId)._y;

        //Get Player Data
        let playerX = $gamePlayer.x;
        let playerY = $gamePlayer.y;

        if(playerX == eventX && playerY == eventY)
        {
            if(playerLastFrameX == null && PlayerLastFrameY == null)
            {
                // player just entered the event tile
                checkingEvent.OnEnter = true;
                checkingEvent.OnStay = false;
                checkingEvent.OnExit = false;
                //console.log("Player has Entered the Tile Event");

            }
            else if(playerLastFrameX == playerX & PlayerLastFrameY == playerY)
            {
                // player is overlaying the event tile
                checkingEvent.OnEnter = false;
                checkingEvent.OnStay = true;
                checkingEvent.OnExit = false;
                //console.log("Player is overlaying the Tile Event");
            }

            //cache the player current coord
            playerLastFrameX = playerX;
            PlayerLastFrameY = playerY;
        }
        else if(playerLastFrameX != null && PlayerLastFrameY != null)
        {
            if(playerLastFrameX == eventX & PlayerLastFrameY == eventY)
            {
                // player has exited the event tile
                checkingEvent.OnEnter = false;
                checkingEvent.OnStay = false;
                checkingEvent.OnExit = true;
                playerLastFrameX = null;
                PlayerLastFrameY = null;
                //console.log("Player has exited the Tile Event");
            }
        }
        else
        {
            checkingEvent.OnEnter = false;
            checkingEvent.OnStay = false;
            checkingEvent.OnExit = false;
        }
    }

    function ResetFlags()
    {
        // releseing the memories
        playerCoordCheckFlag = [];
        //console.log("Reseting Flag");
    }
 })();
to see the plugin, it can work on MV without a problem, except,
the plugin commands wont work, I could turn this into MV plugin
commands, without a problem, and not sure what the plugin command
does on top of the event (which is different per eventID where you check 3,
the other require initialize check Id of the current eventID.

but scriptcalls should work as they arent really targeting anything,
will report after testing, will be back later though, so you hear the results 🙂
oh that, that the sensors event Id, the script is checking event 3 (top right), 4 (top left), 5 (bottom right) and 6 (bottom left)
after small testing on scriptcalls, there is no problem in MV version =)
but it works nicely if you ask me, I see a way I could use it, but I need
to change my mechanic for it as it is on action trigger atm, but I change it
if I may use it in my project =)

I love this plugin, so I can save some things that are linked to it, which also
give me some other idea's to save other things and new mechanics to think
about how to use it more 🙂 (specially switch related).
glad you like it, feel free to use it in your project, but keep me updated if there are any problems, I cant wait and see how could you use it!
I way is a charge system for the player, as it uses some complex mechanic to it,
but this can simplify the mechanic of it, while standing on it and stops when you
move you which has currently 3 events around, so I can control this with 1 event.

other mechanic I had in mine, is using a switch, and with the new plugin recently
released by TSR, I can make a cool mechanic with the JumpSystem, as well for
another plugin (not public), I can raise stuff, and lower them timely while this plugin
control the "onEnter", "onExit", while you idle on the switch to raise again.

there are other things come to mind on rotating while standing and on exit,
where you need to time nicely as they rotate back, if done correctly, you solve
the puzzle, but idle, onEnter, onExit can be tricky on the this puzzle mechanic.

but while I got a puzzle mechanic that rotate, its a bit complex, so recode this
part using this plugin might work just the way I need it to work 😀

as this is still a bit buggy and not exactly 100% working how I want it too,
but this is probably also 1 of the most complex puzzle I'm building 🙂

so for me, it can be really useful, so thank you for making the plugin, even in WIP,
it sure works nice already ^^

btw, there are 2 typo's in the helpfile, on scriptcall part (eventID, flagid)
you have endt.... (t to much), and scrpit a further up instead of script.
I see. Hope it help you.

The main idea mostly that onEnter and onExit are only occurred once to get a “ perfect timing” to start other complex systems. Like catching the moment the player press a button or after entering a tile.

that is why it turned false on next frame, you need to use is staying instead.

Or to use your jumping system as example.

If the player is jumping and has entered, turn on a switch
you need both to activate and stay to make it work nicely:

CheckEventCoordFlag(this._event(), 0) && CheckEventCoordFlag(this._event(), 1)

this make it activate once, and if they stay, it raises, but if you combine it differently,
it can be much more powerful as well for use, just a combination of some other plugins ^^
but there are interesting idea's I want to test with rotating, so rotate, run, press, run, etc,
as they leave, you need to the other 1, so I can experiment some things, for now, it doesn't
break what I test or crash.

but it makes some mechanics interesting.
ok, more feedback is good, thank you very much.
going to make a demo project tomorrow.
got an idea, but not sure if it is useful or not.

do you want me to add "from where" the player enters the tile? (up, down, right, left)
also, do you want me to check if the player was dashing or not? (true,false)
Also, I could add "how long" the player was standing on the tile (by frame)
while dashing is nice to check, as if they dash (true) it wont activated.
that comes in handy as for the other one (in which direction) U, L, R, D
make that optional, default false.

so you can do,: up: true, and rest is automatically false.
but if you can do the dash to activate the event or not, it would be helpful
as I have a mechanic that I have "if its dashing" so if it can be simplified,
I take that option 😀

Original thread: https://forums.rpgmakerweb.com/threads/check-if-the-player-has-entered-is-staying-has-exited-an-event-tile.162954/