Telling how much damage a projectile did in its Death state?

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
User avatar
Ghastly
... in rememberance ...
Posts: 6109
Joined: Fri Jul 06, 2007 2:34 pm

Telling how much damage a projectile did in its Death state?

Post by Ghastly »

Is there a way for a projectile to figure out how much damage it did to what it hit?
User avatar
krokots
Posts: 269
Joined: Tue Jan 19, 2010 5:07 pm

Re: Telling how much damage a projectile did in its Death st

Post by krokots »

Using ZScript.
1) You can use event "WorldThingDamaged" (wiki). This thing is called every time anything got hit and damaged. Inside, you can just check the name of "inflictor" if you want to check what exactly hit, and "damage" to get the damage number.
2) You can override DamageMobj on actors which you want to check. This work in similar way - just check inflictor for name of projectile, and damage for damage.
If you don't know how to make these work, I'll write a simple example for you.
3) I just thought of another method. Not sure if this works though, never tested it. You might use "DamageFunction" instead of "Damage", and inside, you put a function that generates damage and stores it in a variable.

Code: Select all

Class MyProjectile : Actor
{
	int DamageGen;
	Default
	{
		DamageFunction (SetDamage());
	}
	(...)
	int SetDamage()
	{
		DamageGen = random(1, 8) * 5;
		return DamageGen;
	}
}
In death state you can check the DamageGen variable. Should have generated by then.
User avatar
Ghastly
... in rememberance ...
Posts: 6109
Joined: Fri Jul 06, 2007 2:34 pm

Re: Telling how much damage a projectile did in its Death st

Post by Ghastly »

I believe the third method would work, but wouldn't quite work for what I need; in my case I need the damage that was actually dealt, not the damage that it's supposed to deal. In the case that the projectile's supposed to do 40 damage but the victim only had 23 health, I want to store 23 for example.
User avatar
krokots
Posts: 269
Joined: Tue Jan 19, 2010 5:07 pm

Re: Telling how much damage a projectile did in its Death st

Post by krokots »

Ghastly wrote:I believe the third method would work, but wouldn't quite work for what I need; in my case I need the damage that was actually dealt, not the damage that it's supposed to deal. In the case that the projectile's supposed to do 40 damage but the victim only had 23 health, I want to store 23 for example.
Well then you need to add HITTRACER flag to this. When the projectile hits some actor, it is being assigned to projectile "tracer". So then in this function SetDamage, you can add a check

Code: Select all

if(DamageGen > tracer.Health)
and then based on that get the value you want.

Edit. Only one thing, Im not 100% sure if the "tracer" is being assigned before the DamageFunction is being called. If it's not, then the tracer field will be empty at the time of this check (and that's a crash). But even then, you could just make your own function in death state, that uses DamageMobj on tracer.
User avatar
Ghastly
... in rememberance ...
Posts: 6109
Joined: Fri Jul 06, 2007 2:34 pm

Re: Telling how much damage a projectile did in its Death st

Post by Ghastly »

That also assumes the projectile's put in its Death state in between the tracer pointer being assigned, and before damage is dealt.
krokots wrote:If it's not, then the tracer field will be empty at the time of this check (and that's a crash).
This is why you should always if(*pointer*) before using any pointer, by the way.
User avatar
krokots
Posts: 269
Joined: Tue Jan 19, 2010 5:07 pm

Re: Telling how much damage a projectile did in its Death st

Post by krokots »

Ghastly wrote:That also assumes the projectile's put in its Death state in between the tracer pointer being assigned, and before damage is dealt.
krokots wrote:If it's not, then the tracer field will be empty at the time of this check (and that's a crash).
This is why you should always if(*pointer*) before using any pointer, by the way.
From the source code, the missile first assigns its tracer, next goes to Death state. Thats P_ExplodeMissile. So I think that will work, without using own functions in Death state.
BTW what I meant by that, I mean something like this:

Code: Select all

Class MyProjectile : Actor
{
	int DamageDealt;
	Default
	{
		DamageFunction 0;
		+HITTRACER
	}
	States
	{
		Death:
			TNT1 A 0 GetDamage;
			Stop;
	}
	void GetDamage()
	{
		if(tracer)
		{
			int dam = random(1, 8) * 5;
			DamageDealt = tracer.DamageMobj(target, self, dam, 'Normal');
		}
	}
}
You then get the true damage IIRC, i think when I was using DamNums, the damage shows as damage dealt, so even if something hits for 1000 damage, and monster have 10 health, it showed 10. And DamNums works on WorldThingDamaged and that is almost the same as DamageMobj.

Edit. Meh, turns out that DamageMobj returns just damage, not counting victims health. So you could just check the tracer's health before and after DamageMobj to check that.
User avatar
Ghastly
... in rememberance ...
Posts: 6109
Joined: Fri Jul 06, 2007 2:34 pm

Re: Telling how much damage a projectile did in its Death st

Post by Ghastly »

Ah! Didn't think of making the death state do the damage instead of having the engine do it. I got it working. Thanks!

Return to “Scripting”