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());
}
}