Hi guys,
I’m having a little trouble with one of my scripts that have gone a little beyond the course specifics and is requiring me to run a check on multiple pointers. Currently, all of my checks work fine for 5 of the 6 pointers. However, the last actor “SilverActor” won’t check and crashes the program anytime I try to run with it missing. I’ve tried multiple solutions and can’t seem to figure out what’s different. Does anybody have any ideas what I might be doing wrong?
#include "GlassDoorPuzzle.h"
#include "Engine/World.h"
#include "GameFramework/PlayerController.h"
#include "Runtime/Engine/Classes/Components/PrimitiveComponent.h"
#include "Runtime/Engine/Classes/GameFramework/Actor.h"
#define OUT
// Sets default values for this component's properties
UGlassDoorPuzzle::UGlassDoorPuzzle()
{
PrimaryComponentTick.bCanEverTick = true;
}
// Called when the game starts
void UGlassDoorPuzzle::BeginPlay()
{
Super::BeginPlay();
Owner = GetOwner();
CheckPointers();
}
// Called every frame
void UGlassDoorPuzzle::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// Poll the Trigger Volume
if (bCheckActorsOnPlate() == true) // TODO Make hardcode into an editable parameter
{
OpenDoor();
LastDoorOpenTime = GetWorld()->GetTimeSeconds();
}
if (GetWorld()->GetTimeSeconds() - LastDoorOpenTime > DoorCloseDelay)
{
CloseDoor();
}
}
void UGlassDoorPuzzle::OpenDoor()
{
//FVector StartPosition = GetOwner()->GetActorLocation();
//FVector NewPosition = StartPosition + FVector(0.0f, -0.1f, 0.0f) * CurrentSpeed;
Owner->SetActorLocation(EndPosition);
}
void UGlassDoorPuzzle::CloseDoor()
{
Owner->SetActorLocation(StartPosition);
}
bool UGlassDoorPuzzle::bCheckActorsOnPlate()
{
// Find all overlapping actors
TArray<AActor*> OverlappingActors;
CheckPointersOnPlate();
CopperPressurePlate->GetOverlappingActors(OUT OverlappingActors);
// Iterate through them adding their masses
for (const auto* Actor : OverlappingActors)
{
//UE_LOG(LogTemp, Warning, TEXT("%s on pressure plate"), *Actor->GetName())
if (SilverActor->IsOverlappingActor(CopperPressurePlate) && CopperActor->IsOverlappingActor(GoldPressurePlate) && GoldActor->IsOverlappingActor(SilverPressurePlate))
{
// Opens door when true is returned
return true;
}
else
{
return false;
}
}
return false;
}
void UGlassDoorPuzzle::CheckPointers() // TODO Update to (!ensure(variable)) checks
{
// Protecting Pointers
if (!CopperPressurePlate) { UE_LOG(LogTemp, Error, TEXT("%s is missing CopperPressurePlate"), *GetOwner()->GetName()); }
if (!GoldPressurePlate) { UE_LOG(LogTemp, Error, TEXT("%s is missing GoldPressurePlate"), *GetOwner()->GetName()); }
if (!SilverPressurePlate) { UE_LOG(LogTemp, Error, TEXT("%s is missing SilverPressurePlate"), *GetOwner()->GetName()); }
if (!CopperActor) { UE_LOG(LogTemp, Error, TEXT("%s is missing CopperActor"), *GetOwner()->GetName()); }
if (!GoldActor) { UE_LOG(LogTemp, Error, TEXT("%s is missing GoldActor"), *GetOwner()->GetName()); }
if (!SilverActor) { UE_LOG(LogTemp, Error, TEXT("%s is missing SilverActor"), *GetOwner()->GetName()); }
}
bool UGlassDoorPuzzle::CheckPointersOnPlate()
{
// Protecting Pointers
if (!CopperPressurePlate) { return false; }
if (!GoldPressurePlate) { return false; }
if (!SilverPressurePlate) { return false; }
if (!CopperActor) { return false; }
if (!GoldActor) { return false; }
if (!SilverActor) { return false; } // TODO Find out why silver actor cannot be protected
return false;
}