Randomly transfer player inside the same map

Hello everyone,
first thing first, I hope this post is not in the wrong section as I need help specifically with MV and if it isn't I'm sorry, it was not intentional.

What I'm trying to do is creating a "trap" kind of event that produces a subtle visual and auditory cue before "triggering", basically playing an animation on its own tile and checking if the player is there. If the player's X and Y correspond with the event's X and Y, I want it to move the player to another tile inside the same map.
Obviously, I can't simply do Transfer player on the current map with two randomly rolled position because I can end up with unwalkable tiles.
I assume that there's some script call to do:
newX = Math.floor(Math.random() * (mapSizeX + 1));
newY = Math.floor(Math.random() * (mapSizeY + 1));
while (!map.isWalkable(newX, newY) {
newX = Math.floor(Math.random() * (mapSizeX + 1));
newY = Math.floor(Math.random() * (mapSizeY + 1));
}
transferPlayer(newX, newY);

Of course, I know that these are not actual methods from RPGMV, so, is there some way to achieve what I want without recurring to writing my own checker function?
Unfortunately, transferring the player to a static set of position is not really possible because I'm planning to call this common event from multiple maps which might have very different layout.
If you want to do it like that, I think it's:

let newX = Math.randomInt($dataMap.width);
let newY = Math.randomInt($dataMap.height);

while (!$gamePlayer.canPass(newX, newY, 2) && !$gamePlayer.canPass(newX, newY, 4) && !$gamePlayer.canPass(newX, newY, 8) && !$gamePlayer.canPass(newX, newY, 6)) {
newX = Math.randomInt($dataMap.width);
newY = Math.randomInt($dataMap.height);
}

$gamePlayer.reserveTransfer($gameMap.mapId(), newX, newY, 8, 2);


EDIT: Don't know if your idea is a good way of doing it though. I tested it and the FPS can take some hit.

EDIT2: Also, I'm not a mapping expert, but isn't it the case that if you start inside walls/ceilings/whatever, you can sometimes walk along them? So even with the checks you could maybe be thrown in those spots, unless you block them with invisible impassable tiles or plugins.
Thank you for your reply!
EDIT2: Also, I'm not a mapping expert, but isn't it the case that if you start inside walls/ceilings/whatever, you can sometimes walk along them? So even with the checks you could maybe be thrown in those spots, unless you block them with invisible impassable tiles or plugins.
Neither am I LOL, that's the first time I try to do something "big" on RPG Maker, so I assume I'm forgetting a thing here and there, now that you made me thing about it, yes, I think this could be the case, maybe with "roof" tiles as they are flagged this way by the engine to allow (what I assume is) multi-level maps.
How would you do the check then? Unfortunately, I feel like simplyfing it with a set of static positions would be counterproductive as it would mean more work on my side as a dev.

Would using regions be easier? I'm no expert with them, but something like flagging all the places in the dungeon that are meant to be walked on with a region and then having JS check if the chosen tile by the RNG has that specific region be viable?
Yeah, that works.

It'd probably be better practice to set it up so that it can only check a certain tile one time though. Or scan through all tiles, store the ones with the region and pick a random from those.

There's already available event spawner plugins that can target a region id. Just have those throw an empty event to a region and transfer the player to that location LOL.
This seems pretty clever actually and could save me a couple of headache with implementing the feature, as it's for my submission to the current jam and I think writing it up from scratch may be a pretty long task. Thank you very much for your help.
As the question has been answered, do I need to mark the post as solved or something like that? How do I do it? I forgot if it's a thing here lol

Edit: maybe after the official RPGM Game Jam for 2025 is over I'll make it a plugin, idk if it's something others would use but it's a nice challenge 🙂
Cool.
Yeah I think you can report your post to the mods and put reason 'solved' in there.

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/randomly-transfer-player-inside-the-same-map.176744/