MZ Diagonal Event Trigger

I have been banging my head against the code long enough. I don't know JS well enough to figure this out on my own.

1734572747614.png

I have diagonal movement working. In this instance the player should be able to jump across to the NW platform using the pillar (move route event). I was able to do this with 'helper events' but I want to learn more about how the engine works and to do it elegantly with JS. I want to trigger events that are diagonal to the player character with keyboard and touch/mouse inputs.

I found some help in discovering leads to a solution in rmmz_core.js:
Game_CharacterBase.prototype.checkEventTriggerTouchFront = function(d) {
const x2 = $gameMap.roundXWithDirection(this._x, d);
const y2 = $gameMap.roundYWithDirection(this._y, d);
this.checkEventTriggerTouch(x2, y2);
};
Game_CharacterBase.prototype.checkEventTriggerTouch = function(/*x, y*/) {
return false;
};
AND
Game_CharacterBase.prototype.moveStraight = function(d) {
this.setMovementSuccess(this.canPass(this._x, this._y, d));
if (this.isMovementSucceeded()) {
this.setDirection(d);
this._x = $gameMap.roundXWithDirection(this._x, d);
this._y = $gameMap.roundYWithDirection(this._y, d);
this._realX = $gameMap.xWithDirection(this._x, this.reverseDir(d));
this._realY = $gameMap.yWithDirection(this._y, this.reverseDir(d));
this.increaseSteps();
} else {
this.setDirection(d);
this.checkEventTriggerTouchFront(d);
}
};
Game_CharacterBase.prototype.moveDiagonally = function(horz, vert) {
this.setMovementSuccess(
this.canPassDiagonally(this._x, this._y, horz, vert)
);
if (this.isMovementSucceeded()) {
this._x = $gameMap.roundXWithDirection(this._x, horz);
this._y = $gameMap.roundYWithDirection(this._y, vert);
this._realX = $gameMap.xWithDirection(this._x, this.reverseDir(horz));
this._realY = $gameMap.yWithDirection(this._y, this.reverseDir(vert));
this.increaseSteps();
}
if (this._direction === this.reverseDir(horz)) {
this.setDirection(horz);
}
if (this._direction === this.reverseDir(vert)) {
this.setDirection(vert);
}
};
These also looked related:
Game_CharacterBase.prototype.canPass = function(x, y, d) {
const x2 = $gameMap.roundXWithDirection(x, d);
const y2 = $gameMap.roundYWithDirection(y, d);
if (!$gameMap.isValid(x2, y2)) {
return false;
}
if (this.isThrough() || this.isDebugThrough()) {
return true;
}
if (!this.isMapPassable(x, y, d)) {
return false;
}
if (this.isCollidedWithCharacters(x2, y2)) {
return false;
}
return true;
};
Game_CharacterBase.prototype.canPassDiagonally = function(x, y, horz, vert) {
const x2 = $gameMap.roundXWithDirection(x, horz);
const y2 = $gameMap.roundYWithDirection(y, vert);
if (this.canPass(x, y, vert) && this.canPass(x, y2, horz)) {
return true;
}
if (this.canPass(x, y, horz) && this.canPass(x2, y, vert)) {
return true;
}
return false;
};

Can anyone explain how the engine pocesses event triggers? and direct me toward implementing diagonal event triggers?
Can anyone explain how the engine pocesses event triggers?
You can see it right at the end of the regular moveStraight() method.

1734617809844.png


There's nothing in the moveDiagonally() method that checks events, so code would have to be added.

However, the larger problem for you is there's no such thing as diagonal facing in MZ.

So anything that refers to directions - such as roundXWithDirection() and roundYWithDirection() inside of checkEventTriggerTouchFront() - will only look in the cardinal direction the character is facing after their diagonal step is completed.

In order to do what you're wanting, you would either have to code new directions corresponding to 1, 3, 7 and 9 and rewrite any direction-dependent functions to account for them; or add some kind of "moved-diagonally" flag to the character which is checked and also accounted for in those places.

It shouldn't be hard to implement for events that are beneath the character and trigger upon touch, so they go off when you move into that tile - those are direction-independent, so just adding the trigger check to the moveDiagonally() method should catch those. But for bumping into events with same as character priority, it's much more complex.
Re diagonal checkEventTriggerTouchFront, here's an excerpt from my plugin Cae_SlopeMove:
JavaScript:
// Alias! On-slope event trigger: Touch.
void (alias => {
  Game_CharacterBase.prototype.moveDiagonally = function(horz, vert) {
    alias.apply(this, arguments);
    if (!this.isMovementSucceeded()) {                            // If move was blocked
      const dx = $.dirToOffset(horz), dy = $.dirToOffset(vert);   // Offsets from input directions
      const dest = { x: this.x + dx, y: this.y + dy };            // Get attempted move location
      if (this.isCollidedWithCharacters(dest.x, dest.y)) {        // If collided
        this.checkEventTriggerTouch(dest.x, dest.y);              // Check trigger
      }
    }
  };
})($.alias.Game_CharacterBase_moveDiagonally = Game_CharacterBase.prototype.moveDiagonally);
I.e. when diagonal movement is blocked, check touch trigger on the blocking tile.
Thank you both, for your responses. I want to confirm I understand what you are saying correctly.
You can see it right at the end of the regular moveStraight() method.

View attachment 326220

There's nothing in the moveDiagonally() method that checks events, so code would have to be added.

However, the larger problem for you is there's no such thing as diagonal facing in MZ.
...
In order to do what you're wanting, you would either have to code new directions corresponding to 1, 3, 7 and 9 and rewrite any direction-dependent functions to account for them; or add some kind of "moved-diagonally" flag to the character which is checked and also accounted for in those places.
...
for bumping into events with same as character priority, it's much more complex.
I will need to calculate and return the dir8 direction while and after the character is moving in a new function, which can be sent to .setDirection(d). Then, it will read that direction in .checkEventTriggerTouchFront(d). I will likely need to redefine roundXYwithDirection so 1,3,7,9 aren't converted to 2,4,6,8.

Re diagonal checkEventTriggerTouchFront, here's an excerpt from my plugin Cae_SlopeMove:
JavaScript:
// Alias! On-slope event trigger: Touch.
void (alias => {
  Game_CharacterBase.prototype.moveDiagonally = function(horz, vert) {
    alias.apply(this, arguments);
    if (!this.isMovementSucceeded()) {                            // If move was blocked
      const dx = $.dirToOffset(horz), dy = $.dirToOffset(vert);   // Offsets from input directions
      const dest = { x: this.x + dx, y: this.y + dy };            // Get attempted move location
      if (this.isCollidedWithCharacters(dest.x, dest.y)) {        // If collided
        this.checkEventTriggerTouch(dest.x, dest.y);              // Check trigger
      }
    }
  };
})($.alias.Game_CharacterBase_moveDiagonally = Game_CharacterBase.prototype.moveDiagonally);
I.e. when diagonal movement is blocked, check touch trigger on the blocking tile.
Can you confirm I understand this code, to help me learn to read JS?

void() is creating an empty space to modify the core script. Alias is a function that modifies the arguments of Game_CharacterBase.prototype.moveDiagonally. I think .apply(this, arguments) will pass (dx, dest).

If a movement does not succeed, then create constants dx, dy & dest.
If collided with character at dest.XY, then check event triggers at dest.XY.

This seems to bypass the need to check for direction facing that is normally used in checkEventTriggerTouchFront.

Side question: I haven't been able to find which triggers are [0,1,2,3]. These in order of the trigger types in the editor?
triggers [0,1,2,3,4] - correspond to Game_Event funtion.

0 = action button
1 = player touch
2 = event touch
3 = parallel
4 = autorun

so those are checked here, so they linked by a lot of stuff or re-direct to them.
from there, it checks more things what the event do on colliding to start the event
based on any of settings.
I will need to calculate and return the dir8 direction while and after the character is moving in a new function, which can be sent to .setDirection(d).
Wait. If you actually want to implement player-controlled 8-directional map movement, then just find an existing plugin for that, e.g.
I would be very surprised if such a plugin didn't implement diagonal event triggers. 👋

void() is creating an empty space to modify the core script.
No, the void operator simply ensures that nothing meaningful is returned from the IIFE. It's not essential.
Alias is a function that modifies the arguments of Game_CharacterBase.prototype.moveDiagonally.
No, alias is a local variable (IIFE argument) that stores the pre-patch method.
I think .apply(this, arguments) will pass (dx, dest).
No, it will pass horz and vert to the original, pre-patch method stored in alias. This ensures that the actual diagonal movement proceeds as usual.
If a movement does not succeed, then create constants dx, dy & dest.
Correct. And if the movement does not succeed, the character has been blocked by something, so...
If collided with character at dest.XY, then check event triggers at dest.XY.
Yes: it checks for event triggers there, and then starts any applicable event.
This seems to bypass the need to check for direction facing that is normally used in checkEventTriggerTouchFront.
In a way, yes: this patch manually determines and checks the target tile for collisions. The checkEventTriggerTouchFront method is only designed for use with moveStraight, so it takes a direction and uses that to calculate the target tile instead.

Here's an edited, standalone version of the code I posted earlier:
Spoiler: plugin code
JavaScript:
/*:
 * @target MV MZ
 * @plugindesc Allow diagonal moves to trigger head-on Touch collisions.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/174012/
 * @help Free to use and/or modify for any project, no credit required.
 */
;void (function(alias) {
"use strict";
  Game_CharacterBase.prototype.moveDiagonally = function(horz, vert) {
    alias.apply(this, arguments);
    if (!this.isMovementSucceeded()) {                            // If move was blocked
      const dx = horz === 4 ? -1 : 1, dy = vert === 8 ? -1 : 1;   // Offsets from input directions
      const dest = { x: this.x + dx, y: this.y + dy };            // Get attempted move location
      if (this.isCollidedWithCharacters(dest.x, dest.y)) {        // If collided
        this.checkEventTriggerTouch(dest.x, dest.y);              // Check trigger
      }
    }
  };
})(Game_CharacterBase.prototype.moveDiagonally);
Same caveats as usual for colliding move routes: remember to either check the "Skip if Cannot Move" option or make sure the collided event can disappear (or move out of the way) on collision.
I greatly appreciate you clarifying all that for me. I'm learning so much.

Wait. If you actually want to implement player-controlled 8-directional map movement, then just find an existing plugin for that, e.g.
I would be very surprised if such a plugin didn't implement diagonal event triggers. 👋

I have two problems with Galv's plugin. 1) It does work with keyboard event triggers but not with touch/mouse. 2) I can't read it well which means I can't modify it well.

I have modified DBS_DiagonalMove. It was a lot more approachable to me than Galv's plugin. I normalized the diagonal walking speed and stopped the unexpected behavior when pressing multiple directions simultaneously.
OK, nice! You're welcome~

Original thread: https://forums.rpgmakerweb.com/threads/diagonal-event-trigger.174012/