Can't get off Vehicle Sound MZ

This script adds a sound if player is on a vehicle and cannot exit from it.
If you use it, please add "Evil95" to the credits. Thank you.

Updated to 1.2.


Spoiler: Version 1.2
JavaScript:
//==============================================================================
// Cant get off Vehicle Sound
// -----------------------------------------------------------------------------
// Version 1.2 (19.06.2023)
// Von Evil95
//==============================================================================
/*:de
 * @plugindesc v1.2 Lässt ein Sound ertönen, sofern man nicht mit einem Fahrzeug laden oder austeigen kann.
 * @author Evil95
 * @target MZ
 *
 * @param SE
 * @type file
 * @dir audio/se/
 * @desc Wähle ein SE aus.
 * @default Buzzer1
 *
 * @param Volume
 * @type number
 * @desc Lautstärke.
 * Wert zwischen 0 und 100 eingeben.
 * @min 0
 * @max 100
 * @default 100
 *
 * @param Pitch
 * @type number
 * @desc Tonhöhe.
 * Wert zwischen 50 und 150 eingeben.
 * @min 50
 * @max 150
 * @default 100
 *
*/
/*:en
 * @plugindesc v1.2 Makes a sound if you cannot exit a vehicle.
 * @author Evil95
 *
 * @param SE
 * @type file
 * @dir audio/se/
 * @desc Select SE audio file.
 * @default Buzzer1
 *
 * @param Volume
 * @type number
 * @desc Enter value between 0 and 100.
 * @min 0
 * @max 100
 * @default 100
 *
 * @param Pitch
 * @type number
 * @desc Enter value between 50 and 150.
 * @min 50
 * @max 150
 * @default 100
 *
*/

var Imported = Imported || {};
Imported.Evil95_CantGetVehicleOffSound = true;

(() => {
    const params = PluginManager.parameters('Evil95_CantGetVehicleOffSound');
    const SE = String(params['SE']);
    const Vol = Number(params['Volume']);
    const Pit = Number(params['Pitch']);

    const _Game_Vehicle_isLandOk = Game_Vehicle.prototype.isLandOk;
    Game_Vehicle.prototype.isLandOk = function(x, y, d) {
        const result = _Game_Vehicle_isLandOk.call(this, x, y, d);
        if (!result) {
            AudioManager.playSe({name: SE, pan: 0, pitch: Pit, volume: Vol});
        }
        return result;
    };
})();
Save it as Evil95_CantGetVehicleOffSound.js.

Many Thanks go to Arthran for optimizing my code.
Just a couple of tips:

1 - If the same plugin works in MV and MZ, you really don't need two separate release threads. You can just make one thread for the plugin and tell people it works in both. Then you don't get separate discussions of any potential errors or changes.

2 - The only difference between the two is for some reason here you're declaring variables with const and in the MV one you're declaring them with var. const works in MV.

3 - Without the @target MZ parameter, this plugin generates a warning in the plugin manager for anyone who installs it.
Thank you for the reply.

1. I don't own the MZ so Google magic was the only way to check if this is MZ compatible. 😅

2. I didn't know that. Thanks for this information. I just startet with the MV and even with JS. So this is basicly my first little script. 😀

3. I upload a update where i add and change all your suggestions. Many thanks.
Hi. I've got a few tips for you. Not to pick on you, but just to try to give you some advice that will come in handy if you intend to continue with plugin development.

You have declared several global variables, with very simple names. The problem with this is that if another plugin tries to create a global variable with the same name, there will be a conflict. In order for plugins to play nicely with each other, it's important to always try to be a good neighbor and avoid polluting the global namespace more than what is necessary. There are two commonly used ways to accomplish that:
  1. Wrap your code in an IIFE. For this particular plugin, that's what I would probably do.
  2. Declare a global object specific to the plugin, and attach all of your variables to that object. For larger plugins, I prefer this option, because it makes it easier for other authors to create compatibility patches and extensions for your plugin.
The other issue is that you have overriden the function, instead of monkey patching it. Monkey patching (often called "aliasing" in RPG Maker communities) is a technique that allows you to add additional code to a function, without overriding the original code.

If two plugins override the same function, there will definitely be a conflict. If one plugin overrides the function, and the other plugin monkey patches it, then there will be a conflict unless the latter plugin is placed below the former plugin in the plugin manager. If both plugins monkey patch the function, then there will (generally) be no conflict, regardless of the plugin order.

Sometimes, you have no choice but to override a function. But when it's possible, you should always try to monkey patch instead. Not only will that increase the chances that your plugin will play nicely with other plugins, but it will also help ensure that your plugin remains functional after engine updates. In this particular case, monkey patching is an option, and it would actually shorten/simplify your code as well.

Here is an example of your plugin edited to include these changes. If you have a question about anything in particular, please don't hesitate to let me know:
Spoiler: Code After Changes
JavaScript:
//==============================================================================
// Cant get off Vehicle Sound
// -----------------------------------------------------------------------------
// Version 1.2 (19.06.2023)
// Von Evil95
//==============================================================================
/*:de
 * @plugindesc v1.2 Lässt ein Sound ertönen, sofern man nicht mit einem Fahrzeug laden oder austeigen kann.
 * @author Evil95
 * @target MZ
 *
 * @param SE
 * @type file
 * @dir audio/se/
 * @desc Wähle ein SE aus.
 * @default Buzzer1
 *
 * @param Volume
 * @type number
 * @desc Lautstärke.
 * Wert zwischen 0 und 100 eingeben.
 * @min 0
 * @max 100
 * @default 100
 *
 * @param Pitch
 * @type number
 * @desc Tonhöhe.
 * Wert zwischen 50 und 150 eingeben.
 * @min 50
 * @max 150
 * @default 100
 *
*/
/*:en
 * @plugindesc v1.2 Makes a sound if you cannot exit a vehicle.
 * @author Evil95
 *
 * @param SE
 * @type file
 * @dir audio/se/
 * @desc Select SE audio file.
 * @default Buzzer1
 *
 * @param Volume
 * @type number
 * @desc Enter value between 0 and 100.
 * @min 0
 * @max 100
 * @default 100
 *
 * @param Pitch
 * @type number
 * @desc Enter value between 50 and 150.
 * @min 50
 * @max 150
 * @default 100
 *
*/

var Imported = Imported || {};
Imported.Evil95_CantGetVehicleOffSound = true;

(() => {
    const params = PluginManager.parameters('Evil95_CantGetVehicleOffSound');
    const SE = String(params['SE']);
    const Vol = Number(params['Volume']);
    const Pit = Number(params['Pitch']);

    const _Game_Vehicle_isLandOk = Game_Vehicle.prototype.isLandOk;
    Game_Vehicle.prototype.isLandOk = function(x, y, d) {
        const result = _Game_Vehicle_isLandOk.call(this, x, y, d);
        if (!result) {
            AudioManager.playSe({name: SE, pan: 0, pitch: Pit, volume: Vol});
        }
        return result;
    };
})();

While it wasn't necessarily an issue, I went ahead and changed some of your variables to const, where it was appropriate to do so. I also corrected the Pitch parameter, because in RPG Maker, pitch can range from 50 to 150.

Original thread: https://forums.rpgmakerweb.com/threads/cant-get-off-vehicle-sound-mz.158458/