MV Using the mouse to trigger an event closest to the player?

Hello, everyone!

I’m new to RPGMaker MV and working on a simple side scroller after watching some tutorials. I noticed that MV has mouse support, so I was wondering: is there a way to click and trigger the nearest event my character's (event box?) is standing on? Instead of clicking the ground where the actual event box is?

(I've tried increasing the events size vertically with DK_Big_Events, but this causes the npc/event to stay in place since he's bigger than the passable tiles when I need him to move at random.)

1749199187722.png


I made a screenshot here to help visualize it if anyone can give some advice or suggest a plugin.


Thank you.
If you haven't tried it already, you can try to set the movement route of the event to "Through", but then you'll probably have to plan it instead of making it random:

1749204948473.png
Since it's in the same line just have a parallel event running that has conditional when touchinput is triggered it starts checking tiles from player x +1, -1, +2, -2, +3, -3 etc with get location info until it finds a tile where it returns > 0 and then use the script to run the event with:
$gameMap.event(Id).start();
Make it so it stops the "scan" after it finds the first event and also make some kind of range where it stops checking with the +/- if it doesn't find anything.

EDIT: Oh and this info is if you want to trigger the event closest to the player like the title asks, not if you only want to trigger the event if the mouse cursor is touching the event sprite.
If you haven't tried it already, you can try to set the movement route of the event to "Through", but then you'll probably have to plan it instead of making it random:

View attachment 342287

Thank you and yes, I was considering this as well (and just have the npc walk around left to right in a custom path) though wanted to check here if there was an alternative.

Since it's in the same line just have a parallel event running that has conditional when touchinput is triggered it starts checking tiles from player x +1, -1, +2, -2, +3, -3 etc with get location info until it finds a tile where it returns > 0 and then use the script to run the event with:
$gameMap.event(Id).start();
Make it so it stops the "scan" after it finds the first event and also make some kind of range where it stops checking with the +/- if it doesn't find anything.

EDIT: Oh and this info is if you want to trigger the event closest to the player like the title asks, not if you only want to trigger the event if the mouse cursor is touching the event sprite.

Thank you for the advice and yes, unfortunately i'm looking specifically for the event to activate on click so long as the click is above the event and the player is close if it's even possible.
Hello, everyone!

I’m new to RPGMaker MV and working on a simple side scroller after watching some tutorials. I noticed that MV has mouse support, so I was wondering: is there a way to click and trigger the nearest event my character's (event box?) is standing on? Instead of clicking the ground where the actual event box is?

(I've tried increasing the events size vertically with DK_Big_Events, but this causes the npc/event to stay in place since he's bigger than the passable tiles when I need him to move at random.)

View attachment 342274

I made a screenshot here to help visualize it if anyone can give some advice or suggest a plugin.

Thank you.
Is the character in the image part of the event editor inside the event or is it a separate sprite/picture/etc that you placed above the event?
Is the character in the image part of the event editor inside the event or is it a separate sprite/picture/etc that you placed above the event?
The foot is part of the event but the character image is bigger than the event itself like that in the picture. It's just above here cause I used paint for the example.
Hmmmm, I tried to check how to do it. Like I copied some functions from MZ to plugin file and put them in Sprite_Character:


Sprite_Character.prototype.isBeingTouched = function() {
const touchPos = new Point(TouchInput.x, TouchInput.y);
const localPos = this.worldTransform.applyInverse(touchPos);
return this.hitTest(localPos.x, localPos.y);
};

Sprite_Character.prototype.hitTest = function(x, y) {
const rect = new Rectangle(
-this.anchor.x * this.width,
-this.anchor.y * this.height,
this.width,
this.height
);
return rect.contains(x, y);
};

Then I made a parallel event that checks:

◆If:Script:TouchInput.isTriggered()
◆Control Variables:#0020 TEST = SceneManager._scene._spriteset._characterSprites.filter((sprite) => sprite.isBeingTouched() && sprite._character._eventId)
◆If:Script:$gameVariables.value(20).length > 0
◆Control Variables:#0020 TEST = $gameVariables.value(20)[0]._character.eventId()
◆Script:$gameMap.event($gameVariables.value(20)).start();
◆
:End
◆
:End
I may have misunderstood, but from what I see what the OP wants is a system that makes the event trigger without the player being in contact with it.


And to prevent the very distant event from interacting you can check with "Conditional Branch" using this:
JavaScript:
$gameMap.distance($gamePlayer.x, $gamePlayer.y, $gameMap.event(this.eventId()).x, $gameMap.event(this.eventId()).y) < 9

If you happen to want the event to be able to interact only within the limit of 8 tiles (just change the value as you prefer):
Code:
◆If:Script:$gameMap.distance($gamePlayer.x, $gamePlayer.y, $gameMap.event(this.eventId()).x, $gameMap.event(this.eventId()).y) < 9
    ◆ (Put the interaction inside the IF)
    ◆
:End

Edit:
@rpgLord69 I was posting at the time you posted, I didn't notice there was a new reply earlier
The stuff I posted above starts the event without contact.

EDIT: Not saying it couldn't be improved on 😀
The foot is part of the event but the character image is bigger than the event itself like that in the picture. It's just above here cause I used paint for the example.
Edit: There are other valid approaches already presented - this may or may not be what you want and you probably don't want to get into messing with the element styles, object interactives, etc. This may also affect your other plugins, unfortunately. Otherwise, feel free to proceed:

Edit of Edit: Please see post below for new update.
Hey, all!

Thank you for the suggestions! I was asleep when these were posted so my reply is a bit late. All the stuff in Javascript, I have to copy paste into a .js file like a plugin right? Then activate it as a plugin in game.
Hey, all!

Thank you for the suggestions! I was asleep when these were posted so my reply is a bit late. All the stuff in Javascript, I have to copy paste into a .js file like a plugin right? Then activate it as a plugin in game.

and yes the character is loaded as an image in the event editor.
Sorry, I should have tested it. You will need to save this here below as a plugin:
JavaScript:
; (($) => {
    const s = 'none';
    const alias1 = $._createModeBox;
    $._createModeBox = function() {
        alias1.call(this);
        this._modeBox.style.pointerEvents = s;
    }
    const alias2 = $._updateVideo;
    $._updateVideo = function () {
        alias2.call(this);
        this._video.style.pointerEvents = s;
    }
    const alias3 = $._updateUpperCanvas;
    $._updateUpperCanvas = function () {
        alias3.call(this);
        this._upperCanvas.style.pointerEvents = s;
    }
    const alias4 = $._updateErrorPrinter;
    $._updateErrorPrinter = function() {
        alias4.call(this);
        this._errorPrinter.style.pointerEvents = s;
    }
})(Graphics);

Then run this script call on your map (run it once) - change $gameMap.event(2) to the event you want to run:
JavaScript:
const spr = SceneManager._scene._spriteset._characterSprites.find(ev =>
    ev._character === $gameMap.event(2));
spr.interactive = true;
spr.on('pointerdown', () => $gameMap.event(2).start());

Should look like this:
s1.png

s2.png

Edit: There are other valid approaches already presented - this may or may not be what you want and you probably don't want to get into messing with the element styles, object interactives, etc. This may also affect your other plugins, unfortunately. Otherwise, feel free to proceed:

Edit of Edit: Please see post below for new update.
Thank you for this, so I have to indicate which event is being clicked on the script each time right? I'm going to lookup an RPGMaker MV javascript tutorial so I know how to better use these.
Thank you for this, so I have to indicate which event is being clicked on the script each time right? I'm going to lookup an RPGMaker MV javascript tutorial so I know how to better use these.
Correct and you only need to run it once on the map.

Happy learning/hunting!
Thank you as well 🙂

Original thread: https://forums.rpgmakerweb.com/threads/using-the-mouse-to-trigger-an-event-closest-to-the-player.178137/