MZ No Turn Plugin Help

Hello,

I am still struggling to understand how plugins work. I've got this one made up that is intended to enable/disable the script that turns an event towards the player when you interact with it.

Unfortunately, it isn't working, and I have no idea why.

Spoiler: NoTurnEvent
//=============================================================================
// NoTurnEvent.js
//=============================================================================

/*:
* @plugindesc Prevents events from turning when interacted with.
* @Author Connor
*
* @Help This plugin prevents events from turning when the player interacts with them.
*
*/

(function() {
// Alias the Game_Event.prototype.turnTowardPlayer function
const _Game_Event_turnTowardPlayer = Game_Event.prototype.turnTowardPlayer
Game_Event.prototype.turnTowardPlayer = function() {
// Check if the event is configured to not turn
if (!this._noTurn) {
// If not, proceed with normal turning behavior
_Game_Event_turnTowardPlayer.call(this);
}
};

// Create a plugin command to enable or disable turning for specific events
PluginManager.registerCommand('NoTurnEvent', 'EnableTurning', args => {
const eventId = parseInt(args.eventId);
const event = $gameMap.event(eventId);

if (event) {
event._noTurn = false;
}
});

PluginManager.registerCommand('NoTurnEvent', 'DisableTurning', args => {
const eventId = parseInt(args.eventId);
const event = $gameMap.event(eventId);

if (event) {
event._noTurn = true;
}
});
});

My best guess was that I am not correctly grabbing the eventId, but even so, the commands don't show up in the Plugin Command list, so I'm sure I've screwed those up as well.

I've also tried writing script into an event to manually trigger _noTurn (this._noTurn = true), but that got me nowhere.

I'm also not confident that this is compatible with RMMZ, as I can only ever seem to find posts/advice about RMMV.

Thank you for your time.
You need to save it as a .js file, then import it into your project via the editor:
  1. Copy + paste the code into a text editor (e.g. Notepad).
  2. File > Save As:
    • File Type: All Files
    • Filename: whatever.js
    (This works because .js files are plaintext.)
  3. Put that file in your project's js/plugins subfolder.
  4. In the editor, go to Tools -> Plugin Manager and add the new plugin to the list.
  5. Save your project to apply Plugin Manager changes.
  6. Test!
That plugin seems over-complicated, though, and I think it will interfere with the standard "Turn Toward Player" move command. I'd suggest something like this instead:
Spoiler: plugin code
JavaScript:
/*:
 * @target MZ
 * @plugindesc Disables event "locking" (turn to face player on interact).
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/161949/
 * @help Free to use and/or modify for any project, no credit required.
 */

// Patch - only enable event locking when switch 1 is on.
void (alias => {
  Game_Event.prototype.lock = function() {
    if ($gameSwitches.value(1))
      alias.apply(this, arguments);
  };
})(Game_Event.prototype.lock);

// Alternative: override - disable event locking.
// Game_Event.prototype.lock = function() {};

Alternatively you can use the Direction Fix checkbox on events (bottom-left of event-editing window). Direction Fix can also be toggled through a move route.

Direction Fix is commonly used for treasure chest events: for an example, right-click the map in event mode and choose Quick Event Creation -> Treasure. This will give you a few options, then make the event for you. You can then have a look at the commands to see how it's done. 👋
Thank you for the reply.

I wasn't aware of locking, I had assumed "turnTowardPlayer" was what the game was using for that.

Unfortunately, Direction Fix doesn't work for my purposes. I have enemies react differently depending on which side you approach them from. I used Direction Fix to test that my code for that works, but it inhibits their ability to wander.

Your code works great, I'm going to need to experiment a little with toggling it though. Trying to enable it inside the event just has the switch flip AFTER the locking has already triggered. Enabling it in a parallel event means I'll be stuck with 1 option for each map.
I have enemies react differently depending on which side you approach them from. I used Direction Fix to test that my code for that works, but it inhibits their ability to wander.
I was thinking something like: Move Forward, Direction Fix OFF, Turn Left, Direction Fix ON, etc. But yea, that would create a brief pause when they turn, since by default at most 1 move command is processed per character per frame.

For an event-specific thing, you could try this instead:
Spoiler: plugin code v2
JavaScript:
/*:
 * @target MZ
 * @plugindesc Can disable event "locking" (turn to face player on interact).
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/161949/
 * @help You can use this in a map event's Note box:
 *   - Always disable locking for this event:
 *       <noLock>
 *
 * You can use these script calls in a move route:
 *   - Disable locking for character being moved:
 *       this._noLock = true;
 *   - (Re-)enable locking for character being moved:
 *       delete this._noLock;
 *
 * Or these commands in a Script command (not in a move route):
 *   - Disable locking for this event:
 *       this.character(0)._noLock = true;
 *   - (Re)enable locking for this event:
 *       delete this.character(0)._noLock;
 *
 * Or these script calls anywhere:
 *   - Disable locking for map event 12:
 *       $gameMap.event(12)._noLock = true;
 *   - (Re)enable locking for map event 12:
 *       delete $gameMap.event(12)._noLock;
 *
 * Free to use and/or modify for any project, no credit required.
 */
;void (alias => {

  /**
   * @param {Game_Event} reference event
   * @returns {boolean} `true` iff `event` is allowed to lock on.
   */
  const canLock = function(event) {
    return !("_noLock" in event || event.event().meta.noLock);
  };

  // Patch - disable event locking if appropriate.
  Game_Event.prototype.lock = function() {
    if (canLock(this))
      alias.apply(this, arguments);
  };

})(Game_Event.prototype.lock);
Check its help section for details~

[Edit - on reflection, you might actually want a patch like this instead:
JavaScript:
;void (alias => {
  // ...
  Game_Event.prototype.lock = function() {
    alias.apply(this, arguments);
    if (!canLock(this))
      this.setDirection(this._prelockDirection);
  };
})(Game_Event.prototype.lock);
This allows locking as usual, it simply reverts the change of direction for "non-locking" events. That's because I forgot something important earlier: an event's auto-movement will be paused while it is locked.]
I was thinking something like: Move Forward, Direction Fix OFF, Turn Left, Direction Fix ON, etc. But yea, that would create a brief pause when they turn, since by default at most 1 move command is processed per character per frame.
You can see if NRP_QuickMovementRoute fixes this issue. Plugin file has English instructions
this plug-in eliminates the gaps between commands
when "Set Movement Route" is used.
In addition to changing the image and direction,
it also has effects on transparency,

turning switches on and off, etc.
This works excellently caethyril, thank you very much. I wasn't aware you can put commands in the note box, that will be very helpful.

I was thinking something like: Move Forward, Direction Fix OFF, Turn Left, Direction Fix ON, etc. But yea, that would create a brief pause when they turn, since by default at most 1 move command is processed per character per frame.

For an event-specific thing, you could try this instead:
Spoiler: plugin code v2
JavaScript:
/*:
 * @target MZ
 * @plugindesc Can disable event "locking" (turn to face player on interact).
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/161949/
 * @help You can use this in a map event's Note box:
 *   - Always disable locking for this event:
 *       <noLock>
 *
 * You can use these script calls in a move route:
 *   - Disable locking for character being moved:
 *       this._noLock = true;
 *   - (Re-)enable locking for character being moved:
 *       delete this._noLock;
 *
 * Or these commands in a Script command (not in a move route):
 *   - Disable locking for this event:
 *       this.character(0)._noLock = true;
 *   - (Re)enable locking for this event:
 *       delete this.character(0)._noLock;
 *
 * Or these script calls anywhere:
 *   - Disable locking for map event 12:
 *       $gameMap.event(12)._noLock = true;
 *   - (Re)enable locking for map event 12:
 *       delete $gameMap.event(12)._noLock;
 *
 * Free to use and/or modify for any project, no credit required.
 */
;void (alias => {

  /**
   * @param {Game_Event} reference event
   * @returns {boolean} `true` iff `event` is allowed to lock on.
   */
  const canLock = function(event) {
    return !("_noLock" in event || event.event().meta.noLock);
  };

  // Patch - disable event locking if appropriate.
  Game_Event.prototype.lock = function() {
    if (canLock(this))
      alias.apply(this, arguments);
  };

})(Game_Event.prototype.lock);
Check its help section for details~
Great, you're welcome!

<noLock> is not really a "command", it's a standard-format "note-tag". The engine automatically parses tags in that format from applicable note fields, and stores the result in the object's meta property. The plugin then simply checks if meta.noLock exists on the initial event. 👋

Original thread: https://forums.rpgmakerweb.com/threads/no-turn-plugin-help.161949/