MZ Help with Window SE Plugin in MZ

Hi, I am trying to make a plugin for practice that plays a sound effect (SE) whenever the dialogue box is advanced (player presses confirm/cancel button), but the code I have so far doesn't play the SE.

Can someone explain what I'm doing wrong?

The plugin script:
Spoiler
// PLUGIN COMMANDS
/*:
@target MZ
@plugindesc Plays a Sound Effect when the text box window is advanced or closed.

@param seFile
@text SE File
@desc Sound Effect that will be played when the text box window is advanced or closed.
@require 1
@type file
@dir audio/se
@Default Cursor1
*/

// PLUGIN CODE
(() => {
"use strict";
const pluginName = "TextAdvanceSE"

const parameters = PluginManager.parameters(pluginName);

const seFile = (parameters);
const advanceMessage = Window_Message.prototype.isTriggered;
Window_Message.prototype.isTriggered = function(){
AudioManager.playSe(seFile);
return advanceMessage.call(this);
};
})();

Apologize for the novice question and thanks in advance 🙂
Hi there!
The parameter of the "playSe" must be an object:

JavaScript:
{name: "filename", pan: number, pitch:number, volume: number}
It works now, thanks so much! I just have to further tweak the plugin behaviors next 😀

(The working code, if anyone's interested 🙂)

Spoiler
// PLUGIN COMMANDS
/*:
@target MZ
@plugindesc Plays a Sound Effect when the text box window is advanced or closed.

@param name
@text SE File
@desc Sound Effect that will be played when the text box window is advanced or closed.
@require 1
@type file
@dir audio/se
@Default Cursor1

@param volume
@type number
@Default 90

@param pitch
@type number
@Default 100

@param pan
@type number
@Default 0
*/

// PLUGIN CODE
(() => {
"use strict";

const pluginName = "TextAdvanceSE"

const parameters = PluginManager.parameters(pluginName);
const name = (parameters["name"] || "Cursor1");
const volume = Number(parameters["volume"] || 90);
const pitch = Number(parameters["pitch"] || 100);
const pan = Number(parameters["pan"] || 0);

const advanceMessage = Window_Message.prototype.terminateMessage;
Window_Message.prototype.terminateMessage = function(){
let audio = {};
audio.name = name;
audio.volume = volume;
audio.pitch = pitch;
audio.pan = pan;
AudioManager.playSe(audio);
return advanceMessage.call(this);
};
})();
I have a further question: is there a way to change a plugin command argument from inside the plugin itself?

I'm trying to make the "disableThis" argument change back to false automatically after use, but it's not working. How to fix this?
Spoiler
@command disable
@text Disable
@desc Turn the SE off for the next message window.

@arg disableThis
@desc Use if overlapping with other SE or if \^ is utilized.
@type boolean
@Default false

//Plugin commands.
PluginManager.registerCommand(pluginName, "disable", function(args) {
if (eval(args.disableThis)) {
args.disableThis = false;
audio.volume = 0;
} else {
audio.volume = setVolume;
};
});

The full code for context:
Spoiler
// PLUGIN COMMANDS
/*:
@target MZ
@plugindesc Plays a Sound Effect when the message window is advanced or closed.
@Help Set the "Disable" command to turn off the plugin.
@Author Vivisecta

@param name
@text SE File
@desc Sound Effect that will be played.
@require 1
@type file
@dir audio/se
@Default Cursor1

@param volume
@text Volume
@type number
@Default 90

@param pitch
@text Pitch
@type number
@Default 100

@param pan
@text Pan
@type number
@Default 0

@command disable
@text Disable
@desc Turn the SE off for the next message window.

@arg disableThis
@desc Use if overlapping with other SE or if \^ is utilized.
@type boolean
@Default false
*/

// PLUGIN CODE
(() => {
//Disables usage of undeclared variables.
"use strict";

//Plugin name.
const pluginName = "MsgWindowAdvanceSE";

//Plugin parameters.
const parameters = PluginManager.parameters(pluginName);
const name = (parameters["name"] || "Cursor1");
const volume = Number(parameters["volume"] || 90);
const pitch = Number(parameters["pitch"] || 100);
const pan = Number(parameters["pan"] || 0);

//Plugin commands.
PluginManager.registerCommand(pluginName, "disable", function(args) {
if (eval(args.disableThis)) {
args.disableThis = false;
audio.volume = 0;
} else {
audio.volume = setVolume;
};
});

//SE object.
let audio = {
name : name,
volume : volume,
pitch : pitch,
pan : pan
};

//Save set volume.
const setVolume = audio.volume;

//Play SE when message box is closed.
const messageAdvance = Window_Message.prototype.updateInput;
Window_Message.prototype.updateInput = function() {
if (this.isAnySubWindowActive()) {
return true;
};
if (this.pause) {
if (this.isTriggered()) {
Input.update();
this.pause = false;
if (!this._textState) {
AudioManager.playSe(audio);
this.terminateMessage();
};
};
return true;
};
return false;
};
//END OF PLUGIN CODE
})();
Hi there!

First, try to use to paste your code as a code in the post, it helps reading ^^

I believe the problem is that you are not setting a value to the "disableThis" when you are passing it on the "eval".

I think you can try that:

JavaScript:
//Plugin commands.
PluginManager.registerCommand(pluginName, "disable", function(args) {
args.disableThis = eval(args.disableThis)
if (args.disableThis) {
    args.disableThis = false;
    audio.volume = 0;
} else {
    audio.volume = setVolume;
};
});

You can also try to avoid using eval, and do something simple.
Since you are using a boolean argument, the "disableThis" can be either a string "true" or "false"
JavaScript:
args.disableThis = args.disableThis === "true"
Oh, sorry! I just saw the insert code button, I'll be sure to use it next time!
I read up on eval, it seems to generally be avoided, I'll keep it in mind 🙂

I tried using the second script, but it still won't automatically turn on at the next message box.
Did I place it wrong? Also, does it have to be "true" and "false" or can true and false work?

Code:
    //Plugin commands.
    PluginManager.registerCommand(pluginName, "disable", function(args) {
        if (args.disableThis = args.disableThis === "true") {
            args.disableThis = "false";
            audio.volume = 0;
        } else {
            audio.volume = setVolume;
        };
    });
Hi!
The thing is, by default, all arguments inserted in a plugin command are strings.
So, args.disableThis will be a string by default.

Since It is a boolean value,
@arg disableThis
@desc Use if overlapping with other SE or if \^ is utilized.
@type boolean // HERE
@Default false

it can only be one of the two strings: "true" or "false".
So, you need to parse/format/evaluate this string in order to do what you want.
Instead of doing this:
if (args.disableThis = args.disableThis === "true") {

Do this instead:
JavaScript:
args.disableThis = args.disableThis === "true"
if(args.disableThis){ ...

The first line above is checking if the args.disableThis is equal to the string "true".
If it is equal to the string "true" it will return a boolean value true. Otherwise, it will return the boolean value false.

So, if you want to manually set a value for the args.disableThis, you can:
JavaScript:
args.disableThis = false
args.disableThis = true

but it still won't automatically turn on at the next message box
- I didn't understand this part. What do you want automatically turn on in the next message box?
Ah, thank you for the through explanation!
When I said automatically turn on, I mean I want it so that when you use the "disable" plugin command the next message box won't have any sound effect, but the one after that will. It's so the user doesn't have to manually turn the command back on, as this command is for the rare occasions you don't need the SE for the message box closing.

I got it to work, though! All I had to do was put the audio.volume = setVolume; under this.terminateMessage(); so that every time the message box is closed the SE volume is reset.

Thank you very much again! You really helped me a lot 😀

Original thread: https://forums.rpgmakerweb.com/threads/help-with-window-se-plugin-in-mz.143322/