MZ Death Sounds Plugin

Hey there,

I was trying to create a plugin that plays a sound everytime a character dies but I just can't get the sound to actually play. Maybe someone can figure out what went wrong.

Here is the code:

JavaScript:
(() => {
    const pluginName = 'DeathSoundsPlugin';

    // Plugin Commands
    PluginManager.registerCommand(pluginName, 'PlayDeathSound', args => {
        const characterId = Number(args.characterId);
        const soundFile = getDeathSound(characterId);

        if (soundFile) {
            AudioManager.playSe({ name: soundFile, volume: 90, pitch: 100, pan: 0 });
        }
    });

    PluginManager.registerCommand(pluginName, 'SetDeathSound', args => {
        const characterId = Number(args.characterId);
        const soundFile = args.soundFile;
        setDeathSound(characterId, soundFile);
    });

    // Battle Manager
    BattleManager.playDeathSound = characterId => {
        PluginManager.callCommand('DeathSoundsPlugin', 'PlayDeathSound', { characterId: characterId });
    };

    // Game Actor
    const _Game_Actor_performCollapse = Game_Actor.prototype.performCollapse;
    Game_Actor.prototype.performCollapse = function () {
        _Game_Actor_performCollapse.call(this);
        BattleManager.playDeathSound(this.actorId());
    };

    // Helper Functions
    const getDeathSound = characterId => {
        const actor = $gameActors.actor(characterId);
        if (actor && actor.meta) {
            return actor.meta.deathSound || '';
        }
        return '';
    };

    const setDeathSound = (characterId, soundFile) => {
        const actor = $gameActors.actor(characterId);
        if (actor && actor.meta) {
            actor.meta.deathSound = soundFile;
        }
    };
})();
Moved to: Plugin Support
Hi there!

Why did you call a plugin command inside the code?
Plugin commands are supposed to be called by the user, using events.
Do you want to trigger it manually?
Hi there!

Why did you call a plugin command inside the code?
Plugin commands are supposed to be called by the user, using events.
Do you want to trigger it manually?
No, I want to set up different sounds depending on the character with the setDeathSound command and then it's supposed to automatically play the sounds everytime a certain character dies/collapses in a battle.

But I also tried to trigger the sound manually with the PlayDeathSound command but that didn't work either.
No, I want to set up different sounds depending on the character with the setDeathSound command and then it's supposed to automatically play the sounds everytime a certain character dies/collapses in a battle.

But I also tried to trigger the sound manually with the PlayDeathSound command but that didn't work either.
So why don't you call the function directly instead of using plugin commands?
You don't need any plugin commands.

You also don't need to set a sound file with the function.
You can just set the sound file on the actor note tags, since you are using the meta property.
Then just get the meta sound value to play the sound.

Are you using any A.I to make this plugin?
So, there's the part that @Eliaquim said - the procedure of your code is backwards. You should have a low-level function that plays the sound and your plugin command calls that function with the arguments given by the player. You shouldn't be calling plugin commands from within the code.

But then there's also a problem with the setDeathSound() function. You can't assign meta values.

I mean, you can, but it's not going to achieve much. Metadata is read from the database, it's not a writable property that saves. So anything you do with setDeathSound() is going to be erased the next time the user plays the game.

You're also accessing it incorrectly. Game_Actors don't have any meta property. So, amusingly, what you're doing with setDeathSound() actually will save, because you're creating a new property on the actor, but it won't read any notetags you've entered in the editor.
That makes sense. That's one of my problems, I always think too complicated even though the solution could be pretty simple. But I'm also pretty new to creating plugins and I still don't know how to use certain functions correctly, I will have to look more into it.

Thank you, I got it to work now.
This thread is being closed due to being solved. If you'd like it reopened, report it to let us know why. Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/death-sounds-plugin.158284/