UE5 Crashing on Call Function From Different Components

So I have two components: USceneComponent - SC_MoveToPlayer and USceneComponent - LineTracing. I have an actor in my scene with SC_MoveToPlayer that is supposed to move to the player when the boolean ShouldFire = true, and I intend to activate this by running a line trace from LineTracing on my player class, grabbing the component from the hit actor, and running a function to change the boolean within the LineTracing code.

What I’m struggling with is that whenever I change the boolean to true, my editor instantly hard crashes. Like takes up all of my available system memory, freezes up completely, doesn’t close, and no crash log kind of crash. I’ve narrowed the culprit code down to BulletMover->ShouldFire(true) in LineTracing.cpp

Any and all help would be appreciated. I’ve been scouring forums for the last three days trying to make the basic functionality of my game work.

SC_MoveToPlayer.cpp

#include "SC_MoveToPlayer.h"

// Sets default values for this component's properties
USC_MoveToPlayer::USC_MoveToPlayer()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;

	// ...
}


// Called when the game starts
void USC_MoveToPlayer::BeginPlay()
{
	Super::BeginPlay();

	PlayerPawn = GetWorld()->GetFirstPlayerController()->GetPawn();
}


// Called every frame
void USC_MoveToPlayer::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	while(ShouldFire)
	{
		MoveBullet(DeltaTime);
		UE_LOG(LogTemp, Warning, TEXT("Bullet Moving"));
	}
}

///////////////////////////////////////////////////////////////////////
					//Declaring Functions						
//////////////////////////////////////////////////////////////////////

void USC_MoveToPlayer::MoveBullet(float DeltaTime)
{
	DeltaTime;
	//GetPlayerLocation and Rotation
	FVector PlayerPawnLocation = PlayerPawn->GetActorLocation();
	FRotator PlayerPawnRotation = PlayerPawn->GetActorRotation();
	//MoveToLocation and keep player rotation
	FVector CurrentLocation = GetOwner()->GetActorLocation();
	FVector InterpStepping = FMath::VInterpConstantTo(CurrentLocation, PlayerPawnLocation, DeltaTime, BulletSpeed);
	GetOwner()->SetActorLocation(InterpStepping);
	GetOwner()->SetActorRotation(PlayerPawnRotation);
	//DisappearAtEnd When collision hits player or wall
	if(CurrentLocation == PlayerPawnLocation)
	{
		GetOwner()->Destroy();
	}
}

void USC_MoveToPlayer::SetShouldFire(bool NewShouldFire)
{
	ShouldFire = NewShouldFire;
}

LineTracing.cpp

#include "LineTracing.h"
#include "DrawDebugHelpers.h"

// Sets default values for this component's properties
ULineTracing::ULineTracing()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;

	// ...
}


// Called when the game starts
void ULineTracing::BeginPlay()
{
	Super::BeginPlay();

	UE_LOG(LogTemp, Display, TEXT("Working"));
}


// Called every frame
void ULineTracing::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);


}

///////////////////////////////////////////////////////////////////////
					//Declaring Functions						
//////////////////////////////////////////////////////////////////////

void ULineTracing::FireLineTrace()
{
	FVector LineStart = GetComponentLocation();
	FVector LineEnd = LineStart + GetForwardVector() * MaxFireDistance;

	FHitResult HitResult;
	DrawDebugLine(GetWorld(), LineStart, LineEnd, FColor::Red, false);
	DrawDebugSphere(GetWorld(), HitResult.ImpactPoint, 10, 10, FColor::Purple, false, 10);
	GetWorld()->LineTraceSingleByChannel(
		HitResult,
		LineStart,
		LineEnd,
		ECC_GameTraceChannel2
	);
	AActor* HitActor = HitResult.GetActor();
	
	if(HitActor != nullptr)
	{
		USC_MoveToPlayer* BulletMover = HitActor->FindComponentByClass<USC_MoveToPlayer>();
		if(BulletMover != nullptr && BulletMover->ComponentHasTag(AcceptableTag))
		{
			BulletMover->SetShouldFire(true);
			UE_LOG(LogTemp, Display, TEXT("FIRED %s"), *BulletMover->GetName());
		}
		else
		{
			UE_LOG(LogTemp, Display, TEXT("nuffin"));
		}
		UE_LOG(LogTemp, Display, TEXT("HitResult: %s"), *HitActor->GetActorNameOrLabel());
	}
	
}

I would also like to add that if I make my MoveBullet() function blueprint callable and activate it in the BP_Bullet blueprint itself where SC_MoveToPlayer exists, all works fine. This crashing is only occurring when trying to change bool ShouldMove in my LineTracing.cpp Component on the BP_Player.

Even further testing has shown that it’s not calling the function that is crashing ue5, it’s changing the boolean itself. I created a test function that just prints to the ulog and I called it where I’m trying to call SetShouldMove() above and it works fine.

As I was writing this I tried to make the boolean ShouldFire public and just change it directly in LineTracing.cpp BulletMover->ShouldFire = true; but this also hard crashed my ue5.

I’m not super well versed in UE5 or C++ for that matter, I’ve been learning both for about 2 months now, so I’m stumped as to why changing this variable in a separate class is causing a hard crash.

Final update:

Turns out something about my While loop in SC_MoveToPLayer.cpp was scuffed and causing the crash. I changed it out for an If statement and everything worked as intended. I supposed that my first “missing semicolon” type issue was bound to happen at some point.

Here’s the working code for anyone interested:

// Fill out your copyright notice in the Description page of Project Settings.


#include "SC_MoveToPlayer.h"

// Sets default values for this component's properties
USC_MoveToPlayer::USC_MoveToPlayer()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;

	// ...
}


// Called when the game starts
void USC_MoveToPlayer::BeginPlay()
{
	Super::BeginPlay();

	PlayerPawn = GetWorld()->GetFirstPlayerController()->GetPawn();
	
}


// Called every frame
void USC_MoveToPlayer::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	

	if(ShouldFire)
	{
		MoveBullet(DeltaTime);
		UE_LOG(LogTemp, Warning, TEXT("Bullet Moving"));
	}
}

///////////////////////////////////////////////////////////////////////
					//Declaring Functions						
//////////////////////////////////////////////////////////////////////

void USC_MoveToPlayer::MoveBullet(float DeltaTime)
{
	DeltaTime;
	//GetPlayerLocation and Rotation
	FVector PlayerPawnLocation = PlayerPawn->GetActorLocation();
	FRotator PlayerPawnRotation = PlayerPawn->GetActorRotation();
	//MoveToLocation and keep player rotation
	FVector CurrentLocation = GetOwner()->GetActorLocation();
	FVector InterpStepping = FMath::VInterpConstantTo(CurrentLocation, PlayerPawnLocation, DeltaTime, BulletSpeed);
	GetOwner()->SetActorLocation(InterpStepping);
	GetOwner()->SetActorRotation(PlayerPawnRotation);
	//DisappearAtEnd When collision hits player or wall
	if(CurrentLocation == PlayerPawnLocation)
	{
		GetOwner()->Destroy();
	}
}

void USC_MoveToPlayer::SetShouldFire(bool NewShouldFire)
{
	ShouldFire = NewShouldFire;
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.