Doors aren't opening even when weight is correct

I’m fairly sure that in my code, this if statement isn’t working for whatever reason:

if (GetTotalMassOfActorsOnPlate() == massOnPlate)
{
	UE_LOG(LogTemp, Warning, TEXT("%f mass"), GetTotalMassOfActorsOnPlate())
	OpenDoor();
	LastDoorOpenTime = GetWorld()->GetTimeSeconds();
}

When I put a statue onto a plate, it doesn’t open the corresponding door. I used to have a UE_LOG after the for loop in GetTotalMassOfActorsOnPlate() that made sure that the mass was exact on each of the pressure plates when I put the statue on it, but the door didn’t open. I have massOnPlate set to editable anywhere, and I have each of my glass statues (which I’m using as keys) set to the exact mass listed. Also, as default, the statues have generate overlap events turned off, which I’ve changed to be on. Also, at some point, I had the if statement set to GetTotalMassOfActorsOnPlate() being less than the massOnPlate, and the doors did open, since the plates had 0 mass on them and that was less than massOnPlate.

Here’s the full code:

// Copyright Aidan Myers 2017.

#include "BuildingEscape.h"
#include "OpenDoor.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();
	Owner = GetOwner(); // Find the owning actor.

}

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

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

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

	// Poll the Trigger Volume
	//If the ActorThatOpens is in the volume
	if (GetTotalMassOfActorsOnPlate() == massOnPlate)
	{
		UE_LOG(LogTemp, Warning, TEXT("%f mass"), GetTotalMassOfActorsOnPlate())
		OpenDoor();
		LastDoorOpenTime = GetWorld()->GetTimeSeconds();
	}
	// Check if it's time to close the door
	if (GetWorld()->GetTimeSeconds() - LastDoorOpenTime > DoorCloseDelay)
	{
		CloseDoor(); 
	}
}

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

	TArray<AActor*> OverlappingActors;
	PressurePlate->GetOverlappingActors(OUT OverlappingActors);

	for (const auto* Actor : OverlappingActors) {
		TotalMass += Actor->FindComponentByClass<UPrimitiveComponent>()->GetMass();
		UE_LOG(LogTemp, Warning, TEXT("%s is on %s"), *Actor->GetName(), *GetOwner()->GetName())
	}


	return TotalMass;
}

Thanks for the help!

Aidan M.

Edit:
I figured it out! It was because I had it as editable anywhere in unreal engine and was setting it to an integer number but a float type (like 1.0, 2.0, etc.). I think that since I had it editable and was changing it in unreal, unreal automatically decided to change it to an integer which messed up the comparison.

Privacy & Terms