Making a button common only effect events the player is touching.

I'm trying to set up a button common event, assigning a key to repels NPCs that the player is touching. So far I have it partially working - using the 'v' key while touching a NPC activates my repel common event, and the NPC is repelled, however - it also effects all other map NPCs (that have the switch call) simultaneously. How would I make it only repel NPCs the player is directly touching at the time? Have tried all sorts of things and I'm stumped. Am sure whatever the solution is will be stupidly obvious.

(I'm using Yanfly's Button common events to tie the button to the my repel event.)

Spoiler: Eventing:
upload_2019-8-13_16-33-28.png


upload_2019-8-13_16-33-54.png

(This second event page is where the repelling happens, by using the 'move away from player' command in custom route)

upload_2019-8-13_16-36-9.png

Thanks!
You need to compare the location of the event to the player before you activate the event.
You need to compare the location of the event to the player before you activate the event.
Could you explain how to do that? I tried figuring out how to do that, but I couldn't make it work.
Screenshot_80.png


Save player x and y to a variable, save event's x and y to a different variable and compare them (or subtract x's from each other and y's from each other). If the difference between either x's or y's is NOT between 1 and -1, then they're not touching.
@Poryg thanks, that all makes sense, except I'm getting tripped up figuring out how to do a conditional branch that compares 2 variables, or is there a way to have a new variable reference the result of the compared variables? Does that make sense?

I've got the xy comparisons set up:
Spoiler
upload_2019-8-13_18-21-52.png
but not sure how to get that data into a conditional branch.
If one of the two is either greater than 1 or less than -1, then they're not touching.
So the push will fire only when the x is no more than 1 and no less than -1 and y is no more than 1 and no less than -1. That's 2 conditional branches for x and 2 for y.
@Poryg I'm sorry, I feel stupid here. I understand the logic of it, I just don't see how to call the x and y difference variables in the program. I can't see a way to assign a new variable ID to the result of NPC X -= Event X in order to check for whether its value is between 1 and -1.
You don't need to assign it to a new variable.
You take var x of event
subtract var x of hero from it. The result is saved inside the x of the event.
@Poryg
Ah ok. Is this what you mean? It's still triggering all events. It seems that regardless of what the restrictions are in order to trigger the NPC-PUSH control switch, once it's triggered it affects all events on the map that respond to the control switch.

Spoiler
upload_2019-8-13_19-31-40.png

Also tried putting it into the event, but same thing happens.
Oh, stupid me!
I forgot that if they are parallel events, they will fire the same commands at the same time. That means the variable and switch will get influenced for all of them.

It might be better if you have a remote event that will check the positions of events and then pushes the appropriate ones. That may involve some scripting.


Save this code as a plugin
Code:
_alias_pushplug_gi_plugincommand = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function (command, args) {
    _alias_pushplug_gi_plugincommand.call(this, command, args);
    if (command === "pushevents") this.pushEvents(args);
}

Game_Interpreter.prototype.pushEvents = function (args) {
    for (var i in args) {
        var event = $gameMap.event(args[i]);
        if (Math.abs(event.x - $gamePlayer.x) <= 1 && Math.abs(event.y - $gamePlayer.y) <= 1) $gameSelfSwitches.setValue([this._mapId, args[i], "A"], true);
        else $gameSelfSwitches.setValue([this._mapId, args[i], "A"]), false;
    }
}

And then from a remote parallel process event call this plugin command:

pushevents 1 2 3 4 5

where numbers are your event IDs. You can have as many as you need.
The plugin will set a self switch "A" to on if the event is next to player. If not, it will turn it off.

So have the page with "A" set to parallel process.
Also, have the movement at the bottom of the page, because parallel process events stop processing the moment they are turned off.
Amazing, thanks @Poryg !

So I'm getting the following error, but I'm not 100% sure I followed your instructions correctly.
Spoiler
upload_2019-8-13_22-54-3.png

- Plugin installed as instructed, that part is fine. (Named NPC-push.js)
- Remote parallel process event - I think I know what you mean, but I just want to confirm that what you mean by a 'remote' event is just a separate event on the same map as the NPC events?
- I don't actually have a page with A set, I was working with a switch called NPC-push, should I replace that with the self-switch, add it in addition to, or does that go somewhere else?
- Leave in the X,Y stuff in the common event I assume?
- Not sure what you mean to 'have the movement at the bottom of the page'? I currently have the movement set through the autonomous movement box of the event page itself.

Thank you, I really appreciate your help.
Remote parallel process event - I think I know what you mean, but I just want to confirm that what you mean by a 'remote' event is just a separate event on the same map as the NPC events?
Yup. Although it looks like you can have it even in a common event, since the interpreter will still catch current map ID (and that is what we need).
I don't actually have a page with A set, I was working with a switch called NPC-push, should I replace that with the self-switch, add it in addition to, or does that go somewhere else?
You should replace it with the self switch. If you turn the same exact switch for all events, then the exact same thing will happen for all events once it is activated. They have to be on a different switch so you don't move all events at once, that's why the self switch.
Leave in the X,Y stuff in the common event I assume?
Unnecessary to keep it there, the plugin will handle all calculation.
Not sure what you mean to 'have the movement at the bottom of the page'? I currently have the movement set through the autonomous movement box of the event page itself.
I see, I thought you had the movement set like this (when the player is supposed to get pushed)
Screenshot_83.png
OK thanks.

I've gone in and corrected those bits, I'm still getting this error message:

Spoiler
upload_2019-8-15_11-54-56.png

What am I missing? This is what I have in my events:
Spoiler
upload_2019-8-15_11-56-58.png


upload_2019-8-15_11-56-29.png


upload_2019-8-15_11-57-39.png
It looks like the plugin command has wrong arguments. You need to use event IDs for it to work. Can I see the plugin command?
It looks like the plugin command has wrong arguments. You need to use event IDs for it to work. Can I see the plugin command?
Oops sorry forgot to post a screenshot of that one!
Spoiler
upload_2019-8-15_12-15-25.png
The format looks fine.
The only way I can replicate an error is if I use a wrong event ID as a parameter. Do all of the provided event IDs exist?

If yes, here is an attempt to make it foolproof. Added a line of code to checks if the event exists and if not, skip it.

Code:
_alias_pushplug_gi_plugincommand = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function (command, args) {
    _alias_pushplug_gi_plugincommand.call(this, command, args);
    if (command === "pushevents") this.pushEvents(args);
}

Game_Interpreter.prototype.pushEvents = function (args) {
    for (var i in args) {
        var event = $gameMap.event(args[i]) || null;
        if (!event) continue;
        if (Math.abs(event.x - $gamePlayer.x) <= 1 && Math.abs(event.y - $gamePlayer.y) <= 1) $gameSelfSwitches.setValue([this._mapId, args[i], "A"], true);
        else $gameSelfSwitches.setValue([this._mapId, args[i], "A"]), false;
    }
}
@Poryg, yeah they do, but the updated script fixed the issue!

The only thing now is that the NPCs get repelled on touch, where I was trying to get them repelled by a keystroke trigger...

Hey, do you have a Patreon, or a game for sale or something? You're spending so much time helping me, I'd love to support you somehow.
A simple upgrade 😀

Code:
Input.keyMapper[33] = 'pageup';
_alias_pushplug_gi_plugincommand = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function (command, args) {
    _alias_pushplug_gi_plugincommand.call(this, command, args);
    if (command === "pushevents") this.pushEvents(args);
}

Game_Interpreter.prototype.pushEvents = function (args) {
    for (var i in args) {
        var event = $gameMap.event(args[i]) || null;
        if (!event) continue;
        if (Input.isPressed("pageup") && Math.abs(event.x - $gamePlayer.x) <= 1 && Math.abs(event.y - $gamePlayer.y) <= 1) $gameSelfSwitches.setValue([this._mapId, args[i], "A"], true);
        else $gameSelfSwitches.setValue([this._mapId, args[i], "A"]), false;
    }
}

Replace the "pageup" (in both the keymapper and the Input.isPressed part) with the key you need. In order to find the correct key you'll need to open Yanfly's button common events plugin and search for the key. Then you copy the Input.keyMapper part and replace it inside the pushevents plugin. For example for q you'll copy
Code:
Input.keyMapper[81] = 'q';
This will eliminate the need for the common event altogether and you can clear it from the Yanfly plugin's parameters.

And I don't have patreon. Even if I had, I wouldn't be able to share it here (forum rules). This is a problem that's not too complex anyway though, I just made it more complex than I needed thanks to the fact that lately I've been fighting exhaustion due to job. It would feel as if I didn't earn the money properly.
@Poryg it's working!

Thank you so much for your help and patience, especially since you've been exhausted. It's really very appreciated.

Hope the job situation gets a bit better!

Original thread: https://forums.rpgmakerweb.com/threads/making-a-button-common-only-effect-events-the-player-is-touching.112125/