Checking for events

Hey. I'd like to have a feature in my game, where if I click on the designated "search" button in my custom UI (like in old adventure games), my character would either say "nothing's here" or they would find an item, depending on where they stand. How can I check for events based on my position? (I do have to stand in the exact spot for it to work)

I should add that every time the character would use the search button in the spot where there is something to find, it would always be an unique interaction (meaning a different thing with a different text). Is there a way to make it so that I don't have to create conditional branches for every single searchable position in the game?
Welcome to the forum!

Before I tried anything fancy, I would try using a specific Region ID.

You could use a specific region ID for each spot that has something to find. Then, your Search UI button would trigger a Common Event that retrieves the Player current Region ID. If it is the correct Region ID, then you would have the Common Event determine what they find. If not, they get the message about finding nothing.

That seems like it would be pretty easy if all the treasure is random.

If you want specific treasure or specific random treasure lists in different places, that would require a bit more planning for your Common Event.

At least that is what I would try to experiment with first.
The Get Location Info command can be used to get tile info like Zombie mentioned, and store it in a specified variable. It has an option to use the current position of a character, e.g. the player.

To check e.g. the tile 1 step in front of the player, you can store the player's X/Y map coordinates in a couple of variables using Control Variables, then add/subtract from those based on the player's current facing. Then use those variables in Get Location Info. E.g.
  • Comment: Get player coordinates.
  • Control Variables {Search X} -> Set -> Game Data -> Character -> Player, Map X.
  • Control Variables {Search Y} -> Set -> Game Data -> Character -> Player, Map Y.

  • Comment: Adjust for player facing.
  • Conditional Branch -> Character -> Player is facing Down:
    • Control Variables {Search Y} -> Add -> Constant: 1.
  • Conditional Branch -> Character -> Player is facing Left:
    • Control Variables {Search X} -> Sub -> Constant: 1.
  • Conditional Branch -> Character -> Player is facing Right:
    • Control Variables {Search X} -> Add -> Constant: 1.
  • Conditional Branch -> Character -> Player is facing Up:
    • Control Variables {Search Y} -> Sub -> Constant: 1.

  • Comment: Get info for that tile.
  • Get Location Info: {Search ID}, Event ID, ({Search X}, {Search Y})
Yeah and you can store the show text and what item to get in the events. Like when you get the event id with what's shown above in your search common event, you can use something like script:

// id you stored, change X to the correct variable id
const eventId = $gameVariables.value(X);
// map id
const mapId = $gameMap.mapId();
// use self switch page so you can't start event with action button
$gameSelfSwitches.setValue([mapId, eventId, 'A'], true);
// start event
$gameMap.event(eventId).start();

(Then at the end of the events use control self switches A OFF)
(And in your search common event you can put the show text "nothing was found" if you get 0 for the event id)

EDIT: Oh yeah, also make checks that you don't start it with 0 event id and make checks that if you already got that item, you don't get it again.
The Get Location Info command can be used to get tile info like Zombie mentioned, and store it in a specified variable. It has an option to use the current position of a character, e.g. the player.

To check e.g. the tile 1 step in front of the player, you can store the player's X/Y map coordinates in a couple of variables using Control Variables, then add/subtract from those based on the player's current facing. Then use those variables in Get Location Info. E.g.
  • Comment: Get player coordinates.
  • Control Variables {Search X} -> Set -> Game Data -> Character -> Player, Map X.
  • Control Variables {Search Y} -> Set -> Game Data -> Character -> Player, Map Y.

  • Comment: Adjust for player facing.
  • Conditional Branch -> Character -> Player is facing Down:
    • Control Variables {Search Y} -> Add -> Constant: 1.
  • Conditional Branch -> Character -> Player is facing Left:
    • Control Variables {Search X} -> Sub -> Constant: 1.
  • Conditional Branch -> Character -> Player is facing Right:
    • Control Variables {Search X} -> Add -> Constant: 1.
  • Conditional Branch -> Character -> Player is facing Up:
    • Control Variables {Search Y} -> Sub -> Constant: 1.

  • Comment: Get info for that tile.
  • Get Location Info: {Search ID}, Event ID, ({Search X}, {Search Y})

Thank you, what would be the best way to tell the player which specific item they're going to get based on their position? Do I have to do that in common events as well based on conditional branches, or is there a simpler way?
what would be the best way to tell the player which specific item they're going to get based on their position?
That would depend on what you're using to determine what the player gets.

if it is event-dependent, then there are very few ways other than comparing event IDs.
one other possibility would be to use a notetag on the event and read that with the meta structure once you have the event ID - but I don't know if MZ still supports meta or if you'll need a plugin for that.
My first thought on this was just "isn't this just normal event interaction with a key? what's the difference from just making the button interact with the event?".

Is this search separate from normal interaction? Meaning, there are technically two different ways you could interact with a tile?

If that's the case, I have a different method than others suggested that could work. You said they have to be on the tile being searched, rather than adjacent. So, make two common events:

Search (trigger: manual, by clicking the button)

  • Control Switch: Searching ON

Search Fail (trigger: parallel; switch: Searching)

  • Wait 30 frames (or whatever number you want)
  • Show Text ("Nothing was found.")
  • Control Switch: Searching OFF

Then place an event on the tile where something will be found, and add an event page that's activated by the Searching switch. It should be set to Autorun and look something like this:

  • If: Script: this.character(0).pos($gamePlayer.x, $gamePlayer.y)
    • Give an item or whatever you need to do here
    • Control Switch: Searching OFF
Another suggestion, a kind of blend of what rpgLord69 and Solar_Flare have mentioned: you could turn on a switch to toggle "Searching" behaviour, then invoke the usual "Action Button" self-tile trigger. E.g.
  • Turn on searching.
    Control Switches: {Searching} ON.

  • Wait 1 frame to update applicable events to their "Searching" page.
    Wait: 1 frame

  • Perform an "Action Button" trigger on the player's tile.
    Script: $gamePlayer.checkEventTriggerHere([0]);

  • Turn off searching immediately if no event was triggered.
    Otherwise, rely on the searching event to turn off the switch when appropriate.

    Conditional Branch -> Script: !$gameMap.isAnyEventStarting()
    • Control Switches: {Searching} OFF.
I'd also consider doing this using a Conditional Branch rather than via a new event page, e.g.
  • If {Searching} is ON:
    • ("Search" event.)
    • Control Switches: {Searching} OFF.
    Else:
    • (Normal event.)
However, separate pages might be better e.g. if only certain events can be searched, or if you want events to temporarily appear different while searching.

Original thread: https://forums.rpgmakerweb.com/threads/checking-for-events.182204/