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

**URL:** <https://forums.unrealengine.com/t/c-casting-to-line-trace-function-is-causing-the-engine-to-crash/479549>\
**Category:** C++\
**Tags:** tree, blackboard, unreal-engine, CPP, question\
**Created:** [February 28, 2021, 2:39am UTC](https://forums.unrealengine.com/t/c-casting-to-line-trace-function-is-causing-the-engine-to-crash/479549 "2021-02-28T02:39:35Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Quaked54](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Quaked54](https://forums.unrealengine.com/u/Quaked54)\
**Post date:** [February 28, 2021, 2:39am UTC](https://forums.unrealengine.com/t/c-casting-to-line-trace-function-is-causing-the-engine-to-crash/479549/1 "2021-02-28T02:39:35Z")

</div>

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.

 ![331086-shootplayerbttask.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/d/0/8d053484259ce92df5b22f2fba07169a0f90d55d.png)

---

<div class="post-metadata">

**Author:** ![chrudimer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chrudimer/32/918651_2.png) [@chrudimer](https://forums.unrealengine.com/u/chrudimer)\
**Post date:** [February 28, 2021, 1:49pm UTC](https://forums.unrealengine.com/t/c-casting-to-line-trace-function-is-causing-the-engine-to-crash/479549/2 "2021-02-28T13:49:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Quaked54](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Quaked54](https://forums.unrealengine.com/u/Quaked54)\
**Post date:** [February 28, 2021, 5:59pm UTC](https://forums.unrealengine.com/t/c-casting-to-line-trace-function-is-causing-the-engine-to-crash/479549/3 "2021-02-28T17:59:00Z")

</div>

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

}

---

<div class="post-metadata">

**Author:** ![Quaked54](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Quaked54](https://forums.unrealengine.com/u/Quaked54)\
**Post date:** [February 28, 2021, 6:00pm UTC](https://forums.unrealengine.com/t/c-casting-to-line-trace-function-is-causing-the-engine-to-crash/479549/4 "2021-02-28T18:00:22Z")

</div>

I placed a UE\_LOG but the log never fires off.

---

<div class="post-metadata">

**Author:** ![chrudimer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chrudimer/32/918651_2.png) [@chrudimer](https://forums.unrealengine.com/u/chrudimer)\
**Post date:** [February 28, 2021, 6:04pm UTC](https://forums.unrealengine.com/t/c-casting-to-line-trace-function-is-causing-the-engine-to-crash/479549/5 "2021-02-28T18:04:02Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Quaked54](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Quaked54](https://forums.unrealengine.com/u/Quaked54)\
**Post date:** [February 28, 2021, 7:39pm UTC](https://forums.unrealengine.com/t/c-casting-to-line-trace-function-is-causing-the-engine-to-crash/479549/6 "2021-02-28T19:39:00Z")

</div>

I am setting it.

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

---

<div class="post-metadata">

**Author:** ![Quaked54](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Quaked54](https://forums.unrealengine.com/u/Quaked54)\
**Post date:** [February 28, 2021, 8:20pm UTC](https://forums.unrealengine.com/t/c-casting-to-line-trace-function-is-causing-the-engine-to-crash/479549/7 "2021-02-28T20:20:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chrudimer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chrudimer/32/918651_2.png) [@chrudimer](https://forums.unrealengine.com/u/chrudimer)\
**Post date:** [February 28, 2021, 8:21pm UTC](https://forums.unrealengine.com/t/c-casting-to-line-trace-function-is-causing-the-engine-to-crash/479549/8 "2021-02-28T20:21:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Quaked54](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Quaked54](https://forums.unrealengine.com/u/Quaked54)\
**Post date:** [February 28, 2021, 8:28pm UTC](https://forums.unrealengine.com/t/c-casting-to-line-trace-function-is-causing-the-engine-to-crash/479549/9 "2021-02-28T20:28:45Z")

</div>

I set EnemyWeaponBaseMesh as the rootcomponent and it still crashes.

---

<div class="post-metadata">

**Author:** ![Quaked54](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Quaked54](https://forums.unrealengine.com/u/Quaked54)\
**Post date:** [February 28, 2021, 8:53pm UTC](https://forums.unrealengine.com/t/c-casting-to-line-trace-function-is-causing-the-engine-to-crash/479549/10 "2021-02-28T20:53:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Quaked54](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Quaked54](https://forums.unrealengine.com/u/Quaked54)\
**Post date:** [March 5, 2021, 9:17am UTC](https://forums.unrealengine.com/t/c-casting-to-line-trace-function-is-causing-the-engine-to-crash/479549/11 "2021-03-05T09:17:43Z")

</div>

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.
