This segment
Code:
//find the coordinate in front of the player
//first, we map from the direction8 to a map coordinate
var vecx = [0 , -1 , 0 , 1 , -1 , 0 , 1 , -1, 0 , 1];
var vecy = [0 , 1 , 1 , 1, 0 ,0 ,0 ,-1 ,-1 ,-1];
var mapx = vecx[$gamePlayer.direction8()]+$gamePlayer.x
var mapy = vecy[$gamePlayer.direction8()]+$gamePlayer.y
Calls the altimit ".direction8()" function, which gives the 8 directional facing of the player (so upper-left is 7, upper right is 9, down is 2, etc)
It converts that to a coordinate relative to the player (so upper left is [-1,-1], upper right is [1,-1], and down is [0,1]). Then it adds the player's location (whoops, forgot to include that in the first draft). That gives the coordinate of the tile that the player is facing.
This segment
Code:
//loop through all events
var eventList=$gameMap.events();
for (var i = 0; i < eventList.length; i++)
{
gets all the events on the map, and loops through them.
Note:I am NOT looping through "$gameMap.event(i)", because event numbers aren't necessarily sequential (you can have event 1, event 35, and event 56 on the same map with nothing else).
Code:
// check if an event is within a range of 1 of the tile we selected earlier
if ( ( eventlist[i].x - mapx)*( eventlist[i].x - mapx) +( eventlist[i].y - mapy)*( eventlist[i].y - mapy) <1)
{
targetID= eventList(i).eventId()
Once we're looping through them all, we check each event on the map to see how far they are from the tile the player is facing (using the standard sqrt(dx^2 + dy^2) formula ). If the event is within 1 tile of our coordinate, we save the targetID of that event. Then just proceed processing as normal
I haven't tested this guy but you should be able to just put it in a script block.