FDamageEvent to FPointDamageEvent

I am calling TakeDamage from my bullet with MyDamageEvent being a FPointDamageEvent

FPointDamageEvent MyDamageEvent = FPointDamageEvent(**whatever in the constructor**);
 OtherActor->TakeDamage(ProjectileDamage, MyDamageEvent, this->GetInstigatorController(), this);

but I have no idea how to access that data on the other side, where it comes in as a

 struct FDamageEvent const& DamageEvent

Any help? Thank you.

Can you be more specific about what you want to do with it? You should be able to access any of the information pretty easily. You may have to cast the FDamageEvent to a FPointDamageEvent if you want point damage specific info. I think you could just use:

FPointDamageEvent pointDamageEvent = Cast<FPointDamageEvent>(DamageEvent);

Ah yes, I forgot to mention, I tried to cast it like that but it didn’t work…

The error was:

error C2664: 'T *Cast(const FSubobjectPtr &)' : cannot convert argument 1 from 'const FDamageEvent' to 'const FSubobjectPtr &'

I managed to successfully cast from DamageEvent to PointDamageEvent in code. I used a C-style cast, rather than the UE4 Cast<> function, but it worked out:

const FPointDamageEvent* pointDamageEvent = (const FPointDamageEvent*)&DamageEvent;

That will give you the pointer pointDamageEvent to use, and it worked for me when I tested it.

Yea this is the correct way, but remember to check if this is point/radial dmg event:

if (DamageEvent.IsOfType(FPointDamageEvent::ClassID))
	PointDamageEvent = (FPointDamageEvent*)&DamageEvent;
else if (DamageEvent.IsOfType(FRadialDamageEvent::ClassID))
	RadialDamageEvent = (FRadialDamageEvent*)&DamageEvent;

Thank you very much, that did it.

don’t use that syntax please! it’s a C-style cast which can lead to horrible runtime errors.
use that instead:

static_cast<const FPointDamageEvent*>(&DamageEvent);

ensure that you can cast that though. usually in C++ you check it by dynamic_cast instead (which is similar to unreal’s Cast) but it leads to compiler errors in this specific class, so you can use

if(DamageEvent.IsOfType(1)){

to check it before casting. 1 means point damage. 2 means radial damage etc. check the definitions to know more types.

1 Like

It’s probably better to use

if (DamageEvent.IsOfType(FPointDamageEvent::ClassID))

and

if (DamageEvent.IsOfType(FRadialDamageEvent::ClassID))