My DoorOpen and CloseValue and Using UPROPERTY() for DoorCloseDelay

hi
here is my code using GetTimeSeconds() . My values are slightly different i used FInterpret instead of Lerp, . I also used UPROPERTY() for DoorCloseDelay so i can adjust thru ediitor. It is fun too play so i dont have compile everytime.

I also on ScreenMessages and also made custom UE_LOG() . Here are parts of my code and a screen shot showing Screen Message which displays only when door is closed . it also shows which player is in the TriggerVolume .

Here is my partial Header file ;


	float DoorLastOpened{0.0f};

	UPROPERTY(EditAnyWhere)
	float DoorCloseDelay=2.0f;	
};

Here is cpp file ; (including Macros that can be used to print screen messages ;

#define printALT(text, ...) if(GEngine) GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Green, FString::Printf(text, __VA_ARGS__))
#define print(text) if(GEngine) GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::White, text)
#define printT(text, ...) if(GEngine) GEngine->AddOnScreenDebugMessage(-1, DeltaTime, FColor::Orange, FString::Printf(text, __VA_ARGS__))


#include "OpenDoor.h"
#include "EngineGlobals.h"
#include "Runtime/Engine/Classes/Engine/Engine.h"
#include "Engine/World.h"
#include "GameFramework/PlayerController.h"
#include "GameFramework/Actor.h"
#include "Building_Escape.h"


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

	InitialYaw = GetOwner()->GetActorRotation().Yaw;
	CurrentYaw = InitialYaw;
	TargetYaw += InitialYaw;

	if (!PressurePlate)
	{
		UE_LOG(LogBuildng_Escape, Error, TEXT("PressurePlate Pointer Not Initialized: %s"), *GetOwner()->GetName());
	}

	ActorThatOpens=GetWorld()->GetFirstPlayerController()->GetPawn();
}

// Called every frame
void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
	
	if (PressurePlate && PressurePlate->IsOverlappingActor(ActorThatOpens))
	{
		OpenDoor(DeltaTime);
		DoorLastOpened= GetWorld()->GetTimeSeconds();
	}

	else
	{
		if (GetWorld()->GetTimeSeconds() - DoorLastOpened > DoorCloseDelay)
		{
			 CloseDoor(DeltaTime);
		}
	}
	
}

void UOpenDoor::OpenDoor(float DeltaTime)
{
	// Logging
	//UE_LOG(LogBuildng_Escape, Warning, TEXT("Door Yaw is %0.2f"), GetOwner()->GetActorRotation().Yaw);

	FRotator DoorRotation = GetOwner()->GetActorRotation();
	CurrentYaw = FMath::FInterpTo(CurrentYaw, TargetYaw, DeltaTime, 4.0f);
	DoorRotation.Yaw = CurrentYaw;
	GetOwner()->SetActorRotation(DoorRotation);

	// Screen Message Update for Door Yaw & Actor Opens the Door
	if(GEngine) GEngine->AddOnScreenDebugMessage(-1, DeltaTime, FColor::Orange, FString::Printf(TEXT("Current Yaw: %0.2f"), CurrentYaw));
	if (GEngine && ActorThatOpens)
		GEngine->AddOnScreenDebugMessage(-1, DeltaTime, FColor::Green, FString::Printf(TEXT("Actor Opens Door: %s"), *ActorThatOpens->GetName()));
}

void UOpenDoor::CloseDoor(float DeltaTime)
{
	FRotator DoorRotation = GetOwner()->GetActorRotation();
	CurrentYaw = FMath::FInterpTo(CurrentYaw, InitialYaw, DeltaTime, 10.0f);
	DoorRotation.Yaw = CurrentYaw;
	GetOwner()->SetActorRotation(DoorRotation);

}

Here is my custom UE_LOG in Building_Escape.h file; (partial) . You need to declare as extern in the header file and also DEFINE it in Building_Escape.cpp file

DECLARE_LOG_CATEGORY_EXTERN(LogBuildng_Escape, Log, All);

Here is Building_Escape.cpp file (partial);

DEFINE_LOG_CATEGORY(LogBuildng_Escape);

Here is the screen shot showing the Actor (Default Pawn in the Trigger Volume) The screen message disappears when you get out of the Trigger Volume ;

2 Likes

Great job with posting your code and the photos! Keep up the awesome work!

1 Like

Thank you. I really enjoy the course it is both creative, technical and thesupport / community is awesome.
I cant wait to work for a project after getting enough skills and also keep learning. :see_no_evil::see_no_evil:

Privacy & Terms