My convoluted way of getting an actor without being an actor component

He says it is difficult to get certain components from something that doesn’t have a direct relation. I used the grabber mechanics for an action so an other opens. Though I don’t use a pressure plate or something like that for that door but just the key.

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


#include "OpenJailDoor.h"

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

	PitchOGValue = GetOwner()->GetActorRotation().Pitch;
	YawOGValue = GetOwner()->GetActorRotation().Yaw;
	RollOGValue = GetOwner()->GetActorRotation().Roll;

	PitchNEWValue = PitchOGValue;
	YawNEWValue = YawOGValue - YawTarget;
	RollNEWValue = RollOGValue;
	
	ActorThatOpens = GetWorld()->GetFirstPlayerController()->GetPawn();

	FindPhysicsHandle();
	SetupInputComponent();
	// ...
	
}

void UOpenJailDoor::FindPhysicsHandle()
{
	PhysicsHandle = GetWorld()->GetFirstPlayerController()->GetPawn()->FindComponentByClass<UPhysicsHandleComponent>();
	if (!PhysicsHandle)
	{
		UE_LOG(LogTemp, Error, TEXT("No PhysicsHandleComponent Found On: %s"), *GetWorld()->GetFirstPlayerController()->GetPawn()->GetName());
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("PhysicsHandleComponent Found On: %s"), *GetWorld()->GetFirstPlayerController()->GetPawn()->GetName());

	}
}

void UOpenJailDoor::SetupInputComponent()
{
	InputComponent = GetWorld()->GetFirstPlayerController()->GetPawn()->FindComponentByClass<UInputComponent>();
	if (!InputComponent)
	{
		UE_LOG(LogTemp, Error, TEXT("No InputComponent Found On: %s"), *GetWorld()->GetFirstPlayerController()->GetPawn()->GetName());
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("InputComponent Found On: %s"), *GetWorld()->GetFirstPlayerController()->GetPawn()->GetName());
		InputComponent->BindAction("Grab", IE_Pressed, this, &UOpenJailDoor::Grab);
	}
}

void UOpenJailDoor::Grab()
{

	FHitResult HitResult = GetFirstPhysicsBodyInReach();
	AActor* ActorHit = HitResult.GetActor();
	
	UE_LOG(LogTemp, Warning, TEXT("Grabber Pressed"));

	if (ActorHit)
	{
		OpenGate = true;
	}
	else
	{
		OpenGate = false;
	}
	
	// ...
	
}

void UOpenJailDoor::GrabReleased()
{
	if (!PhysicsHandle)
	{
		return;
	}
	else
	{
		OpenGate = false;
		UE_LOG(LogTemp, Warning, TEXT("Grabber Released"));
	}

	
	// ...
}

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

	if (OpenGate == true)
	{
		OpenDoor(DeltaTime);
		DoorLastOpened = GetWorld()->GetTimeSeconds();
	}
	else
	{
		if (GetWorld()->GetTimeSeconds() - DoorLastOpened > DoorCloseDelay)
		{
			CloseDoor(DeltaTime);
		}
		else
		{
			
		}
	}
	
	// ...
}

void UOpenJailDoor::OpenDoor(float DeltaTime)
{
	FHitResult HitResult = GetFirstPhysicsBodyInReach();
	AActor* ActorHit = HitResult.GetActor();
	
	UE_LOG(LogTemp, Warning, TEXT("Open Door"));

	if (ActorHit)
	{
		OpenGate = false;
	}
	else
	{
		float const CurrentYaw = GetOwner()->GetActorRotation().Yaw;

		FRotator OpenDoor(0.0f, YawNEWValue, 0.0f);
		OpenDoor.Yaw = FMath::FInterpTo(CurrentYaw, YawNEWValue, DeltaTime, DoorOpenSpeed * DeltaTime);
		GetOwner()->SetActorRotation(OpenDoor);
	}
	// ...
}

void UOpenJailDoor::CloseDoor(float DeltaTime) const
{
	float const CurrentYaw = GetOwner()->GetActorRotation().Yaw;

	FRotator OpenDoor(0.0f, YawOGValue, 0.0f);
	OpenDoor.Yaw = FMath::FInterpTo(CurrentYaw, YawOGValue, DeltaTime, DoorCloseSpeed * DeltaTime);
	GetOwner()->SetActorRotation(OpenDoor);
	
	// ...
}

FHitResult UOpenJailDoor::GetFirstPhysicsBodyInReach() const
{	
	
	FHitResult Hit;
	FCollisionQueryParams TraceParams(FName(TEXT("")), false, GetOwner());
	// Ray-cast
	GetWorld()->LineTraceSingleByObjectType
    (
        OUT Hit,
        GetPlayersPosition(),
        GetPlayersReach(),
        FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
        TraceParams
    );

	AActor* ActorHit = Hit.GetActor();

	if (ActorHit)
	{
		UE_LOG(LogTemp, Error, TEXT("Linetrace has hit: %s"), *(ActorHit->GetName()));
	}

	return Hit;
}

FVector UOpenJailDoor::GetPlayersReach() const
{
	FVector PlayerViewPointLocation;
	FRotator PlayerViewPointRotation;
	
	GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(OUT PlayerViewPointLocation, OUT PlayerViewPointRotation);

	return PlayerViewPointLocation + PlayerViewPointRotation.Vector() * Reach;
}

FVector UOpenJailDoor::GetPlayersPosition() const
{
	FVector PlayerViewPointLocation;
	FRotator PlayerViewPointRotation;
	
	GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(OUT PlayerViewPointLocation, OUT PlayerViewPointRotation);

	return PlayerViewPointLocation;
}




And yes I know the tickcomponent is expensive, but didn’t wanted to invest to many time. Already put too much time in things:

Privacy & Terms