Sound when Pause Sign shows script

I have figured out how to make it work so that a certain sound will play at the end of each message window's text, i.e. when the pause sign (cursor icon, you might call it) is displayed. That's at the end of the text display, not after user input to close the Message Window. But I need help to make it stop working, selectively.

The following lines of script should be run on each map at least once to make it run...

// Play sound at end of text
var TH_WindowMessage_onEndOfText = Window_Message.prototype.onEndOfText;
Window_Message.prototype.onEndOfText = function() {
TH_WindowMessage_onEndOfText.call(this);
AudioManager.playSe({name: 'NAME', pan: X, pitch: Y, volume: Z});
};

Replace NAME, X, Y, Z. "NAME" is the name of the audio file in the se folder, no extension. Leave the inverted commas.

I've tried updating the playSe function with a zero volume setting, or with a sound file that plays silence, but nothing has worked.

Could a more capable scripter offer a solution? Even a completely different method would be welcome, as long as it works selectively. Thanks in advance.
Just to note, if you set the "volume" to 100 then each time the script runs it gets louder by 100. So make sure to clear it before running it again.

To fix your problem, all you have to do is replace "playSe" with "stopSe" and make sure it still refers to the same settings as the last one. That will disable the sound from playing at the end of the message windows 🙂
Just to note, if you set the "volume" to 100 then each time the script runs it gets louder by 100. So make sure to clear it before running it again.

To fix your problem, all you have to do is replace "playSe" with "stopSe" and make sure it still refers to the same settings as the last one. That will disable the sound from playing at the end of the message windows 🙂

Thanks for the response!

Unfortunately, it does not appear to work... :hsad:
After trying it as you described, I experimented a bit, putting a line of code with "stopSe" in place of "playSe" immediate below the "play" line. Still no go...

Also, I'm not experiencing an exponential increase in volume despite repeat runs of the script, but I've got it set at 40%, so that could be why it isn't noticeable.
The following lines of script should be run on each map at least once to make it run...
I am not sure what you mean by this since if you are aliasing the function as a plugin, it is already loaded when the game loads - there is no need to continuously load it at the beginning of each map. This would imply that you are running it as a script call in an event instead. Are you using this as a plugin or as a script call in an event (parallel trigger, etc)?

Sound effects should only play once. Background sounds and background music would play continuously on a loop.

I currently have no issues aliasing the function as a plugin and the sound effect is played at the end of the text:
JavaScript:
(function() {
    const alias = Window_Message.prototype.onEndOfText;
    Window_Message.prototype.onEndOfText = function() {
        alias.call(this);
        AudioManager.playSe({
            name: 'Absorb1',
            volume: 50,
            pitch: 100
        });
    }
})();
I am not sure what you mean by this since if you are aliasing the function as a plugin, it is already loaded when the game loads - there is no need to continuously load it at the beginning of each map. This would imply that you are running it as a script call in an event instead. Are you using this as a plugin or as a script call in an event (parallel trigger, etc)?

Sound effects should only play once. Background sounds and background music would play continuously on a loop.

I currently have no issues aliasing the function as a plugin and the sound effect is played at the end of the text:
JavaScript:
(function() {
    const alias = Window_Message.prototype.onEndOfText;
    Window_Message.prototype.onEndOfText = function() {
        alias.call(this);
        AudioManager.playSe({
            name: 'Absorb1',
            volume: 50,
            pitch: 100
        });
    }
})();
That works beautifully. Much more elegant than my solution. Thank you!

However, I still need to be able to enable and disable it at run time. This is why I was running it as a script instead of a plugin; in an attempt to have that much control over it.
That works beautifully. Much more elegant than my solution. Thank you!

However, I still need to be able to enable and disable it at run time. This is why I was running it as a script instead of a plugin; in an attempt to have that much control over it.
Not a problem, you can tie it to a game switch (for the example, I'll use game switch 1) so you can turn it on or off whenever you need. You'll just have to use the control switch event command before running the text:

JavaScript:
(function() {
    const alias = Window_Message.prototype.onEndOfText;
    Window_Message.prototype.onEndOfText = function() {
        alias.call(this);
        if ($gameSwitches.value(1)) {
            AudioManager.playSe({
                name: 'Absorb1',
                volume: 50,
                pitch: 100
            });
        }
    }
})();
Not a problem, you can tie it to a game switch (for the example, I'll use game switch 1) so you can turn it on or off whenever you need. You'll just have to use the control switch event command before running the text:

JavaScript:
(function() {
    const alias = Window_Message.prototype.onEndOfText;
    Window_Message.prototype.onEndOfText = function() {
        alias.call(this);
        if ($gameSwitches.value(1)) {
            AudioManager.playSe({
                name: 'Absorb1',
                volume: 50,
                pitch: 100
            });
        }
    }
})();
Thank you very much for this! I'm learning from your code here, too. Very much appreciated.

I will put you in the credits under plugins! 😀
Thank you very much for this! I'm learning from your code here, too. Very much appreciated.

I will put you in the credits under plugins! 😀
Thank you - no credit necessary. Much appreciated - just pay it forward or credit this forum because there is a lot of great help/code/plugins from this community.

Also, the script you were using would work fine as a plugin as well. The difference is that mine is an anonymous function that is invoked immediately (IIFE - Immediately Invoked Function Expression).


You could do this with what you wrote and it should work (adding the game switch):
JavaScript:
// Play sound at end of text
var TH_WindowMessage_onEndOfText = Window_Message.prototype.onEndOfText;
Window_Message.prototype.onEndOfText = function() {
    TH_WindowMessage_onEndOfText.call(this);
    if ($gameSwitches.value(1)) AudioManager.playSe({name: 'NAME', pan: X, pitch: Y, volume: Z});
};
(changing the values 'NAME', X, Y, and Z, of course)
JavaScript:
var TH_WindowMessage_onEndOfText = Window_Message.prototype.onEndOfText;
Window_Message.prototype.onEndOfText = function() {
    TH_WindowMessage_onEndOfText.call(this);
IIFEs are typically used to limit scope, e.g. to avoid polluting the global namespace.

If you want the original method to be globally-accessible (e.g. so other plugins etc can read/modify it), I'd suggest defining a namespace to keep things organised, e.g.
JavaScript:
var pinebox = {};
pinebox.Window_Message_onEndOfText = Window_Message.prototype.onEndOfText;
Window_Message.prototype.onEndOfText = function() {
  pinebox.Window_Message_onEndOfText.apply(this, arguments);
  // your code here
};
However, declaring the alias locally, as in your first reply, is often sufficient.

Also, just so you know, "TH" is the prefix Tsukihime/Himeworks uses for their local aliases. It's an arbitrary choice to minimise naming conflicts; choose your own prefix (if any) for your own code.
IIFEs are typically used to limit scope, e.g. to avoid polluting the global namespace.

If you want the original method to be globally-accessible (e.g. so other plugins etc can read/modify it), I'd suggest defining a namespace to keep things organised, e.g.
JavaScript:
var pinebox = {};
pinebox.Window_Message_onEndOfText = Window_Message.prototype.onEndOfText;
Window_Message.prototype.onEndOfText = function() {
  pinebox.Window_Message_onEndOfText.apply(this, arguments);
  // your code here
};
However, declaring the alias locally, as in your first reply, is often sufficient.

Also, just so you know, "TH" is the prefix Tsukihime/Himeworks uses for their local aliases. It's an arbitrary choice to minimise naming conflicts; choose your own prefix (if any) for your own code.
Thank you @caethyril !

I couldnt remember off the top of my head which dev used TH for their plugins. I assumed the OP was quoting a specific part of someone else's plugin that had "TH" so I just continued with what they used.

Agreed! I noticed that you use the void operator for IIFEs. In my mind, it makes sense visually as well when I see the operator (also coming from pre ES6, I think?).

Is there a reason specifically for it for later versions? I read that there are concatenation issues with the () at the end without the semicolon so using void can prevent errors but I am not sure of other things.
I assumed the OP was quoting a specific part of someone else's plugin that had "TH" so I just continued with what they used.
Oh, OK, I didn't notice that earlier. No worries! 👋

I noticed that you use the void operator for IIFEs. In my mind, it makes sense visually as well when I see the operator (also coming from pre ES6, I think?).

Is there a reason specifically for it for later versions? I read that there are concatenation issues with the () at the end without the semicolon so using void can prevent errors but I am not sure of other things.
Yep, I've taken to using it for the reasons you described: readability and an ASI hint. The void operator was defined in ES1 (1997), so the specific JS version makes no difference. It's just a personal style choice I adopted at some point~
IIFEs are typically used to limit scope, e.g. to avoid polluting the global namespace.

If you want the original method to be globally-accessible (e.g. so other plugins etc can read/modify it), I'd suggest defining a namespace to keep things organised, e.g.
JavaScript:
var pinebox = {};
pinebox.Window_Message_onEndOfText = Window_Message.prototype.onEndOfText;
Window_Message.prototype.onEndOfText = function() {
  pinebox.Window_Message_onEndOfText.apply(this, arguments);
  // your code here
};
However, declaring the alias locally, as in your first reply, is often sufficient.

Also, just so you know, "TH" is the prefix Tsukihime/Himeworks uses for their local aliases. It's an arbitrary choice to minimise naming conflicts; choose your own prefix (if any) for your own code.
Much appreciated. Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/sound-when-pause-sign-shows-script.171887/