[RMXP] Checking if there's a solid event at X, Y coordinates

I'm looking for a script that allows me to check whenever a position in the map is occupied by a solid (Through OFF) event.

My intent is to create a sliding puzzle where the pieces can move at all 4 directions, however, there are 1x2 and 2x1 pieces on the board, which makes collision checking super difficult to figure out, with the only option I've thought of being checking all of the pieces' Xs and Ys, which would be a coding nightmare to pull off.

In order to avoid such fate, I ask for a script that can condense it all to just checking if there's a solid event on the X, Y coordinate the piece wants to go, I believe this would be the easiest way to do this.
There is a minor problem with this if the event is supposed to block two tiles:
even if the sprite is larger, for position and collision purposes only the base tile counts.
if that is a problem then you would still need to check adjacent tiles and event types.

That said, I don't work with RMXP but the more modern makers have an event command "get location info" that would be used for this, does that command exist on XP?
There is a minor problem with this if the event is supposed to block two tiles:
even if the sprite is larger, for position and collision purposes only the base tile counts.
if that is a problem then you would still need to check adjacent tiles and event types.

That said, I don't work with RMXP but the more modern makers have an event command "get location info" that would be used for this, does that command exist on XP?
Well yeah, I do know I would still have to check for two tiles, but it would make the process far easier and less painful than a horde of conditional branches to check if the tiles are available.

And no, there is no "Get location info" as far as I'm aware
Game_Character, which Game_Event is a subclass of, has a method called passable? that you could utilize. It takes in three parameters: the starting tile's X and Y coordinate, and the direction you're trying to move off of it (2, 4, 6, or 8; numbers correspond to the position of the NumPad keys). Because you're checking if the pushable event can move from its current position, we set the coordinates to the event's X/Y.

If using a conditional branch, you'd use the "Script" option and have this as your condition:
Code:
e = $game_map.events[EVENT_ID]; e.passable?(e.x, e.y, DIRECTION)
replacing EVENT_ID and DIRECTION accordingly.
Pro tip: replacing EVENT_ID with @event_id will get this event's ID.

For more intuitive eventing, you can add this script to your project:
Code:
class Game_Character
  def can_move_down?
    passable?(@x, @y, 2)
  end
 
  def can_move_left?
    passable?(@x, @y, 4)
  end
 
  def can_move_right?
    passable?(@x, @y, 6)
  end
 
  def can_move_up?
    passable?(@x, @y, 8)
  end
end
and now your script call would be, e.g.
Code:
$game_map.events[EVENT_ID].can_move_down?

Do you think you'll always be replacing EVENT_ID with @event_id? If so, we can be even more efficient. Add this script:
Code:
class Interpreter
  def this_event
    $game_map.events[@event_id]
  end
 
  def can_move_down?
    this_event.passable?(this_event.x, this_event.y, 2)
  end
 
  def can_move_left?
    this_event.passable?(this_event.x, this_event.y, 4)
  end
 
  def can_move_right?
    this_event.passable?(this_event.x, this_event.y, 6)
  end
 
  def can_move_up?
    this_event.passable?(this_event.x, this_event.y, 8)
  end
end
and now your script call will be
Code:
can_move_down?
IT WORKED!!! THANK YOU SO MUCH!!!

Well, almost

It seems like the piece still goes through other pieces even when all are set to solid
You'll need to share your eventing if you want further help with this. My scripts have nothing to do with that.
Oh, it appears I made a mistake, and now it is working perfectly. Apologies!
Well, I almost like KK20's code but it has a defect that should be corrected first. Why? Because setting an instance variable with a predefined value might have serious effects in the long run.

This is what I mean: the first movable event would work fine without issues. The problem arises when there comes a second or third event and @_this_event is still pointing to the old event. :asad:
Oh yeah, I was under the impression each event had their own Interpreter instance or something. Forgot that only applied to parallel processes.

Edited the post by removing the memoization.

Original thread: https://forums.rpgmakerweb.com/threads/rmxp-checking-if-theres-a-solid-event-at-x-y-coordinates.169426/