How to make a common event reference an adjacent event?

Hi!

Here's what I'm trying to do and can't work out the method. I know it can be done in pure eventing, I'm just missing something (knowledge mostly lol).

I am using a plugin to call common events with button bindings, and that works great. But they are just calling fairly simple CE's. What I'd like to do is have the player flash a special hand gesture when they're next to an NPC. It can't really use the regular action button because I need the hand gesture to be completely separate from their interaction with the NPC. So, I'd like to bind a button to call a CE that will do some separate stuff if they flash the hand gesture.

I can't figure out a few things here -- first off would be, whether to try and run the function inside the adjacent NPC event, or to do it all centrally in the one "hand gesture" common event. They both present problems so far as I can tell: if it's all in the CE, I need to check to see if an event is in an adjacent tile, and then I need to check which NPC it is.

But if I put everything in the NPC event, then how do I even go about triggering it without using the action button?

Hope I've explained it well enough. Happy for any thoughts or advice or guidance -- thank you so much!
To make sure I understand this correctly: you have some events that you can interact with normally with the action button. However, you have an additional action that shows a hand gesture and if performed next to some events result in a different reaction. Is that reaction unique to each event, or is it the same for all of them?

Either way, the best way is probably to put most of the logic in a common event, so that you're not duplicating it across every npc. Detecting is there's an npc (I'm assuming it's e.g. an event with a specific notetag) nearby it would be a fairly simple scriptcall, I'd be happy to make that for you once you confirm that this is what you need.

If the reaction is always the same, that's all you'd need. If each event has a unqiue one, have a switch that turns on when the gesture is used, and an extra event page for each event with that switch as a condition. Then just run that event from the common event.
and if you don't want to use script calls, that can be done completely with events as well - it's just a bit of a list of event commands that are needed.

first, two variables for the XY of the player.
then four conditional branches checking facing, with different +-1 on the coordinate to get the position in front of the player in the direction he's facing.

then get location info on the new coordinates to get the event ID on that position.
if get location info returns zero then there is no event there.
otherwise use the event number to decide which event it is and what to do.
To make sure I understand this correctly: you have some events that you can interact with normally with the action button. However, you have an additional action that show a hand gesture and if performed next to some events result is a different reaction. Is that reaction unique to each event, or is it the same for all of them?

You have it exactly right!

The reaction would be different depending on the NPC, yes. I'm a little confused about how I would have different event pages, if everything is being done within the common event?

I would be very grateful for any help with how to do this with script calls!

then get location info on the new coordinates to get the event ID on that position.
if get location info returns zero then there is no event there.
otherwise use the event number to decide which event it is and what to do.

Andar, wouldn't I have to check for the Map ID as well? Would you advise that doing it this way might create a rather messy event if using only eventing? So many conditionals within conditionals, I could kinda see it turning into a mess?
There are two problems here that can be mostly solved independently - getting the ID of the event to run, and running the reaction for that event.

As to the first one, you get it with this script:
$gameMap.eventsXy($gameMap.roundXWithDirection($gamePlayer.x, $gamePlayer.direction()), $gameMap.roundYWithDirection($gamePlayer.y, $gamePlayer.direction()))[0]?.eventId();, or with commands as described by Andar.

As to the second one, you could then have a bunch of conditional branches for each map and event id and their reactions there, though I wouldn't really recommend that if you plan on having a significant amount of those events. It would work, but it'd be quite a pain fo deal with.

A simpler and more manageable way would be to have that reaction in each of those events - that way it will be a lot easier to find it when you if want to change it later. You could do this (in your common event):
JavaScript:
let event = $gameMap.eventsXy($gameMap.roundXWithDirection($gamePlayer.x, $gamePlayer.direction()), $gameMap.roundYWithDirection($gamePlayer.y, $gamePlayer.direction()))[0];
if(event && event.event().meta.HasReaction){
    $gameSwitches.setValue(42, true);
    event.refresh(); //This is needed for it to update which page will be ran
    event.start();
}
This will run the event in front of the player if it has a <HasReaction> notetag. In each of those events, create a page with switch 42 as condition, and make sure to turn that switch off at the end of that page.
Note that running an event with event.start() means that it will only run after the common event is finished, so you can't really have anything else happen after it.

Alternatively, a slightly simpler version if you have RunNearbyEvent:
JavaScript:
let event = $gameMap.eventsXy($gameMap.roundXWithDirection($gamePlayer.x, $gamePlayer.direction()), $gameMap.roundYWithDirection($gamePlayer.y, $gamePlayer.direction()))[0];
if(event && event.event().meta.HasReaction){
    MAC_RunNearbyEvent.run(event.eventId(), this, event.event().pages.length);
}
This will run the last page of that event, ignoring its conditions (so you don't need to set the switch off in every one of them). It will also allow you to have more things in the common event after this if you need them.
There are two problems here that can be mostly solved independently - getting the ID of the event to run, and running the reaction for that event.

As to the first one, you get it with this script:

Insanely grateful for this help, Mac ... I'll do my best to implement and will report back. Really impressed at how you seemed to grok the problem so quickly, I was worried I hadn't explained it well but you got it. Will edit here with updates.
Andar, wouldn't I have to check for the Map ID as well? Would you advise that doing it this way might create a rather messy event if using only eventing? So many conditionals within conditionals, I could kinda see it turning into a mess?
yes, that's why I said it will be a list of commands.
But partially it depends on what exactly you want to do and how you organise the events.

It is possible to presort events and make sure that the IDs you want are the same over all maps, but usually that works only if you have less than 10 or 20 events for this on each map.

For more events or if you can't presort, then a scripted solution is better.
There are two problems here that can be mostly solved independently - getting the ID of the event to run, and running the reaction for that event.

Hi again @Mac15001900, you appear to be a little bit of a wizard when it comes to this stuff, because this works!

Before reporting as closed, would you mind if I asked a few quick questions about this solution so I understand it better and can extend it later on, on my own?

First off, I got it working without the RunNearbyEvent script -- that solution seems a little odd to me, since you say it calls the last page of the event regardless of conditions? Wouldn't that cause any action button interaction with the event to run that last page first?

Second, assuming 42 is the switch, why is it that I need to turn that switch off in the event page? In my implementation, you're perfectly fine to flash the secret gesture over and over, and I can control what happens within that event page, like maybe the NPC starts saying "yes, I see it, all right already" etc. But is there some other reason why I am turning off that switch?

In any case, floored by this one, you just nailed the problem, and shot out a solution with a terrific explanation. Grateful.

Thanks also @Andar as always.
I'm glad it worked 🙂
Wouldn't that cause any action button interaction with the event to run that last page first?
I probably should have clarified: the last page would have a condition that's never true (like a switch that's always off). So it can only be used with this special action.
why is it that I need to turn that switch off in the event page
I was assuming that you'd still want to have regular interaction not trigger that page - so that after you use the gesture it'd run the gesture page, but if you then interacted with it normally afterwards it would run a regular interaction page. If that's not the case then I guess you don't need to turn it off.
I'm glad it worked 🙂

I probably should have clarified: the last page would have a condition that's never true (like a switch that's always off). So it can only be used with this special action.

I was assuming that you'd still want to have regular interaction not trigger that page - so that after you use the gesture it'd run the gesture page, but if you then interacted with it normally afterwards it would run a regular interaction page. If that's not the case then I guess you don't need to turn it off.

Got it! Makes total sense to me now.

This was a biggie -- really floored at the solution, and so appreciative that you took the time.

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/how-to-make-a-common-event-reference-an-adjacent-event.167207/