How to have an actor play SE when hit during battle

Hi all,

I feel like this one has to have been addressed previously, but I've been searching around and haven't quite found a solution. There's one for MZ that involves a troop page that doesn't resemble the one I'm looking at in MV.

Simple goal -- when my main actor takes any damage during battle, I'd love for him to play an appropriate sound effect, even like a little grunt or "ow!". Ideally a random SE from amongst a few SE's.

Even better would be the ability to choose from a different pool of SE's depending on how much damage he takes. Like a little "owee" from taking 5 damage, to a nice big YAAAAGH on taking big dmg.

I feel like this ought to be doable without plugins but I haven't figured out a solution. I considered having a parallel event running throughout the entire game that keeps checking for damage like every 5 frames or so, but a) I would still need some way to store HP values and compare the new ones, which might work but I can't figure out how to make that work with a parallel event (also that solution would add to overhead).

Ideal I think would be just some way to run a Common Event upon taking a hit. But even there I would need to access the final damage number the engine has tallied from the damage formulas.

Anyone have any ideas? Thank you!

c

EDIT: Looking over the MZ solution again, which is here, I see that this indeed would apply to MV, but I'm not sure I understand how the provided solution works. I think I can probably figure it out, so I'll test it now and report back ...

EDIT 2: Okay so I've tested this out and I cannot get it to work -- but at least now it seems like a problem of my inexperience with Battle Events.

According to this solution by @JohnDoeNews I have two event pages on my test troop:

2024-02-27 01_49_37-Database.png


And then a second page:

2024-02-27 01_49_43-Database.png


The assigning HP values to variables works fine, but all of it takes place at the beginning of each new turn, instead of at the end of the turn where the damage happens.

I've always been horrible at figuring out how to get things to happen at the right spot in turns, so I'm not surprised this is confuzzling me.

Hoping someone who knows their stuff can help -- thanks again!


EDIT 3: Solved it, the problem was in overcomplicating things.

The first page, establishing the initial HP level, works fine, but then the second page should simply happen at the end of each turn. Not sure why the fancy Turn condition was necessary for the other requester's use case but it doesn't seem to apply here.

Of course, it's still not truly ideal because I will need to copy these pages to every single troop, so if anyone can suggest a cleaner, easier solution, kindly point me in the right direction :LZSsmile:
That solution won't trigger at the correct time. And you can't normally run parallel events in battle. So you'll need a plugin.
That solution won't trigger at the correct time. And you can't normally run parallel events in battle. So you'll need a plugin.

After changing to simply end of turn, it does seem to work. Other than this solution bein a pain that I need to apply to every single troop, might there be something I'm missing?
But isn't your sound effect delayed like that? It doesn't happen with the damage/damaged sprite. And if you have more than one character fighting, the timing can be completely out of place. Because it happens at the end of turn.
But isn't your sound effect delayed like that? It doesn't happen with the damage/damaged sprite. And if you have more than one character fighting, the timing can be completely out of place. Because it happens at the end of turn.
Hmm yeah you're right -- I was just testing with one character but with the full party it's a mess.

So in your opinion, running a CE upon taking damage in battle is really something that must be done via a plugin?

Might there not be a way to do this using a state?
I think you'll need a plugin.

For example use Yanfly's plugins to have a passive state on your actor where they play a SE when the (damage) 'value' variable is > 0
As @rpgLord69 says, there's no way to set up troop events that will run only every time the actor takes damage.

You can use Yanfly's Buffs & States Core and Auto Passive States to do this.

Put a passive state on the actor, and give it the following code.
Code:
<Custom Respond Effect>
if (this.isDamage() && value > 0)
{
// Make a list of as many different SE filenames as you want to choose from
    let grunt = ["Harold_pain_1", "ow", "Harold_pain_2", "thwack"];
   let filename = grunt[Math.randomInt(grunt.length)];

// Adjust any of these numbers that you want
    AudioManager.playSe({name: filename, pan: 0, pitch: 100, volume: 100});
}
</Custom Respond Effect>

There are also almost certainly dedicated plugins to do this kind of thing - have you Googled it?
There are also almost certainly dedicated plugins to do this kind of thing - have you Googled it?

Yes Turan, I certainly did Google it to see what extant options there were -- as a matter of principle I don't seek advice on forums until I do due diligence on my own first.

I saw the Auto Passive State solution and couldn't figure out how to make it work for this purpose, as it's not patently obvious to a code newbie like me how to wrangle a broadly powerful plugin like this to do a specific, tricky thing.

I'm grateful for the pointer and the code and will try it out now and report back!

EDIT: You're pretty incredible, @ATT_Turan ... this works great. If you don't mind could I ask a quick follow-up question looking ahead?

In order to make things easier I've moved the actual audio part to a Common Event and am calling it from within the Passive State's notetag, like so:

<Custom Respond Effect> if (this.isDamage() && value > 0) { $gameTemp.reserveCommonEvent(475) } </Custom Respond Effect>

In testing this seems to work fine -- does this seem right to you, and would you foresee any issues with this in the future?

Additionally, if I want to scale different responses depending on how much damage is done, could I continue to use "value" to calculate?

Thanks a bunch tho, wow this solved a biggie. Grateful.
1709107024025.png

I just run a common event that has a pool of random hurt sounds in my AS code:
1709107278854.png

1709107294404.png

Lol ... so does this cause the CE's to run even if the enemy misses though?
In my case, yes, because there is no missing. Shouldn't be a problem to run another check if the attack connected before the voiceline check tho.
In testing this seems to work fine -- does this seem right to you, and would you foresee any issues with this in the future?
I wouldn't have suggested it that way, as I wouldn't think the next window to run a common event would be exactly when the actor took the hit - I'd think there would be a lag between "you see the skill resolve" and "you hear the grunt."

But if it's working acceptably for you in your testing, then that's fine. There's no other kind of issue.

Additionally, if I want to scale different responses depending on how much damage is done, could I continue to use "value" to calculate?
Absolutely. Just use different mathematical operators in regards to the value (== this, or >= this, or whatever).
This thread is being closed due to being solved. If you need it reopened, report it to let us know why. Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/how-to-have-an-actor-play-se-when-hit-during-battle.166210/