Rotating door using curves

I wasn’t happy with the way we are doing the linear interpolation (we are not, in fact, interpolating linearly)

So I search for some way to use curves and discovered this UCurveFloat

Basically you create a Curve in the Editor (on your UE’s Content Browser -> Right-click -> Miscellaneous -> Curve -> CurveFloat), then you open it and create your desired curve going from 0 to 1 in x axis (we can extend this through code)’ and from 0 to 90 in y axis.

And don’t forget to drag and drop the curve in the OpenDoor component inside Unreal Editor. Ideally, we want to check if the pointer to the curve isn’t null (otherwise your editor will crash due to the null pointer ref)

OpenDoorComponent

// OpenDoor.h

#include "Curves/CurveFloat.h"
private:
	UPROPERTY(EditInstanceOnly)
	UCurveFloat* YawCurve;		

	UPROPERTY(EditAnywhere)
	// Time used to extend the opening of the door
	float OpeningDuration = 3.5f;

	FRotator InitialRotation;

	bool bIsOpen;
	float ElapsedTime;
// OpenDoor.cpp

void UOpenDoor::BeginPlay()
{
	Super::BeginPlay();

	InitialRotation = GetOwner()->GetActorRotation();

	ElapsedTime = 0;
	bIsOpen = false;
}

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

	if (bIsOpen)
	{
		return;
	}

	if (ElapsedTime > OpeningDuration)
	{
		bIsOpen = true;
		ElapsedTime = 0.f;
		return;
	}

	ElapsedTime += DeltaTime;
	   	
	FRotator CurrentRotation = GetOwner()->GetActorRotation();

	auto CurrentYaw = YawCurve->GetFloatValue(ElapsedTime / OpeningDuration);
	CurrentRotation.Yaw = InitialRotation.Yaw + CurrentYaw;
	GetOwner()->SetActorRelativeRotation(CurrentRotation);
}

EDIT: Never mind. I now realize it’s the UPROPERTY.
So I just learned that the auto keyword will take on the type of whatever value you’re initializing the variable with, what are the cons of just using auto? Could I write like Python and do away with writing int, float, string, etc.?

How did you get the option to input a curve in the OpenDoor details? This is all I see in mine.

Hi @Randyee,

Sorry for the long time to reply!

Regarding the auto keyword, it is just a way for the compiler to infer the type, as you already discovered.

I loved your question about ‘the cons’. For me it boils down to readability; I don’t like to use it everywhere. Sometimes it could improve the readability, while at other times it could have the opposite effect. Check my notes about that video below to get more details and examples.

There is an awesome (and short) video about this topic (and much other C++ stuff on that channel, Cherno is amazing)

Cheers

EDIT:
Just reading the Unreal Coding Standard and found this:

You shouldn’t use auto in C++ code, although a few exceptions are listed below. Always be explicit about the type you’re initializing. This means that the type must be plainly visible to the reader.

It’s very important that types are clearly visible to someone who is reading the code. Even though some IDEs are able to infer the type, doing so relies on the code being in a compilable state. It also won’t assist users of merge/diff tools, or when viewing individual source files in isolation, such as on GitHub.

There are a few exceptions, but it make sense to avoid using it in most situations.

Privacy & Terms