Distinguish between player touching event and event touching player

In the ABS I am developing I would like to be able to distinguish between a player touching an event (enemy) and the enemy touching the player. I am doing that to make the outcome more likely favour the aggressor.


I thought I might be able to do that by duplicating my event page and changing it from player touch to event touch (and changing the entity which receives negative HP appropriately) however that did not work. It did look strange as I ended up with two event pages with Autonomous Movement "Approach" so I am thinking that only the first page activates.
If you have two event pages with the same conditions, only the higher-numbered page will activate (so only your last page, not your first page).


Without a plugin, there is no way to do what you're after.   
Thanks - that explains it. What would I need to do in the plugin? At some point I think I will need to change this to a plugin anyway so I can use this event for other enemy types.



Would I need to somehow work out whether it was the event or player that was moving at the time and its trajectory? 
This should do it for you - save into a .js file in your plugins folder - name doesn't matter.  I'd suggest you put it very high in your plugin list, in case there are other plugins that alias these methods.



Your event trigger needs to be Event Touch (this means the event touches the player OR the player touches the event; Player Touch requires the player to initiate contact).


Game_Player.prototype.startMapEvent = function(x, y, triggers, normal) {
if (!$gameMap.isEventRunning()) {
$gameMap.eventsXy(x, y).forEach(function(event) {
if (event.isTriggerIn(triggers) && event.isNormalPriority() === normal) {
event.setInitiator(1);
event.start();
}
});
}
};

Game_Event.prototype.setInitiator = function(value) {
this._initiator = value;
};

Game_Event.prototype.checkEventTriggerTouch = function(x, y) {
if (!$gameMap.isEventRunning()) {
if (this._trigger === 2 && $gamePlayer.pos(x, y)) {
if (!this.isJumping() && this.isNormalPriority()) {
this.setInitiator(2);
this.start();
}
}
}
};

Game_Event.prototype.checkEventTriggerAuto = function() {
if (this._trigger === 3) {
this.setInitiator(3);
this.start();
}
};




Then in your event command, you can do a conditional branch like this:

Code:
If Script: $gameMap.event(this._eventId)._initiator === 1
  Comment: Player initiated the contact
Else
  If Script: $gameMap.event(this._eventId)._initiator === 2
    Comment: Event initiated the contact
  End
End

and just add the relevant commands in place of the comments.


Let me know how you go with this.  I haven't tested it, so there might be some typos or something (if you get an error, post a screenshot of the console).  I don't think there will be an issue because we haven't pre-defined _initiator - if you do a test and it's not set, it will just be undefined and your conditions should both evaluate to false.


If you have other plugins that alter the behaviour between the player and events, that could cause this to not work, or to not work in all situations.  So they might need to be looked at as well.
Hi,


Thanks! I managed to make this work with a couple of edits.


1. event.setInitiator(2) should be this.setInitiator(2)


2. The conditional logic should be $gameMap.event(this._eventId) (not $gameMap.events(this._eventId))


Apart from that it all seems to be working now thanks!


Tom
thanks.  I'll edit my post to add those fixes, in case anyone else grabs the script.
Perfect solution, thanks a lot!

Original thread: https://forums.rpgmakerweb.com/threads/distinguish-between-player-touching-event-and-event-touching-player.71476/