GetOverlappingActorsCrash

So this one is driving my nuts. Basically whenever I or another object overlaps the trigger volume Unreal Crashes, so I am unable to see any errors.If i comment out the line that has Actor->FindComponentByClass()->GetMass();
Then it doesn’t crash.
I’m assuming something must be wrong with my setup?

anyways here is the Open Door Script

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

#include “OpenDoor.h”
#include “GameFramework/WorldSettings.h”
#include “Engine/World.h”
#include “Components/PrimitiveComponent.h”

#define OUT

// Sets default values for this component’s properties
UOpenDoor::UOpenDoor()
{
// 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 UOpenDoor::BeginPlay()
{
Super::BeginPlay();

// Find the owning Actor
Owner = GetOwner();

}

void UOpenDoor::OpenDoor()
{
Owner->SetActorRotation(FRotator(0.0, OpenAngle, 0.0f));

}

void UOpenDoor::CloseDoor()
{
// Set the door rotation
Owner->SetActorRotation(FRotator(0.0, DefaultZRotation, 0.0f));
}

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

//if the ActorThatOpens is in the volume
if (GetTotalMassOfActorsOnPlate() > 30.f) { // TODO make into parameter

	//then open door
	OpenDoor();
	LastDoorOpenTime = GetWorld()->GetTimeSeconds();
}

//check if its time to close the door
if(GetWorld()->GetTimeSeconds() - LastDoorOpenTime > DoorCloseDelay)
	CloseDoor();

}

float UOpenDoor::GetTotalMassOfActorsOnPlate()
{
float TotalMass = 0.f;

// Find all the overlapping actors
TArray<AActor*> OverlappingActors;
PressurePlate->GetOverlappingActors(OUT OverlappingActors);


// Iterate through them add their mass
for (const auto& Actor : OverlappingActors)
{		
	TotalMass += Actor->FindComponentByClass<UPrimitiveComponent>()->GetMass();
	UE_LOG(LogTemp, Warning, TEXT("%s on pressure plate"), TotalMass);
}

return TotalMass;

}

1 Like

TotalMass is a float but you’re assuming it’s a string with %s, try changing that to a %f, or you can change TotalMass in the log to *Actor->GetName();

1 Like

Ahh right you’re a gem I totally missed that

thank you!

Happy to help :grinning:

Privacy & Terms