XP Detecting if an event is on specific coordinates

I'm trying to detect the location of an event to prevent events from moving onto each other when both have through turned on. The reason they're like that is so the player can stand on them to interact and move them to a chosen location, but they can't walk off the area (which would be possible if the player had through on instead). This is the script I'm using to do that, but I don't know how to use the result from it after that.
Code:
$game_map.check_event(
$game_variables[13],
$game_variables[14])

For reference, that calls this from Game_Map
Code:
#--------------------------------------------------------------------------
  # * Get Designated Position Event ID
  #     x          : x-coordinate
  #     y          : y-coordinate
  #--------------------------------------------------------------------------
  def check_event(x, y)
    for event in $game_map.events.values
      if event.x == x and event.y == y
        return event.id
      end
    end
  end
I tried using the script to change another variable and check that with a conditional branch, but when there wasn't an event, it crashed the game since the variable's result wasn't a number.

I also tried another way with this and placed it directly into a conditional branch:
Code:
def check_for_event(x, y)
    for event in $game_map.events.values
      if event.x == x and event.y == y
        return true
      end
    end
  end
Using that made it work, but if it wasn't true, then the action for it not being true didn't happen.

Is there a way to have it be used directly in a conditional branch and return true if there's an event instead of returning the event id when it gets one?
Hello,

Your script does not work as intended because you don't return a value for all cases, leading to unexpected behavior when there is no event.
For you first method, if there is no event, you can return an id that is not defined (like 0):
Ruby:
  def check_event(x, y)
    for event in $game_map.events.values
      if event.x == x and event.y == y
        return event.id
      end
    end
    return 0 #no event found
  end

For your second method, that you can use in a conditional branch, you should return false if you don't find the event:
Ruby:
  def check_for_event(x, y)
    for event in $game_map.events.values
      if event.x == x and event.y == y
        return true
      end
    end
    return false #no event was found
  end

As a side-note, in ruby, there are shorter ways to find if there is an event or not.

You can do: (where x,y are your position)
Ruby:
$game_map.events.values.find{|ev| ev.x == x && ev.y == y}
And it will return the event found, and so you can build something like this:
Ruby:
  def check_event(x,y)
    e = $game_map.events.values.find{|ev| ev.x == x && ev.y == y}
    return e ? e.id : 0 #if an event was found, return the id, else return 0
  end

If you don't care about the event itself, you can use .any? instead of .find
Ruby:
$game_map.events.values.any?{|ev| ev.x == x && ev.y == y}
This line will be true if there is an event at (x,y) and false otherwise.
Timtrack said:
If you don't care about the event itself, you can use .any? instead of .find
Ruby:
$game_map.events.values.any?{|ev| ev.x == x && ev.y == y}
This line will be true if there is an event at (x,y) and false otherwise.

Yes, you can use that OR use one? instead. At the end of the day, the OP would only need 1 single event to be there at a given point, right?

EDIT:
Ruby 1.8.7 does include it, the problem is that RMXP and RMVX running on 1.8.1 had never implemented it.
RMVX ACE can take advantage of it without issues. :S

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/detecting-if-an-event-is-on-specific-coordinates.182537/