C++ Casting to Line trace function is causing the engine to crash

The only time when the engine crashes because of the cast is when the enemy fires it’s gun. If the Enemy does not fire the crash does not happen. At line 29 the engine shows that there was a problem attaching the line trace to the socket. I don’t know maybe I am attaching/and or firing the line trace wrong but I doubt it. The crash is where the line says

 const FVector EnemyPistolStartTrace = EnemyWeaponBaseMesh->GetSocketLocation(FName("PistolSocket"));

The Cast

void ACriminalJill::ShootPlayer(AActor* Actor)
{
	SemiGun->ShootEnemy(Actor);
}

The Function

void AEnemyWeaponSemi::ShootEnemy(AActor* Actor)
{		
		FCollisionQueryParams PistolQueryParams = FCollisionQueryParams(SCENE_QUERY_STAT(WeaponTrace), true, this);
		PistolQueryParams.AddIgnoredComponent(EnemyWeaponBaseMesh);
		const FVector EnemyPistolStartTrace = EnemyWeaponBaseMesh->GetSocketLocation(FName("PistolSocket"));
		FCollisionResponseParams ResponseParams;
		FActorSpawnParameters SpawnParams;
		SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
		TArray<FHitResult> PlayerHit;
		FRotator CurrentRotation = EnemyWeaponBaseMesh->GetSocketRotation(FName("PistolSocket"));
		//const FTransform& MuzzleTransform = EnemyWeaponBaseMesh->GetSocketTransform(FName("PistolSocket"));
		const FVector EnemyPistolEndTrace = EnemyPistolStartTrace + CurrentRotation.Vector();
		UGameplayStatics::SpawnEmitterAttached(EnemyWeaponBaseMuzzleFlash, EnemyWeaponBaseMesh, FName("EnemyGlockSocket"));
		if (GetWorld()->LineTraceMultiByChannel(PlayerHit, EnemyPistolStartTrace, EnemyPistolEndTrace,
			ECollisionChannel::ECC_GameTraceChannel1, PistolQueryParams, ResponseParams))
		{
			
			if (PlayerHit.Num() > 0)
			{
				for (FHitResult& Results : PlayerHit)
				{
					if (APlayer* Player = Cast<APlayer>(Actor))
					{
						Player->Health -= Player->HealthDecrease;
					}
				}
			}
		}
	
}

Task
Here I am creating a function in C++ in order to use it for the BTTask. The Extra parameter is for the perception sensing.

Hi, make sure that EnemyWeaponBaseMesh is not null. If it is, then EnemyWeaponBaseMesh->GetSocketLocation will crash.

I tried this but for some reason the linetrace still does not fire.
if(EnemyWeaponBaseMaesh)
{

}

I placed a UE_LOG but the log never fires off.

I tried this but for some reason the
linetrace still does not fire.

Still crashes or does not execute?

If EnemyWeaponBaseMesh is null, then if(EnemyWeaponBaseMesh) will be false and whatever comes inside the if will not execute. So if EnemyWeaponBaseMesh is null (I would check whether or not it is null, if it is then you need to solve that, if it is not, then that is not causing your problem), then the solution is to properly set it, so that it points to something valid.

Sorry I have troubles following you there =)

If you’re already using C++, start UE from visual studio and reproduce the crash. Then inside visual studio look at the line where it crashed (it will be shown there) and at all values of the relevant variables for that line of code.

I am setting it.

AEnemyWeaponSemi::AEnemyWeaponSemi()
{
	PrimaryActorTick.bCanEverTick = true;
	EnemyWeaponBaseMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("WeaponBaseMesh"));
}

wait i set the enemy weapon base mesh as the root component

I set EnemyWeaponBaseMesh as the rootcomponent and it still crashes.

I placed a break point at the if statement and the skeletal mesh returns null and it crashes.

I placed the line trace function inside of the AI it self instead of casting to the firearm and it worked. I just attached the linetrace to the socket of the weapon that I had created a default sub object of and attached to my character within the character. I did not spawn the weapon I just attached the weapon within the class hierarchy then attached the linetrace to the socket of the weapon mesh inside of the character and the mesh did not make the engine crash.