MV Easiest way to transfer an event to the player location.

Now that I have baited you with a thread title that anyone could answer if they've used the engine for a couple days, let me add the the requirement for this is to also make sure the event isn't going to be stuck inside a wall or otherwise inaccessible.

I could of course transfer the event right on top of the player since that's a surefire way to have it on an accessible tile, but I wanted to see what methods are easy to implement to have it spawn near the player, (simply because it'll look better and the player won't have to leave the tile and step back on it to interact)

I might also add that I can't really use regions, since they are occupied otherwise.

If there's no easy and quick way to make this, I'll just opt to transfer it on top of the player.
Make a Script Call and paste this:

JavaScript:
var points = [ ];

for (let x = $gamePlayer.x - 2; x <= $gamePlayer.x + 2; x++) {
    for (let y = $gamePlayer.y - 2; y <= $gamePlayer.y + 2; y++) {
        if ($gameMap.isValid(x, y) && $gameMap.isPassable(x, y, 0)) {
            points.push({ x, y });
        }
    }
}

if (points.length > 0) {
    const randomIndex = Math.floor(Math.random() * points.length);
    const randomPoint = points[randomIndex];
    
    $gameMap.event(42).locate(randomPoint.x, randomPoint.y);
}
Replace 42 with your event ID. You can also play around with the 2s in the upper section to adjust the ranges.
If there's no spot available, nothing happens.
Make a Script Call and paste this:

JavaScript:
var points = [ ];

for (let x = $gamePlayer.x - 2; x <= $gamePlayer.x + 2; x++) {
    for (let y = $gamePlayer.y - 2; y <= $gamePlayer.y + 2; y++) {
        if ($gameMap.isValid(x, y) && $gameMap.isPassable(x, y, 0)) {
            points.push({ x, y });
        }
    }
}

if (points.length > 0) {
    const randomIndex = Math.floor(Math.random() * points.length);
    const randomPoint = points[randomIndex];
 
    $gameMap.event(42).locate(randomPoint.x, randomPoint.y);
}
Replace 42 with your event ID. You can also play around with the 2s in the upper section to adjust the ranges.
If there's no spot available, nothing happens.

Hmm, thanks for that! Maybe I should've clarified that somewhere, but I can't have nothing happening. Basically this is for the exit to appear near the player, so it'll have to absolutely appear somewhere near him.

Tho If I understand this right, this script would check every tile around the player in a range of 2, check if any of them is passable, then locate the event onto one of them at random? In that case I would think there will always be passable tiles somewhere and it probably won't need any sort of fail-safe...
JavaScript:
var points = [ ];

for (let x = $gamePlayer.x - 2; x <= $gamePlayer.x + 2; x++) {
    for (let y = $gamePlayer.y - 2; y <= $gamePlayer.y + 2; y++) {
        if ($gamePlayer.canPass(x, y, 0)) {
            points.push({ x, y });
        }
    }
}

var event = $gameMap.event(42);

if (event) {
    if (points.length > 0) {
        const randomIndex = Math.floor(Math.random() * points.length);
        const randomPoint = points[randomIndex];
       
        event.locate(randomPoint.x, randomPoint.y);
    } else {
        event.locate($gamePlayer.x, $gamePlayer.y);
    }
}
Now, the event will be positioned right on the player's position when there's no passable spot. You can also tweak a little bit, e.g. write $gamePlayer.y - 1 in the method call to make it appear above the player - it's not rocket science.
JavaScript:
var points = [ ];

for (let x = $gamePlayer.x - 2; x <= $gamePlayer.x + 2; x++) {
    for (let y = $gamePlayer.y - 2; y <= $gamePlayer.y + 2; y++) {
        if ($gameMap.isValid(x, y) && $gameMap.isPassable(x, y, 0)) {
            points.push({ x, y });
        }
    }
}

var event = $gameMap.event(42);

if (event) {
    if (points.length > 0) {
        const randomIndex = Math.floor(Math.random() * points.length);
        const randomPoint = points[randomIndex];
       
        event.locate(randomPoint.x, randomPoint.y);
    } else {
        event.locate($gamePlayer.x, $gamePlayer.y);
    }
}
Now, the event will be positioned right on the player's position when there's no passable spot. You can also tweak a little bit, e.g. write $gamePlayer.y - 1 in the method call to make it appear above the player - it's not rocket science.
It's still black magic to me, haha! Thanks, mate!
Hmmm like that even if you set "range" to 1 can't the exit appear diagonally unreachable if the player is blocked by rocks of whatever?

You could also check for $gamePlayer.canPass and manipulate the coordinate accordingly.
1780559898185.png

Hmm. Looks like that didn't quite work out. I've not changed the code outside of the event ID.

@rpgLord69 How'd that look code wise?
True, I totally forgot about $gamePlayer.canPass, it's slightly more advanced.
I updated my prior post!
With the new code:
1780560199991.png



Ahh, I see what it is... The black tile is not passable, but the little rock border of the wall technically is, but has directional blocks. Hmm. Would this code work if I put region restrictions on it?
@rpgLord69 How'd that look code wise?

let x = $gamePlayer.x; let y = $gamePlayer.y;
const direction = [2,4,6,8].find(dir => $gamePlayer.canPass(x, y, dir));
if (direction) {
const eventId = 8;
switch(direction) {
case 2: y += 1; break;
case 4: x -= 1; break;
case 6: x += 1; break;
case 8: y -= 1; break;
}
$gameMap.event(eventId).locate(x, y);
}

EDIT: Oh I noticed you posted new pics. Not 100% sure if the above fixes that.
Would this code work if I put region restrictions on it?
Depends on how the region restriction plugin is made, but they should play well with the $gamePlayer.canPass function.
Here is a completely different idea, although it requires some setup:

use Terrain Tags.

This basically requires you to decide on the tileset if the event can be placed on a tile or not, and mark it as terrain tag 0 or 1 depending on the answer.

Then check the "random position around" for the terrain tag to see if the event is allowed to be placed on that tile or not instead of only checking passability.
let x = $gamePlayer.x; let y = $gamePlayer.y;
const direction = [2,4,6,8].find(dir => $gamePlayer.canPass(x, y, dir));
if (direction) {
const eventId = 8;
switch(direction) {
case 2: y += 1; break;
case 4: x -= 1; break;
case 6: x += 1; break;
case 8: y -= 1; break;
}
$gameMap.event(eventId).locate(x, y);
}

EDIT: Oh I noticed you posted new pics. Not 100% sure if the above fixes that.
Been testing it a bit and it seems to work fine. Looks like it always spawns it either left or below the player unless it's blocked, then it does above or right side of. Suppose that'll work just fine.

Thanks everyone for chiming in!

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/easiest-way-to-transfer-an-event-to-the-player-location.183907/