Check to see if an event is facing another event?

So essentially I want to have a Weeping Angel/SCP-173 style enemy that will only be able to move and act if nothing is looking at it. However in my case, due to a plugin, the player character and followers are all standard events, not the usual player character events.

Is there a good string of code that will flip a switch if the character events are facing the weeping angel event? I can provide more details if needed, obviously.
Edit: Alright, here is a working solution to your problem. There is probably a cleaner way to do this, but this should get the job done:

rpg maker help.png


What this does is store the direction of each event into variables. The directions are always stored as numbers with this code.

Here is a "cheat sheet" for what the numbers mean:

2 = down
8 = up
4 = left
6 = right

From here, create whatever else is necessary in your event to check for other directions the angel statue could face a player event, as well as all the things you want to happen when they are facing each other.

Happy game making,
-Niniann
If you want it to proc only when they are mutually looking at one another, you can take advantage of the fact that opposing numpad directions (Control Variables -> Game Data -> Character -> Direction) add to 10.

If you just want to check "is player event facing toward this character", you can check X/Y coordinate differences (Control Variables -> Game Data -> Character -> Map X/Y) based on the player's facing, e.g.
Spoiler: event example
◆If:Player is facing Down ◆Control Variables:#0001 Check = Map Y of Player ◆Control Variables:#0001 Check -= Map Y of This Event ◆If:Check < 0 ◆Show Balloon Icon:This Event, Silence (Wait) ◆ :Else ◆Show Balloon Icon:This Event, Exclamation (Wait) ◆ :End ◆ :Else ◆If:Player is facing Left ◆Control Variables:#0001 Check = Map X of This Event ◆Control Variables:#0001 Check -= Map X of Player ◆If:Check < 0 ◆Show Balloon Icon:This Event, Silence (Wait) ◆ :Else ◆Show Balloon Icon:This Event, Exclamation (Wait) ◆ :End ◆ :Else ◆If:Player is facing Right ◆Control Variables:#0001 Check = Map X of Player ◆Control Variables:#0001 Check -= Map X of This Event ◆If:Check < 0 ◆Show Balloon Icon:This Event, Silence (Wait) ◆ :Else ◆Show Balloon Icon:This Event, Exclamation (Wait) ◆ :End ◆ :Else ◆Control Variables:#0001 Check = Map Y of This Event ◆Control Variables:#0001 Check -= Map Y of Player ◆If:Check < 0 ◆Show Balloon Icon:This Event, Silence (Wait) ◆ :Else ◆Show Balloon Icon:This Event, Exclamation (Wait) ◆ :End ◆ :End ◆ :End ◆ :End
If you want a more complicated setup, e.g. a vision arc less than 180 degrees, or line-of-sight with obstacles, it may be simplest to find an appropriate plugin.
I like using this if you need a script call to check if event 1 and event 2 are looking at each other:
JavaScript:
let e = $gameMap._events;
if (e[1].direction() === Math.abs(e[2].direction() - 10)) {
    // do stuffs
};
Edit: Just realized @caethyril gave you info similar to this already haha. Too early here.
If you want it to proc only when they are mutually looking at one another, you can take advantage of the fact that opposing numpad directions (Control Variables -> Game Data -> Character -> Direction) add to 10.

If you just want to check "is player event facing toward this character", you can check X/Y coordinate differences (Control Variables -> Game Data -> Character -> Map X/Y) based on the player's facing, e.g.
Spoiler: event example
◆If:Player is facing Down ◆Control Variables:#0001 Check = Map Y of Player ◆Control Variables:#0001 Check -= Map Y of This Event ◆If:Check < 0 ◆Show Balloon Icon:This Event, Silence (Wait) ◆ :Else ◆Show Balloon Icon:This Event, Exclamation (Wait) ◆ :End ◆ :Else ◆If:Player is facing Left ◆Control Variables:#0001 Check = Map X of This Event ◆Control Variables:#0001 Check -= Map X of Player ◆If:Check < 0 ◆Show Balloon Icon:This Event, Silence (Wait) ◆ :Else ◆Show Balloon Icon:This Event, Exclamation (Wait) ◆ :End ◆ :Else ◆If:Player is facing Right ◆Control Variables:#0001 Check = Map X of Player ◆Control Variables:#0001 Check -= Map X of This Event ◆If:Check < 0 ◆Show Balloon Icon:This Event, Silence (Wait) ◆ :Else ◆Show Balloon Icon:This Event, Exclamation (Wait) ◆ :End ◆ :Else ◆Control Variables:#0001 Check = Map Y of This Event ◆Control Variables:#0001 Check -= Map Y of Player ◆If:Check < 0 ◆Show Balloon Icon:This Event, Silence (Wait) ◆ :Else ◆Show Balloon Icon:This Event, Exclamation (Wait) ◆ :End ◆ :End ◆ :End ◆ :End
If you want a more complicated setup, e.g. a vision arc less than 180 degrees, or line-of-sight with obstacles, it may be simplest to find an appropriate plugin.
Appreciate the help, but I need something between two events, not the player and an event.

Honestly, VizuStella's Encounter Effects plugin would probably be perfect, if it could be tweaked a little. I'll probably see about getting help on that front. Thanks anyway!
Appreciate the help, but I need something between two events, not the player and an event.
Control Variables -> Game Data -> Character lets you select the player or any map event. Is there a problem?
Control Variables -> Game Data -> Character lets you select the player or any map event. Is there a problem?
Valid point! I'm taking another crack at it, though it definitely has the potential to get really complicated. There are - unfortunately - four player character events to keep track of simultaneously, which exponentially complicates matter. Not to mention line of sight being broken by walls can't easily be taken into account. Still, I'm gonna see if I can come up with something that works.
This will depend on how much wonkiness you are prepared to accept, but what I do is to put the following in a custom move route of the event:

JavaScript:
const Pla = $gamePlayer;
if(Pla.findDirectionTo(this.x,this.y) != Pla.direction()){
this.moveStraight(this.findDirectionTo(Pla.x,Pla.y));
}
It basically looks at the path the player needs to take to get to the event, and if the first step is equal to the direction the player is facing, it is considered that the player is facing the event.

There's a lot of wonkiness with this (one of which is that it behaves strangely when in close proximity), but as far as simple solutions go, I find it works better in a complicated dungeon map with lots of walls than pure direction sensing. Note that since the move route script accepts only a single row, so you need to weed out the linefeeds.

JavaScript:
const Pla = $gamePlayer;if(Pla.findDirectionTo(this.x,this.y) != Pla.direction()){this.moveStraight(this.findDirectionTo(Pla.x,Pla.y));}
@IPTdragon - if you do want line-of-sight considerations, you might be interested in feature D02 of my plugin Cae_Tweaks:
It adds a method Game_CharacterBase#los for line-of-sight checks, e.g.
JavaScript:
$gameMap.event(1).los($gameMap.event(2), 135)
"Can event 1 see event 2, assuming a 135-degree vision arc centred on event 1's current facing direction?" This is blocked by A3/A4 autotiles, collidable characters, and any specified regions.

There are almost certainly other, similar plugins available.
@IPTdragon - if you do want line-of-sight considerations, you might be interested in feature D02 of my plugin Cae_Tweaks:
It adds a method Game_CharacterBase#los for line-of-sight checks, e.g.
JavaScript:
$gameMap.event(1).los($gameMap.event(2), 135)
"Can event 1 see event 2, assuming a 135-degree vision arc centred on event 1's current facing direction?" This is blocked by A3/A4 autotiles, collidable characters, and any specified regions.

There are almost certainly other, similar plugins available.
This seems like it could be super helpful! To use it do I need to put that code into a conditional statement or does it just return a true/false value or something?
The script call returns true or false, so it can be used in valid script fields, e.g.
  • Conditional Branch -> Script, or
  • In an if statement in a script call.
The script call returns true or false, so it can be used in valid script fields, e.g.
  • Conditional Branch -> Script, or
  • In an if statement in a script call.
Screenshot 2024-02-20 141447.png

Okay so this is a very very basic branch just to see if it works. It returns false ('Oof') despite the two events (1 and 88) being directly in front of each other. I'm assuming I'm just doing something wrong, though.
Cae_Tweaks has a lot of features, but all of them are disabled by default. You need to:
  • Import the plugin through the Plugin Manager (as usual);
  • Double-click the relevant parameter (D02: Line-of-Sight Tools) and change its value to ON (true);
  • Save your project to apply Plugin Manager changes, then test.
You can also check the Console tab of the dev tools window (opened by pressing F8 or F12 during playtest). During playtest, D02 outputs a simple representation of the line it traces. Errors will also be displayed here, usually in red text.

If you still can't get it to work, maybe you're using wall-top tiles as floor? If necessary you can edit the isLosBlocked function in the plugin's code to change what it detects as blocking line-of-sight:
Spoiler: cae_tweaks v12 excerpt - islosblocked
JavaScript:
    /**
     * @this {Game_CharacterBase} sighting character
     * @param {Game_CharacterBase} target sight target
     * @param {LinePoint} o point on the line of sight
     * @returns {boolean} `true` iff line of sight is obstructed.
     */
    const isLosBlocked = function(target, o) {
        const { x, y, d } = o;
        if (target.pos(x, y))
            return false;   // goal!
        // Walls and wall-tops block LoS.
        // Don't consider elevation, would want more structure for that.
        const tileIds = [0,1,2,3].map(z => $gameMap.tileId(x, y, z));
        if (tileIds.some(id => Tilemap.isShadowingTile(id)))
            return true;
        // Collidable characters block LoS.
        if (this.isCollidedWithCharacters(x, y))
            return true;
        // Opaque regions block LoS.
        if (OPAQUE_REGIONS.contains($gameMap.regionId(x, y)))
            return true;
        return false;
    };
Here we see 3 checks: wall (tops), collidable characters, and opaque regions. To remove a check, you can just // comment out the relevant lines, e.g.
JavaScript:
//      const tileIds = [0,1,2,3].map(z => $gameMap.tileId(x, y, z));
//      if (tileIds.some(id => Tilemap.isShadowingTile(id)))
//          return true;
Cae_Tweaks has a lot of features, but all of them are disabled by default. You need to:
  • Import the plugin through the Plugin Manager (as usual);
  • Double-click the relevant parameter (D02: Line-of-Sight Tools) and change its value to ON (true);
  • Save your project to apply Plugin Manager changes, then test.
You can also check the Console tab of the dev tools window (opened by pressing F8 or F12 during playtest). During playtest, D02 outputs a simple representation of the line it traces. Errors will also be displayed here, usually in red text.

If you still can't get it to work, maybe you're using wall-top tiles as floor? If necessary you can edit the isLosBlocked function in the plugin's code to change what it detects as blocking line-of-sight:
Spoiler: cae_tweaks v12 excerpt - islosblocked
JavaScript:
    /**
     * @this {Game_CharacterBase} sighting character
     * @param {Game_CharacterBase} target sight target
     * @param {LinePoint} o point on the line of sight
     * @returns {boolean} `true` iff line of sight is obstructed.
     */
    const isLosBlocked = function(target, o) {
        const { x, y, d } = o;
        if (target.pos(x, y))
            return false;   // goal!
        // Walls and wall-tops block LoS.
        // Don't consider elevation, would want more structure for that.
        const tileIds = [0,1,2,3].map(z => $gameMap.tileId(x, y, z));
        if (tileIds.some(id => Tilemap.isShadowingTile(id)))
            return true;
        // Collidable characters block LoS.
        if (this.isCollidedWithCharacters(x, y))
            return true;
        // Opaque regions block LoS.
        if (OPAQUE_REGIONS.contains($gameMap.regionId(x, y)))
            return true;
        return false;
    };
Here we see 3 checks: wall (tops), collidable characters, and opaque regions. To remove a check, you can just // comment out the relevant lines, e.g.
JavaScript:
//      const tileIds = [0,1,2,3].map(z => $gameMap.tileId(x, y, z));
//      if (tileIds.some(id => Tilemap.isShadowingTile(id)))
//          return true;
The plugin is on, but during playtest I don't see any LoS lines being drawn. Will it draw LoS for events automatically or do I need to activate something? Also, the floor tile is indeed floor tile.

Do you want me to post any screenshots or anything? I can if you need me to.
You need to call the los method manually: my plugin doesn't support stuff like event triggers, it's literally a script function. Also, it doesn't draw anything visible to the player in-game: it's just for checking sight, not drawing it.

However, during playtest, it should output a debug representation to the Console tab of the dev tools window, e.g.

console.png

This screenshot illustrates 4 checks for line-of-sight from map event 3 (currently facing left <-):
  1. To map event 15, with default FoV: sight is clear for 4 tiles, then blocked => false.
  2. To map event 9, with 180° FoV: sight is clear to the target => true.
  3. To map event 13, with 300° FoV: sight is clear to the target => true.
  4. To map event 13, with default FoV: target is not within FoV, so line is not traced => false.
If you're only seeing a pentagon (represents viewer) followed by false, that means line-of-sight is being blocked by the first tile.

If you're still having trouble, it may help to share screenshots of:
  • Your map, in region editing mode (so that tiles, events, and region IDs are all visible);
  • The script call you are using; and
  • The Console tab of the dev tools window.

Edit: oh, maybe you're using pixel-movement? If so:
Spoiler: pixel movement suggestion
You can try opening Cae_Tweaks in a text editor and edit this part:
JavaScript:
    Game_CharacterBase.prototype.los = function(char, fov) {
        if (!(char instanceof Game_CharacterBase))
            return false;
        const x0 = this.x,
              y0 = this.y,
              x1 = char.x,
              y1 = char.y,
               d = this.direction();
...to this:
JavaScript:
    Game_CharacterBase.prototype.los = function(char, fov) {
        if (!(char instanceof Game_CharacterBase))
            return false;
        const x0 = Math.round(this.x),
              y0 = Math.round(this.y),
              x1 = Math.round(char.x),
              y1 = Math.round(char.y),
               d = this.direction();

Original thread: https://forums.rpgmakerweb.com/threads/check-to-see-if-an-event-is-facing-another-event.165867/