Enemy Distance Scanner (Radar)

Made for a request here.

This script acts like a radar/scanner to check the distance of the player from the enemy events on the map.

Features:

[1] Able to check the nearest enemy and return the event's id.

[2] Able to list down a list of enemy event's ids and their distance from the player.

[3] Able to check any event's distance from player.

How to Use:

[1] Script itself is plug and play, simply paste it below Material and above Main.

[2] To make an event scan-able by the radar, you will need to add <enemy> tag to the event's name.

      [IMG]http://s30.postimg.org/79ddzbvhp/enemytag.png[/IMG]

[3] Script calls for scanning:

Spoiler
Get distance value of Event X from Player

$game_map.distance_from(id)id = any event's id (works for event without name tag)

Get distance of all Enemies from Player

$game_map.scan_targetThis will list all the enemy event's id and their distance from the player

Get the nearest enemy event's id

nearest_enemythis will return the event's id of the nearest enemy
Pair these with Control Variables' Script box to output the result to game variable.

eg. Control Variables: [0001] = nearest_enemy

Compatibility:

Should be compatible with other custom scripts.

Terms of Use:

Free for both Commercial and Non-commercial

Script:

Spoiler
Code:
#==============================================================================# ■ Meow Face Enemy Scanner#------------------------------------------------------------------------------# Return the ID of the nearest Event (from player position)#==============================================================================# How to Use:# [1] Put this script below Material and above Main# [2] Put this name tag on events you want to scan: <enemy># [3] Use Script Calls "nearest" to get the nearest enemy#==============================================================================class Game_Map  def distance_from(id)    $game_player.distance_x_from($game_map.events[id].x).abs + $game_player.distance_y_from($game_map.events[id].y).abs  end  def scan_target    meowradar = {}    @events.values.each do |meow|      if meow.enemy?        meowscan = distance_from(meow.id)        meowtarget = (meow.id).to_s        meowradar[meowtarget] = meowscan      end    end    meowradar  end  def scan_nearest    scan_target.min_by{|k,v| v}[0] if !scan_target.empty?  endendclass Game_Event < Game_Character  def enemy?    @event.name.include?('<enemy>')  endendclass Game_Interpreter  def nearest_enemy    $game_map.scan_nearest  endend
EPIC! *Claps*
This sparked a cool idea for a game mechanic. Could even be used to detect ghosts in that type of game

This is a cool script!

Edit: could this also trigger a switch when you find something? Or a self switch in the event it finds?
Hey man! Again thank you for a phenomenal script <3
How difficult would it be to make this work for Move_route script calls as well? I could see that coming in real handy!

Cheers!
Is it possible to only have it active for a certain amount of time and triggered by a skill?
Hey man! Again thank you for a phenomenal script <3

How difficult would it be to make this work for Move_route script calls as well? I could see that coming in real handy!

Cheers!
You are welcome!

I've not tested it but it should work in script calls like: (from the event script box)

$game_map.events[nearest_enemy].moveto(x,y)
Is it possible to only have it active for a certain amount of time and triggered by a skill?
It's not advisable to have a radar running all the time. Especially if you have a large map and lots of events, it can lag.

nearest_enemy should work with any script call box that uses the interpreter class (map, troops, events)

as for

$game_map.distance_from(id)or

$game_map.scan_targetthese should work any where, either from script or script calls or even damage formulas.

But i am not sure how are you going to add that to a skill, it will depends on your scripting/eventing skill.
I didnt mean all the time. I meant it as a triggered scan
I didnt mean all the time. I meant it as a triggered scan
$game_map.scan_targetthis should trigger the scan, store and return with data scanned. (events with <enemy> tag are stored)

$game_map.scan_target["1"]should give you the data(range) of event 1 (event 1 must have <enemy> tag to be listed)

So basically this script only give you either ID or Range of certain event.
Thanks. Rawr
Thanks. Rawr
You're welcome!
The script in the top post is being messed up due to the recent V3 to V4 forum migration.


Please use this alternative version until the web staff is able to find a way to fix the top post.

Code:
#==============================================================================
# ■ Meow Face Enemy Distance Scanner
#------------------------------------------------------------------------------
# Return the ID of the nearest Event (from player position)
#==============================================================================
# How to Use:
# [1] Put this script below Material and above Main
# [2] Put this name tag on events you want to scan: <enemy>
# [3] Use this Script Call to get the closest enemy's id (in Control Variables Script box)
#       closest
#      eg.
#        Control Variables 1 = closest
#==============================================================================
class Game_Map
  def distance_from(id)
    $game_player.distance_x_from($game_map.events[id].x).abs + $game_player.distance_y_from($game_map.events[id].y).abs
  end
  def scan_target
    meowradar = {}
    @events.values.each do |meow|
      if meow.enemy?
        meowscan = distance_from(meow.id)
        meowtarget = (meow.id).to_s
        meowradar[meowtarget] = meowscan
      end
    end
    meowradar
  end
  def scan_nearest
    scan_target.min_by{|k,v| v}[0] if !scan_target.empty?
  end
end
class Game_Event < Game_Character
  def enemy?
    @event.name.include?('<enemy>')
  end
end
class Game_Interpreter
  def nearest_enemy
    $game_map.scan_nearest
  end
end
I can't say how awesome this script has been for my project so far. It's enabled me to let enemies react to a second on screen player (evented). And it's let me create closest enemy target skills for the ABS I'm using. 

So thank you, once more, from the bottom of my heart! <3
You're welcome! 😉
thanks for this
,just a question, 'what's the 'Material' and 'Main' you're talking about in the script? Sorry i'm a noob
@Leonade773x When you open the script editor, there's a left column with the names of the scripts. At the bottom of that column, there's Materials and Main.

Original thread: https://forums.rpgmakerweb.com/threads/enemy-distance-scanner-radar.46115/