top of page
losP3TH.png

RPG Maker MZ/MV Expert Tutorial

Line of Sight

Line of Sight isn't difficult, but the lines of code can get lengthy. Don't let that intimidate you. By the end of this series, you will be able to create Lines of Sight of all aura types (circular, linear, quadratic, etc.) and easily access it through common events.

 

Below are the copy-paste and easy-to-read versions to better your understanding. Use the navigational tool to quickly access each part.

LoSTH.jpg

Line of Sight Part 1 - Everything Except Circles

Copy & Paste Code:

Spotted in Facing Direction (Copy Pasted as 1-Line)

$gameMap.event(this._eventId).direction() == 2 && $gameMap.event(this._eventId).y < $gamePlayer.y || $gameMap.event(this._eventId).direction() == 8 && $gameMap.event(this._eventId).y > $gamePlayer.y || $gameMap.event(this._eventId).direction() == 4 && $gameMap.event(this._eventId).x > $gamePlayer.x || $gameMap.event(this._eventId).direction() == 6 && $gameMap.event(this._eventId).x < $gamePlayer.x

​

Triangle Vision (Copy Pasted as 1-Line)
Math.abs($gameMap.event(this._eventId).x - $gamePlayer.x) + Math.abs($gameMap.event(this._eventId).y - $gamePlayer.y) <= 3

​

Line Vision (Copy Pasted as 1-Line)
Math.abs($gameMap.event(this._eventId).y - $gamePlayer.y) <= 3 && $gameMap.event(this._eventId).x == $gamePlayer.x || Math.abs($gameMap.event(this._eventId).x - $gamePlayer.x) <= 3 && $gameMap.event(this._eventId).y == $gamePlayer.y

​

Box Vision (Copy Pasted as 1-Line)
Math.abs($gameMap.event(this._eventId).y - $gamePlayer.y) <= 3 && Math.abs($gameMap.event(this._eventId).x - $gamePlayer.x) <= 1 || Math.abs($gameMap.event(this._eventId).x - $gamePlayer.x) <= 3 && Math.abs($gameMap.event(this._eventId).y - $gamePlayer.y) <= 1

Easy to Read Code (Click to Enlarge):

*Note: Do not copy the code below. Use Copy & Paste Code above

Line of Sight Code EZRead.PNG
P1
CirclesTH.jpg

Line of Sight Part 2 - Circles

Use Below Image Key to Better Understand Code (Click to Enlarge)

circles key.png

Key

A1 & A2 - Aura radius and test in aura respectively

B - Test whether difference in X or Y is smaller. Smaller X goes to Cs, Larger X goes to Ds.

C1 - Test difference in X is less than/equal to radius (A1)

C2 - Pythagorean theorem to solve for Y and test if the difference in Y is less than/equal to radius. 

D1 - Test difference in Y is less than/equal to radius (A1)

D2 - Pythagorean theorem to solve for X and test if the difference in X is less than/equal to radius. 

E1 & E2 - Tells the event that player is within aura, otherwise the switch remains OFF as in A2.

E3 - When player is in aura, do the following.

​

​

​

Copy & Paste Code

B:

Math.abs($gamePlayer.x-$gameMap.event(this._eventId).x) < Math.abs($gamePlayer.y-$gameMap.event(this._eventId).y)

 

C1 (Note change [$gameVariables.value(2)] as appropriately):

Math.abs($gamePlayer.x - $gameMap.event(this._eventId).x) <= $gameVariables.value(2)

 

C2 (Note change [$gameVariables.value(2)] as appropriately):

Math.abs($gamePlayer.y-$gameMap.event(this._eventId).y) <= Math.round( Math.sqrt(Math.pow($gameVariables.value(2),2) - Math.pow($gamePlayer.x - $gameMap.event(this._eventId).x, 2)) )

​

D1 (Note change [$gameVariables.value(2)] as appropriately):

Math.abs($gamePlayer.y - $gameMap.event(this._eventId).y) <= $gameVariables.value(2)

​

D2 (Note change [$gameVariables.value(2)] as appropriately):

Math.abs($gamePlayer.x-$gameMap.event(this._eventId).x) <= Math.round(Math.sqrt(Math.pow($gameVariables.value(2),2) - Math.pow($gamePlayer.y - $gameMap.event(this._eventId).y,2)) )

Easy to Read Code (Click to Enlarge):

*Note: Do not copy the code below. Use Copy & Paste Code above

Line of Sight Code EZRead.PNG

More Line of Sight

P2
losP3TH.png
losP3TH.png

Line of Sight Part 3 - Adding Chase

We take the code from Part 2 to execute a chase in three different ways: 1) unconditionally, 2) within aura, and 3) length of time.

  • YouTube
P3
parallelprocessth.png
parallelprocessth.png

Parallel Process' Secret Wait Timer

A quick look at how parallel process secretly waits 1 frame.

  • YouTube
P3.5 Parallel Process
losFinaleTh.jpg
losFinaleTh.jpg

LoS P4 - Creating for an End-User

Take a leap forward by using 2D arrays and iteration to recreate the algorithm for an End-User. Warning: Not easy.

  • YouTube
P4:End-User
bottom of page