Getting name or id of event at specific location (in current map)

I'm trying to build a combat-themed puzzle where you move different types of units (events) to battle positions. I intend on making 24 battle scenes like this, so to save myself endless conditional branches I decided to turn to scripting. There are only going to be so many types of enemy and so many types of unit, and I have a look-up table for success.

Now I can get the location of a specific event, but what I need is to get the name of another event based on specific x and y coordinates. From there I can use conditional branches to identify the matchup.
Using event commands, you can put the x and y values into variables, and use the Get Location Info, which will give you an event id at that location. If you must do it via script, look at Game Interpreter and see what it does behind the scenes when that command is run. It'll be a call to $game_map.event_at or something like that (don't have Ace open atm and in a bit of a hurry)
It's
Code:
$game_map.events_xy(x,y)
The return value is an array, in case somehow your event is pilled up in a single grid.
So you need to add [0]
Code:
$game_map.events_xy(x,y)[0].name
or something
I got the puzzle working, thanks!

If you're interested:

strongholdbattle.png

By going on various quests you'll develop reputation with different types of units which allows you to hire them to defend your stronghold.

So far I have:

Guards (cheap to hire and good vs bandits)
Archers (good vs unarmoured or slow enemies)
Clerics (will join if you help them, good vs undead)
Paladins (will join after stage in main quest, expensive to hire but good vs most except fast enemies)
Pirates (will join if you help them, good vs fast enemies & ghosts)
Dwarves (will join if you help them, good vs enemies found underground)
and Sprites (will join if you help them, good vs enemies vulnerable to fire magic)

Each enemy requires at least 1 unit to fight them off. There can be up to 4 enemies attacking at once. If you match a unit with the wrong enemy, they lose a life (most have 2 lives before you have to hire replacements).

This is the script code that makes it all happen:

Code:
unitev = $game_variables[4]
unitid = unitev - 1

listenemy = ["Bat","Slime","Bandit","Wisp","Zombie","Giant Spider","Ghoul","Dryad","Golem","Wraith","Fergrul","Dragon"]
listbeat = [4, 1044, 600, 617, 295, 754, 4052] # bitwise &

x = $game_map.events[unitev].x
y = $game_map.events[unitev].y + 2
enemy = $game_map.events_xy(x, y)[0].name

return if listbeat[unitid] & (2 ** listenemy.find_index(enemy)) > 0 #successful match!

$game_variables[3] += 1 #counter for how many units you lost
$game_self_switches[[@map_id, unitev, 'B']] = true #show the unit's 'damage' sprite
$game_variables[61 + unitid] -= 1 #unit loses a life

That gets run for each unit in battle. The listbeat array contains numbers based on binary matches for the enemies they beat. So for example, the paladin is: 001010111111. Meaning they don't beat bats or slimes, but do beat bandits, etc. I convert this binary into an integer (4052) and then use a 'bitwise and' to match it against the enemy that I find opposite that unit. This is a neat trick I learned at college.

I don't know whether this battle puzzle is worth showing off as a tutorial somewhere but anyway, that's all I needed, thanks!

Original thread: https://forums.rpgmakerweb.com/threads/getting-name-or-id-of-event-at-specific-location-in-current-map.112841/