Direction Based Line of Sight


I made this mostly for touch encounter games to help add a little personality to the touch encounters.  I also think that this can be utilized for puzzles and stealth missions in games.

This tutorial was made with the default scripts.  There may be incompatibility with some scripts, particularly pixel movement scripts in which case you would have to completely rewrite the Conditional Branch script calls using the syntax of those scripts.  Another that comes to mind would be an 8 directional movement script in which case you may need to take the concepts presented here and expand them to include the additional directions.

You can download the project file here.  I recommend you play through the test game to get a feel of the events before you read the tutorial.

Glossary of Terms used in Script Calls

*Note:  I am not a scripter and have a limited understanding of RGSS3, so if I got any of the descriptions wrong, I will not be offended if you correct me in the comment section below.

$[@event_id] – this is used to get information about the currently running event @event_id is the same as saying this event if you need a specific event on a map just use it’s event id number which is displayed in the upper left hand corner when you edit the event.

$game_player – this used to get information about the player

(compared values).abs – is used to get the absolute value between them.  Absolute values are always positive.

Some uses:

$[@event_id].x – this gets the X coordinate value of the currently running event’s position on the map

$[@event_id].y – this gets the Y coordinate value of the currently running event’s position on the map

$game_player.x – this gets the X coordinate value of the player’s position on the map

$game_player.y – this gets the Y coordinate value of the player’s position on the map

$[@event_id].direction – this gets the value of which direction the currently running event is currently facing.

$game_player.direction – this gets the value of which direction the player is currently facing

  • Direction Key:
    • 2 is down
    • 8 is up
    • 4 is left
    • 6 is right

&& – when used between to conditions basically means “and”.  If there are multiple && used then all conditions must be met.

|| – when used, is basically a line break and can be said to be the same as “or”.  If this is used in a conditional branch script call that means any of the conditions will be enough to satisfy the conditional branch.

The X/ Y Coordinate Plane and Relative Values

In mathematics the X axis is horizontal and the Y axis is vertical and is generally written as (x,y).  This is true in RPG Maker as well and you can see this in the bottom right hand corner of the map editor.  In RPG  Maker coordinate (0,0) is in the upper left hand corner which means as you go right from (0,0) the X value increases and as you go down from (0,0) the Y value increases.

A relative value compares two objects on a coordinate plane and finds the difference between them.   This tutorial utilizes this and also uses script calls that use less than and greater than X and/ or Y comparisons to determine where an object is relative to a different object.

I’ll use the above image to illustrate some examples:

  • the X value of the spider is less than the X value of Ralph
  • the X value of the scorpion is greater than the X value of Ralph
  • the Y value of the bat is less than the Y value of Ralph
  • the Y value of the hornet is greater than the Y value of Ralph

Go to Part 1:  Four Simple Line of Sight Examples


12 responses to “Direction Based Line of Sight

  1. juni127

    September 20, 2014 at 2:29 pm

    Hou do I can get the id of the event knowing only the positions

    • infamous bon bon

      September 20, 2014 at 9:57 pm

      I’m not sure exactly what you mean. Are you asking if there is a way to get the id of the event at a set of coordinates on the map or something else? Maybe an example would help.

  2. Eba-chan

    October 24, 2015 at 10:19 am


    I do have probably a stupid question, but it’s a huge problem to me. I tried to make the Assasin version of Your LOS for pigeons hunting, but I’m stuck on some stupidity… The part, where a player kills the NPC isn’t working. I even copy&paste the evil guy from Your demo and tried it with it, but it’s still not working. On the other hand, when I copy&paste my own (the pigeon) event into Your project, it’s working! O_O
    I copied both events (mine and Yours) into an almost empty project (I use it for testing the scripts or messing with their settings) but to no avail. I commented out all of the scripts and I added “Script Fix” from your project to assure everything is the same… but nothing changed. Only the “Spotting part” is working.
    I tried it in a completely fresh project, but no, it’s not working. Now I’m really lost. With the try in a new project, I’m sure it’s not another script fault or my own. So… do You have any idea, where is the problem?

    Thank you for Your time and an answer, whatever it will be.

  3. Eba-chan

    October 25, 2015 at 8:07 am

    about that comment I posted yesterday, I apologize for it. I kinda figured out what was wrong. Your script is flawless, it was the condition with C button, that was… moody? I don’t know what to say, but even your assassin-NPC-event made it look like it wasn’t recognizing the C button in my project. Maybe the program needed a restart.
    So I’m sorry for my rant and babbling the other day.

    • infamous bon bon

      October 25, 2015 at 8:34 am

      Oh good you got it figured out. Those little things can be so frustrating.

  4. Unmercyful

    February 6, 2016 at 1:42 pm

    Hi there, This is such an awesome tutorial just want to say thank you. I been trying convert the script calls to MV. So far I got the distance script converted but have not been able to accomplish converting the directional script. Just curious if you knew what the script call for MV was? That one you have set on the second conditional branch in your demo events for the enemy/player direction. I’d be very appreciative if you get a moment to enlighten me. I been at this for days lol. If not, that’s cool still wanna say thanks for great tutorial.

    • infamous bon bon

      February 7, 2016 at 12:47 pm

      I don’t know them yet, but it’s something that will come up in my next project, so hopefully I will learn that stuff and make a tutorial.

      • Unmercyful

        February 7, 2016 at 1:05 pm

        Thanks for the reply. I do have a couple I’d like to share with to help pay back for such an awesome tutorial.



        I have been able to translate Triangular Line of Sight:

        Math.abs($gameMap.event(this._eventId).x – $gamePlayer.x) + Math.abs($gameMap.event(this._eventId).y – $gamePlayer.y) <= 3

        I just got the MV directional script this morning and translated the directional sight on the first 3 events:

        $gameMap._events[this._eventId]._direction == 2 && $gameMap.event(this._eventId).y $gamePlayer.y || $gameMap._events[this._eventId]._direction == 4 && $gameMap.event(this._eventId).x > $gamePlayer.x || $gameMap._events[this._eventId]._direction == 6 && $gameMap.event(this._eventId).x < $gamePlayer.x

        The rest I am still working on. But with this I should be able to figure it out. I wanted to give to you what I had so if you plan on making an another tutorial maybe this will help. I'd make tutorial but I'm that good at it.

      • infamous bon bon

        February 7, 2016 at 7:35 pm

        Thank you this will definitely save me some time.

  5. shiicolate

    August 31, 2017 at 4:45 pm

    Hello! I’m aware this post is quite old, but is it possible for the NPC to NOT see when the player is behind a wall, or some other kind of cover? I was thinking, maybe set these cover areas as a specific region ID, then script it so if the NPC is looking in that direction, even if they have a longer view distance, it gets blocked by the wall?
    I’m having trouble with that, because in the game I’m making there are a lot of rooms in the map, and often the player gets spotted by a guard that is on the other side of the wall. I know I could carefully set routes for them so they don’t walk into a spot where this could happen, but this just seems… Meh! hahah
    Thank you a lot!

    • infamous bon bon

      August 31, 2017 at 4:57 pm

      I’m sure there is a way, but I never was able to figure it out. Dig around in the region code and you may be able to figure it out.

      • shiicolate

        August 31, 2017 at 5:25 pm

        The fast reply surprised me! hahah thank you for the attention.
        My first attempt was definining a region ID to each room and setting a condition before the X and Y ones to check if the player is in said region, which is messy, but works(ish).
        I have no idea how to script at all, but maybe with a bit more tinkering I’ll be able to figure a more elegant solution out.


Leave a Reply to infamous bon bon Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: