I "optimized" OpenDoor.cpp Is this actually good practice?

I had this thought that maybe it would be a good idea to try to execute as little as possible on every tick.
In the code from the lecture we constantly either open the door or close it.
I changed it so we only execute OpenDoor() and CloseDoor() when necessary.
In our game this isn’t really relevant because there is so little going on in the game that CPU performance would not be an issue. But would this be good practice in general or am I unnecessarily overcomplicating things?

Here is what I changed:

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

	if (PressurePlate && TotalMassOfActors() >= MassToOpenDoor)
	{
		if (CurrentYaw != OpenAngle)
		{
			OpenDoor(DeltaTime);
		}
		DoorLastOpened = GetWorld()->GetTimeSeconds();
	}
	
	//if the door has been open longer than DoorCloseDelay
	else if(GetWorld()->GetTimeSeconds() > DoorLastOpened + DoorCloseDelay && CurrentYaw != InitialYaw)
	{
		CloseDoor(DeltaTime);
	}
}

void UOpenDoor::OpenDoor(float DeltaTime)
{
	CurrentYaw = GetOwner()->GetActorRotation().Yaw;
	if(std::abs(CurrentYaw - OpenAngle) < 0.1f)
	{
		CurrentYaw = OpenAngle;
	}
	FRotator DoorRotation(0.f, 0.f, 0.f);
	DoorRotation.Yaw = FMath::FInterpTo(CurrentYaw, OpenAngle, DeltaTime, DoorOpenSpeed);

	GetOwner()->SetActorRotation(DoorRotation);
	
	if(!AudioComponent) {return;}
	if (!DoorIsOpen)
	{
		AudioComponent->Play();
		DoorIsOpen = true;
	}
}

void UOpenDoor::CloseDoor(float DeltaTime)
{
	CurrentYaw = GetOwner()->GetActorRotation().Yaw;
	if(std::abs(CurrentYaw - InitialYaw) < 0.1f)
	{
		CurrentYaw = InitialYaw;
	}
	FRotator DoorRotation(0.f, 0.f, 0.f);
	DoorRotation.Yaw = FMath::FInterpTo(CurrentYaw, InitialYaw, DeltaTime, DoorCloseSpeed);
	GetOwner()->SetActorRotation(DoorRotation);

	if(!AudioComponent) {return;}
	if (DoorIsOpen)
	{
		AudioComponent->Play();
		DoorIsOpen = false;
	}
}

The lines

if (CurrentYaw != OpenAngle)

and

else if(GetWorld()->GetTimeSeconds() > DoorLastOpened + DoorCloseDelay && CurrentYaw != InitialYaw)

make sure we only open the door if it is not currently open and only close it if it is not currently closed.

I do this by checking if the current yaw is the same is the target yaw. However, that will never be the case with FInterpTo because CurrentYaw will infinitely approach TargetYaw but never really reach it.
That is why I added the lines

if(std::abs(CurrentYaw - OpenAngle) < 0.1f)
{
	CurrentYaw = OpenAngle;
}

and

if(std::abs(CurrentYaw - InitialYaw) < 0.1f)
{
	CurrentYaw = InitialYaw;
}

To set the CurrentYaw to the target yaw once it is close enough.

2 Likes

How’s the game functioning?

The game functions the same as in the lecture. The only difference is that CloseDoor() and OpenDoor() only get executed when required whereas in the lecture either one or the other gets executed on every tick.

Privacy & Terms