C++ Access Violation casting OnHit OtherActor char to MyChar

Hi all,

I’m working on a function to affect the player characters appearance when they get hit by a projectile actor and I’m running into a bit of trouble: The actual code to cast the OtherActor’s controller’s character is causing a memory access violation. My thinking is maybe I’m confused about which controller the GetInstigatorController is returning? Here’s the OnHit in my character:

.cpp

void ASimpleNetworkGame47Character::OnHit(AActor* SelfActor, AActor* OtherActor, FVector NormalImpulse, const FHitResult& Hit)
{
	// Make sure we actually got hit by something and that something is a projectile and we're not its owner (in case of deflection).
	if((OtherActor != NULL) 
		&& (OtherActor->IsA(ASimpleNetworkGame47Projectile::StaticClass()))
		&& OtherActor->GetOwner() != SelfActor)
	{
	//	// DEBUGGING.
	//	if(GEngine)
	//	{
			//GEngine->AddOnScreenDebugMessage(-1, 4.f, FColor::Red, TEXT("Hit from character OnHit."));
			UE_LOG(LogTemp, Log, TEXT("OtherActor: %s"), *OtherActor->GetName());
	//	}

          // If this part is commented out, no access violation occurs.
		ASimpleNetworkGame47Character* hitChar = Cast<ASimpleNetworkGame47Character>(OtherActor->GetInstigatorController()->GetCharacter());			
		if(hitChar != NULL)
		{
			HurtHealMeshUpdate(-.1f); // Other member func. to change character material.
		}
	}
}

Any ideas?
Cheers,
-Nate

I don’t think Cast is actually crashing as it handles null pointers. I imagine the real probably is “GetInstigatorController()” is returning null and then you’re trying to call “GetCharacter” on a null pointer. Unless you specifically pass in an instigator where ever this damage is first originating from, it looks like the system will just pass in null for the instigator which is something you’ll need to protect against, you can’t assume there will always be an instigator.

Also if you just want the character, I don’t think you need to get the instigator controller, you can simply do the following (which is null safe and handles if there is no instigator controller):

if (ASimpleNetworkGame47Character* hitChar = otherActor->GetInstigator<ASimpleNetworkGame47Character>())
{
   HurtHealMeshUpdate(-.1f);
}

I’d drop in some break points on where you are first applying this damage to verify that the instigator is being set, and then one in this callback to see if you are getting the data back that you expect.

Hi @AdeptStrain,

Indeed you’re correct. I had the same assumptions about GetInstigatorController being null so I set a breakpoint at the Cast and anything beyond OtherActor is null :frowning: Oddly enough, OtherActor is the projectile itself which is even more strange. I might have to rethink this whole thing as there appears to be another underlying issue at hand.

Btw: HurtHealMeshUpdate is purely visual and I was trying to get the visual component working first (the logical portion is easier).

Thanks for the input! :slight_smile:

-Nate

Followup:

I feel a bit embarrassed: Upon further review of my code I realized I was using the correct method… in the victim actor/character. Basically I just commented out the code I was having trouble with and called HurtHealMeshUpdate (because this character is the one needing to listen to the OnHit event) and voala!

Cheers,
-Nate