[XP/VX] Enemy Sight Range

Demos:
XP: http://www.mediafire.com/?m9buccxhygx
VX: http://www.mediafire.com/?pdgxbndcqxd

This tutorial is very difficult to understand at first.  I recommend downloading the demo and following along with me as I explain each line of the event, but you can just copy and paste the events if you want.  With this tutorial, you will be able to give an enemy sight range as well as make them chase after the player upon detection.

Death Page 1 Show
Setup
Conditions: None
Graphic: Evil 6 (Can be changed)
Autonomous Movement: Type: Fixed (Can be changed)
Speed: 3
Freq: 3
Options: Walking Animation
Priority: Same as Characters
Trigger: Parallel Process

Facing Down
Quote:
@>Conditional Branch: This event is Facing Down
:  Branch End

You'll want to start with just one direction.  It's much easier to copy and paste this conditional branch once it's finished than make each one separately.  You should uncheck 'Set handling when condtions do not apply'.

Quote:
   @>Control Variables: [0001: Player X] = Player's Map X
   @>Control Variables: [0002: Player Y] = Player's Map Y
   @>Control Variables: [0003: Enemy X] = This event's Map X
   @>Control Variables: [0004: Enemy Y] = This event's Map Y

This sets the variables that will be needed.  It records the player's map position and the enemy's map position so that they can be compared later.

Quote:
   @>Control Variables: [0002: Player Y] -= Variable: [0004: Enemy Y]

This is where it starts getting confusing.  I'm going to use a small graph since that's how I figured it out. 
http://img.photobucket.com/albums/v437/ ... ngDown.png[/img]
In this example, you can see that the X variables must be equal (which is included later), but you need to find the difference of the Y variables.  In this example, the enemy's coordinates are (2,2) and the player's coordinates are (2, 3).  To make the variable positive, I'm subtracting the enemy Y from the player Y:
3-2=1

Quote:
   @>Conditional Branch: Variable [0001: Player X] == Variable [0003: Enemy X]
   :  Branch End

The X coordinates must be equal.

Quote:
      @>Conditional Branch: Variable [0002: Player Y] <= 5
      :  Branch End

This is the number of spaces that you want the enemy to see you.  In this demo, the enemy can see up to 5 spaces away.

Quote:
         @>Conditional Branch: Variable [0002: Player Y] >= 0
         :  Branch End

This is needed because variables can be both positive and negative.  If you don't have this, the enemy will be able to see behind him infinitely.

Quote:
            @>Control Switches: [0001: Death] = ON

This is a switch and not a self switch because you'll probably want the enemy to give up the chase after a certain amount of time.  If you want them to chase the player until they catch them, you can use a self switch instead.

Facing Left
After you copy and paste the event, you have to change a couple of things.  Obviously, change the outermost conditional branch.
Quote:
   @>Conditional Branch: This event is Facing Left

If you forget to change this, your enemy will be able to see in all directions when facing down and be blind when facing left, right, or up.

Quote:
   @>Control Variables: [0003: Enemy X] -= Variable [0001: Player X]

http://img.photobucket.com/albums/v437/ ... ngLeft.png[/img]
Now, the enemy's coordinates are (2,2) and the player's coordinates are (1,2), so I subtracted the player's X from the enemy's X to make the variable positive.
2-1=1

Quote:
   @>Conditional Branch: Variable [0002: Player Y] == Variable [0004: Enemy Y]
   :  Branch End

This time, the Y coordinates must be equal.

Quote:
      @>Conditional Branch: Variable [0003: Enemy X] <= 5
         @>Conditional Branch: Variable [0003: Enemy X] >= 0
         :  Branch End
      :  Branch End

Now the X variable is used to determine the range.

Facing Right
This time, copy Facing Left so there's only a few things you have to change.
Quote:
   @>Conditional Branch: This event is Facing Right

First, don't forget to change this.

Quote:
   @>Control Variables: [0001: Player X] -= Variable [0003: Enemy X]

Just switch the player X and enemy X.

Quote:
      @>Conditional Branch: Variable [0001: Player X] <= 5
         @>Conditional Branch: Variable [0001: Player X] >= 0
         :  Branch End
      :  Branch End

Switch these lines from enemy X to player X.
http://img.photobucket.com/albums/v437/ ... gRight.png[/img]
The enemy's coordinates are (2,2) and the player's coordinates are (3,2).
3-2=1

Facing Up
Copy the Facing Up conditional branch.
Quote:
   @>Conditional Branch: This event is Facing Up

Make sure you change this.

Change the subtraction  line:
Quote:
   @>Control Variables: [0004: Enemy Y] -= Variable: [0002: Player Y]

You're just switching the player Y and enemy Y.

Quote:
      @>Conditional Branch: Variable [0004: Enemy Y] <= 5
         @>Conditional Branch: Variable [0004: Enemy Y] >= 0
         :  Branch End
      :  Branch End

Then switch the lines from player Y to enemy Y.
http://img.photobucket.com/albums/v437/ ... cingUp.png[/img]
The enemy's coordinates are (2,2) and the player's coordinates are (2,1).
2-1=1


Death Page 2 Show
Setup
Conditions: Switch [0001: Death] is ON
Graphic: Evil 6 (Can be changed)
Autonomous Movement: Type: Approach
Speed: 4 (Can be changed)
Freq: 5
Options: Walking Animation
Priority: Same as Characters
Trigger: Parallel Process (You can also use Event Touch.  Just get rid of all the location conditional branches and skip right to the battle processing.  This will make the battle occur when the enemy touches the player instead of when they're one tile apart.)

Facings
Copy and paste the entire first event page, either using copy event page or copying the commands.   First, change the values for sight range from 5 to 1 (this means it will activate when they are 1 space apart.  If you have a ranged enemy, this can be higher).

I've tried using Player Touch, but for that, the player has to walk into the enemy.  Event touch is risky because it will activate whenever it touches an event, whether it's the player or that torch in the middle of the floor.

Next, change the switch line in each facing:
Quote:
            @>Control Switches: [0001: Death] = OFF

This is the first command because of the parallel event that makes the enemy get tired of chasing the player and stop.

Then, use a battle processing to start the battle.  After the battle, you'd usually use the Erase Event command to make the event disappear until the player comes back to the map.  I used a move route to move the player back so you could activate the enemy more than once.  Make sure you add these to all facings.


Give Up Common Event Show
Setup
Trigger: Parallel
Condition Switch: [0001: Death]

Commands
You'll want a wait command that waits for however many frames you wish your enemy to pursue the player.  Then, turn the switch [0001: Death] to OFF.  This puts the enemy back to the first event page.


Multiple Enemies:
These are some things to be aware of when making multiple enemies.  First, you'll need different variables for each enemy's X and Y coordinates, but you can use the same variables for the player's X and Y coordinates.  Also, you'll want to use different switches for each enemy and different common events for each enemy.  This way, you're not only allowing the player to be followed by multiple enemies, but you can also set different time frames for when the enemies get tired.


If I need to make anything clearer or if you want to know how to change something, please post in this thread or PM me and I'll get back to you as soon as possible.
Wow.
That just took the demo and made it so much more complicated than it needed to be. @_@
You can, of course, just copy and paste the events, but I thought I'd explain exactly what everything does and why it's like it is.  Tutorials aren't supposed to be copy and paste.  You're supposed to learn as you're going along.
you vx demo isnt working
bojjenclon wrote:
you vx demo isnt working

The link is fixed.  I'm not sure why Mediafire changed it, but it's working now.
Wow... This is awsome! Will definitly use
Thanks for the comments.  It's nice to know that everyone appreciates my work.
Thank you, Guardian. I will definitely be using this in my evented ABS.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=52017