MV [MV]Plugin with conditional branch script that check if player is facing current event without using Variable or specific ID

I am currently working on creating an overworld enemy that roams and interacts with the player's attacks. The spawning of these enemies will utilize the event copy feature from the YEP plugin. Given that there will frequently be multiple enemies on the screen, I am unable to use variables to store their X and Y coordinates.
The specific ID of these enemies does not transfer when they are copied to another map, which prevents me from using scripts that rely on the the enemy's specific event ID. Instead, I need a script that verifies whether the player is facing the enemy and is positioned correctly to engage with it, without relying on variables or specific IDs. If all conditions are met, the script should return true, otherwise, it should return false.
If you are able to get the character/event, you could do this (save the text below as a plugin - .js file):
JavaScript:
Game_Character.prototype.isFacingCharacter = function(character) {
    const sx = this.deltaXFrom(character.x);
    const sy = this.deltaYFrom(character.y);
    let d;
    if (Math.abs(sx) > Math.abs(sy)) {
        d = sx > 0 ? 4 : 6;
    } else if (sy !== 0) {
        d = sy > 0 ? 8 : 2;
    }
    return d === this.direction();
};
You should be able to do:
JavaScript:
$gamePlayer.isFacingCharacter($gameMap.event(1))

And it will return true if the player is facing map event 1.

I'll have to look into yanfly but I believe the event IDs are 1000+ or something when they are created. You should be able to specify the event IDs above 1000 for it or use $gameMap._spawnedEvents to get a list of all your spawned events.

They will be listed in an array and you can grab them with $gameMap._spawnedEvents[1] for the first spawned event, and so on.

Edit: I notice you mentioned Event Copier so that will be different. You would reference the actual event on the map and check the ._copiedEvent property to see of it is true or not (true means that the event is copied, false is false).

Another method: To get an event, you can also grab it based on its location with this and the code above:
JavaScript:
// Get the top event that is located at 5, 0
const event = $gameMap.eventsXy(5, 0)[0];
// Check if the player is turned toward it, if so, return true
$gamePlayer.isFacingCharacter(event);
If you need to see if the player is at a specific location, you can also do:
JavaScript:
// return true if player is at location 3, 5
$gamePlayer.pos(3, 5);
Something more specific, check if player is facing the event at 5, 0 AND the player is 3 tiles to the left of the event, and 4 tiles down from the event:
JavaScript:
const event = $gameMap.eventsXy(5, 0)[0];
$gamePlayer.isFacingCharacter(event) && $gamePlayer.pos(event.x - 3, event.y + 4);
If you aren't able to get the event's id you can run the script from inside the event and have the event call its own id (this._eventId) or itself (this)

E.g. in the event's move route
$gamePlayer.isFacingCharacter(this)
If you aren't able to get the event's id you can run the script from inside the event and have the event call its own id (this._eventId) or itself (this)

E.g. in the event's move route
$gamePlayer.isFacingCharacter(this)
Does this script also check if the player is in the correct position to face the event, let's say they have a gun attack , and it's on the left of the enemy , player x have to be lower than enemy.


If not, is there a script that can do that?
If I have both of these script , my problem would be solved , thank you
Where do you need to put the script?
Do you make the enemies parallex events that checks the player's position?
it's on the left of the enemy , player x have to be lower than enemy.
Just remember to save the prototype function above first to use the script call (also assuming you are using the move route):
JavaScript:
$gamePlayer.isFacingCharacter(this) && $gamePlayer.x < this.x
Where do you need to put the script?
Do you make the enemies parallex events that checks the player's position?
I want to put it in conditional branch of an event that's on parallel
If you aren't able to get the event's id you can run the script from inside the event and have the event call its own id (this._eventId) or itself (this)

E.g. in the event's move route
$gamePlayer.isFacingCharacter(this)
bandicam 2025-02-13 11-03-16-864.png

I put the script you gave in the condition branch but it doesn't work, I've check closely, all progress stopped once it reach this ( $gamePlayer.isFacingCharacter(this) ) conditional branch, what did I do wrong here.
I said to put it in a move route. If you want to put it in the event page change this to this.character(0)
I said to put it in a move route. If you want to put it in the event page change this to this.character(0)
Ohhh. It work now! ( It got pass the facing conditional branch as least ) Does this apply to $gamePlayer.x < this.x when used in event page too?
When you use a move route you're choosing what "this" is. If you use "this" in the event page it is not referring to the event but the game interpreter which is why you have to change it to this.character(0).
When you use a move route you're choosing what "this" is. If you use "this" in the event page it is not referring to the event but the game interpreter which is why you have to change it to this.character(0).
It worked! thought when I try to do $gamePlayer.x = this.x ( checking if both X is the same ) It didn't work, Is it a different symbol for = ? this is the last step to finish what I'm trying to do
You use == for comparison. One = is assignment. Also this has to be this.character(0) if you're using it outside a move route
Here is what I do, and the "==" conditional branch Didn't work
You use == for comparison. One = is assignment. Also this has to be this.character(0) if you're using it outside a move route
bandicam 2025-02-16 16-10-08-681.png
Don't capitalize the y
wait never
Here is what I do, and the "==" conditional branch Didn't work

View attachment 331733
It's my fault, I made the Y uppercase I think it had to be lower case.
It work now! Tysm, also sorry for so many unnesecary question

anyways here's the result:
image don't work, but like Aqua said from comparison:

= : assignment
== : compare value
=== : strict compare value (must be equal)

sometimes > don't work as "> 5" looks for a number 6 or higher
while ">= 5" check 5 or higher.

so make sure to remember the basics so it can do exactly what you look for.

Original thread: https://forums.rpgmakerweb.com/threads/mv-plugin-with-conditional-branch-script-that-check-if-player-is-facing-current-event-without-using-variable-or-specific-id.175369/