play sound if enemy is dead or almost

NED

Well-known member
Let me explain:
For my game system, I would like to play particuar bone snap sound effect during some of the custom slams when enemy is almost dead, perhaps something like 0->10 of health.

Is there some way to detect the enemy you're holding to check his/her life level?

Thanks
 
yes its doable:

@script
(if frame==10)
{
void self = getlocalvar("self");
int target = getlocalvar("Target" + self, target);
int THealth = getentityproperty(target,"health");
int SFX1 = loadsample("data/chars/thor/god.wav");
if (THealth<=10)
      {
      playsample(SFX1, 0, openborvariant("effectvol"), openborvariant("effectvol"), 100, 0)
      }
}
@end_script

the frame==10 should be changed to a frame AFTER the @slamstart and BEFORE commands like @THROW @SLAM, etc. Change the SFX1 to the sample you want to play.
 
I suggest making this part of the takedamage event. Then you can use a logic chain to decide what to do. This code sample is from my Golden Axe Hackjob project and executes the following logic:

[list type=decimal]
[*]Is the damage real? No = Exit.
[*]Is the damage lethal?
-Is damage from pit? Yes = Pit KO ("Ahhhhhhhhhhhhhh!!!")
-Is damage not from pit? Yes = Normal KO.
[*]Is the damage for burning? Yes = BURN and exit. At this point you can add checks for any kind of special damage.
[*]HP >= 25% Max. Yes = DYING ("I need healing!", "Help me!", and so on..)
[*]All other checks failed. Normal PAIN (only play 50% of the time so we don't over do it).
[/list]

Note: The soun0005 function is a system that chooses an appropriate random sound to play from selected category (KO, PAIN, DYING, ATTACK, etc.). That part is fairly complex and involves automatically cataloging sound files so I don't need to bother with hard coding file paths or adjusting for different models. Don't worry about that, just sub in your sound player of choice.

Code:
if (!fDam){ return; }														//If damage isn't "real", forget the rest.

	if(fHP > 0)																	//Damage is survivable?
	{
		iAni = getentityproperty(vSelf, "animationid");							//Get animation.
		iPos = getentityproperty(vSelf, "animpos");								//Get current frame.
		iLnd = getentityproperty(vSelf, "landframe", iAni);						//Get landframe.

        if (iPos != iLnd)														//Not landing?
        {
            if (iDrop > 0)                                                      //Knockdown hit?
		    {
				if (iAType == ATK_BURN
                    && iAni != A_BURN)											//Burn hit and not already burning?
			    {
				    soun0005(vSelf, VOIBRN, -1, 1, 0, 0);						//Burn sound.
			    }
			    else
			    {
				    if ((fHP / fMaxHP) <= 0.25 )								//Damage below 25% max health?
				    {
					    soun0005(vSelf, VOIDYN, -1, 1, 0, 0);		            //Dying sound (always).
				    }
				    else
				    {
						soun0005(vSelf, VOIPN, -1, 1, 0, 0);					//Pain sound (always).
				    }
			    }
		    }
		    else
		    {
				soun0005(vSelf, VOIPN, -1, 0.5, 0, 0);							//Pain sound (50%).
		    }
        }
        else
        {
		    if ((fHP / fMaxHP) <= 0.25 )										//Damage below 25% max health?
		    {
			    soun0005(vSelf, VOIDYN, -1, 1, 0, 0);							//Dying sound (always).
		    }
		    else
		    {
			    soun0005(vSelf, VOIPN, -1, 1, 0, 0);							//Pain sound (always).
		    }
	    }
	}
	else
	{

		fX	= getentityproperty(vSelf, "x");									//Get X location.
		fZ	= getentityproperty(vSelf, "z");									//Get Z location.

		if(checkhole(fX, fZ))													//In a pit?
		{
			soun0005(vSelf, VOIPIT, -1, 1, 0, 0);								//Pit KO sound (always).
		}
		else
		{
			soun0005(vSelf, VOIDIE, -1, 1, 0, 0);								//KO sound (always).
		}
	}

Here is the code in action. The recording is a bit out of sync, but pay attention to the sounds. The random battle cries when someone knocks an opponent down, the normal pain when hit, the crying for help when hit at low health (that's the part you are asking about).

http://youtu.be/XWtCEdRZnJI

Give it a shot and good luck!
DC

 
Thanks to both of you. :)

@Ilu
I tried you script and openbor is not loading then no response.

@DC
Impressive use of scripts!
I'm always surprized how you give interactive elements (falling, of falling in water...)
And this slam to the wall!

I'll need these 2:
4 is basically what will give my enemies special damage sounds
5 is the voice that will be played randomly during slams

I have to find a way to simplify the script, because there are a lot of parameters not useful (in my particular project)
 
O Ilusionista said:
yes its doable:

@script
(if frame==10)
{
void self = getlocalvar("self");
int target = getlocalvar("Target" + self, target);
int THealth = getentityproperty(target,"health");
int SFX1 = loadsample("data/chars/thor/god.wav");
if (THealth<=10)
      {
      playsample(SFX1, 0, openborvariant("effectvol"), openborvariant("effectvol"), 100, 0)
      }
}
@end_script

the frame==10 should be changed to a frame AFTER the @slamstart and BEFORE commands like @THROW @SLAM, etc. Change the SFX1 to the sample you want to play.

I given a retry on this code.
Tried in Rachel (player) - Openbor don't loads
Tried on Mex (generic enemy) - Openbor loads but when can't load a level with Mex

My frame 3 is between slamstart and end
There is no typo in sound directory "data/sounds/powhit.wav"
I changed the value of health limit for debug purposes "THealth<=80"
The script is on the line right after animation name

Code:
@script
(if frame==3)
{
void self = getlocalvar("self");
int target = getlocalvar("Target" + self, target);
int THealth = getentityproperty(target,"health");
int SFX1 = loadsample("data/sounds/powhit.wav");
if (THealth<=80)
       {
       playsample(SFX1, 0, openborvariant("effectvol"), openborvariant("effectvol"), 100, 0)
       }
}
@end_script

Is there something wrong or that can prevent this script to work?
Did I forgot something?  :-\
 
Ned, it was my fault. If you make it a takedamagescript, it will work way better. Beacuse I was making the ATTACKER to play the sound, where is way easier to make the DAMAGE TAKER to play it (and sounds more logic). How dumb I am sometimes, lol

Anyway, save this as "damagesound.c" and declare as
takedamagescript data/scripts/damagesound.c

Code:
void main()
{
	void self = getlocalvar("self"); //Get calling entity.
        void Health = getentityproperty(self,"health");// Get health;
	int SFX1 = loadsample("data/chars/thor/god.wav");
	if (Health<=10)
              {
              playsample(SFX1, 0, openborvariant("effectvol"), openborvariant("effectvol"), 100, 0)
              }
}

It should work. If not, change the playsample line to:
playsample(SFX1, 0, 120, 120, 100, 0)
 
Again, thanks for your support Ilu

I tried these 2 ways of calling for the SND, but it still don't loads.

Also, I'm not sure this kind of script style (void main) would be the way to call for a sound at a particular frame of a particular move done by enemy.

If only it could have something as simple as:
Code:
type = playsnd
triggerall = life = [1,250]
On enemy get hit anim...

Perhaps I'll just use a random script to play a sound randomly, so it will simulate the fact sound happen not always...
 
tried these 2 ways of calling for the SND, but it still don't loads.
Give me more info:
A) the code makes OpenBOR crash
b) or the sound just doesn't plays?

Also, I'm not sure this kind of script style (void main) would be the way to call for a sound at a particular frame of a particular move done by enemy.

The code will run right when the enemy get hit, like it was run in frame 0. If you need a specific frame, its doable. Even if you need to be executed in a specific animation with a specific frame, its also doable. The "void main()" means that the function will run right when this code is called (explaining in a very noob way, a better coder can explain you better).

If only it could have something as simple as:

But it is, trust me. The magic relies in you start to understand the script and not simply using it. I suggest it to everyone its how I learned.

 
Back
Top Bottom