How to check nearest Event id from this event?

miniet

Villager
Member
Joined
Sep 4, 2015
Messages
9
Reaction score
1
Primarily Uses
Does anybody knows how to check nearest Event ID from this Event(@event_id)?
ex) This Event(1) -> Event(2) is nearest distance from event(1)
need to know Event(2)'s ID with script down below or another script can run this.

I use meowface's script but it is player based so wanna change "player" part to "this_event".
but changing "$game_player" to "$game_map.events[@event_id]" doesn't seem to work.
and nametag based searching event ID could be more helpful.

and i use Rpgmaker vx ace version. sorry i didn't mention that.

attached meowface's full script and link below
https://forums.rpgmakerweb.com/index.php?threads/enemy-distance-scanner-radar.46115/

====================================================================
# ■ 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
 
Last edited:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
39,952
Reaction score
13,586
First Language
English
Primarily Uses
RMMV
You should say whether you're using RPG Maker XP, VX, or VX Ace as they are all different in little ways.


$game_map.events[id1].distance_x_from($game_map.events[id2].x).abs + $game_map.events[id1].distance_y_from($game_map.events[id2].y).abs

However, I don't think the above will get you the nearest event.

You could have two events that are on the same tile horizontally, but one is 10 tiles higher than the other. The above would return 10. Then you could have two events that are 5 tiles apart horizontally and 6 tiles vertically. The above would return 11. Clearly, the event that is 5 and 6 tiles away is closer, but by that formula, it would give you the event that's 10 tiles away.
 

miniet

Villager
Member
Joined
Sep 4, 2015
Messages
9
Reaction score
1
Primarily Uses
Oh i forgot to mention my version of rpgmaker
It's VX Ace version. and it's an honor to have reply of yours. shaz!
You're right about that formula, Event(2) would not be the nearest one but
My focus is how to change player part to event(self) with above script.
Checking nearest one could be next subject i guess.
 
Last edited:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
39,952
Reaction score
13,586
First Language
English
Primarily Uses
RMMV
I gave you the code to check the distance between two events :)
 

miniet

Villager
Member
Joined
Sep 4, 2015
Messages
9
Reaction score
1
Primarily Uses
Thanks I'll try that right now.
 

kyonides

Reforged is laughable
Veteran
Joined
Nov 17, 2019
Messages
285
Reaction score
70
First Language
English
Primarily Uses
RMXP
I won't say the code above won't work, it should work, but let's say it's a bit convoluted for a newbie or anybody using that call too often so I'd propose to add the following scriptlet that will provide the same features in a convenient way.

Code:
class Game_Event
  def distance_from(event)
    distance_x_from(event.x).abs + distrance_y_from(event.y).abs
  end
end
So now the script call will look like this:

$game_map.events[id1].distance_from($game_map.events[id2])
 

miniet

Villager
Member
Joined
Sep 4, 2015
Messages
9
Reaction score
1
Primarily Uses
Thanks to reply of yours. kyonides!
I'm still hard to figure out how to get distance with this event(1) and other event(2)
Event(1) would be (event_id : event myself) so doesn't need to fix the id of event
Event(2) will be the target event which has a name of "<enemy>"
and script will gather id and distance of events(has a name of "<enemy>") into array
then "nearest_enemy" will call first event id of nearest distance from array

Some tips i've accepted here and there, i've changed above script like below.
first i've made script "$game_variables[1] == event_id" to get this event's id with page of one event.
and changed above script like this
$game_map.events[$game_variables[1]].x.distance_x_from($game_map.events[id].x).abs + $game_map.events[$game_variables[1]].y.distance_y_from($game_map.events[id].y).abs
and made script "$game_variables[2] == nearest_enemy"
then calculate distance between $game_variables[1]'s x/y and $game_variables[2]'s x/y

It does work though but I think more elegant version of this will be possible..
I don't know how to get this without primitive variables ways.
 
Last edited:

A-Moonless-Night

WINTER IS COMING
Veteran
Joined
Mar 17, 2012
Messages
667
Reaction score
431
First Language
English
Primarily Uses
RMVXA
Try this:
Ruby:
class Game_Character < Game_CharacterBase
  def distance_from(chara)
    distance_x_from(chara.x).abs + distance_y_from(chara.y).abs
  end
end

class Game_Interpreter
  def distance_from(id1, id2)
    get_character(id1).distance_from(get_character(id2))
  end
end
Now you can just use the script call distance_from(ID1, ID2), where ID1 and ID2 are the IDs of the event or player (player is -1).
 

kyonides

Reforged is laughable
Veteran
Joined
Nov 17, 2019
Messages
285
Reaction score
70
First Language
English
Primarily Uses
RMXP
Try this:
Ruby:
class Game_Character < Game_CharacterBase
  def distance_from(chara)
    distance_x_from(chara.x).abs + distance_y_from(chara.y).abs
  end
end

class Game_Interpreter
  def distance_from(id1, id2)
    get_character(id1).distance_from(get_character(id2))
  end
end
Now you can just use the script call distance_from(ID1, ID2), where ID1 and ID2 are the IDs of the event or player (player is -1).
Yeah, you pretty much changed the class where distance_from should be defined, I guess that should help him calculate it no matter if he's looking for an event's or player's current distance.
OP should take advantage of the short distance_from method she included in the Game_Interpreter class for it saves him a lot of typing. :D
Every method you include in the Game_Interpreter class can be invoked directly without any "prefix". So the following call:

Code:
distance_from(-1, 25)
Would let you check the distance between the player and the event number 25.

Keep in mind that it might throw an error if the named event doesn't exist! :o
They're not sorted by ID so you can have an event number 21 and the next could be number 32. :s That happens when you keep deleting old events.
 

miniet

Villager
Member
Joined
Sep 4, 2015
Messages
9
Reaction score
1
Primarily Uses
Many Thanks again A-Moonless-Night & kyonides
I'm gonna adapt above script line into mine right away~
now it's the time to have some fun! haha,
finally my NPC event can track down nearest another Enemy event's ID and distance
and kill him with firearms if he is in range.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Ami
--- Cheating Part 2 ---

F.Healer: Ahh,This item really expensive!
F.Mage: What should we do?
Hero: Don't worry,i can use this (Activate 999999999G,Suddenly The Game Police Is Watching)
Game Police:Ehem!!! (I See You Gesture)
Hero: (Gulp)...Just...forget it...

Second bust is almost ready. I have to stop for now, but I wanted to show him off. Just have to do a handful of edits and the rest of his expressions will be done.
Kupotepo wrote on casper667's profile.
Thank you @casper667 for make many amazing plugins. Do you see right @hiddenone make the blogs about your plugins?
Does anyone know where the MZ PluginEditorDemo.js can be found? It might be called SampleCoreScript now. I'd like to see what changed from MV.
Among Us w/friends stream

Forum statistics

Threads
103,337
Messages
998,864
Members
134,845
Latest member
nenektogel
Top