Timeline in c++

How to use Timeline in C++

This example gives an idea about how to use timelines in C++.

YourClass.h

UCLASS()
class YOURGAME_API AYourClass: public AActor {
 
protected:
    UPROPERTY()
    UTimelineComponent* MyTimeline;
 
    UPROPERTY()
    UCurveFloat* FloatCurve;
 
    UFUNCTION()
    void TimelineCallback(float val);
 
public:
 
    AYourClass();
};

YourClass.cpp

AYourClass::AYourClass()
{
    static ConstructorHelpers::FObjectFinder<UCurveFloat> Curve(TEXT("/Game/Curves/C_MyCurve"));
    check(Curve.Succeeded());
 
    FloatCurve = Curve.Object;
    MyTimeline = CreateDefaultSubobject<UTimelineComponent>(TEXT("MyTimeline"));
}
 
void AYourClass::BeginPlay()
{
    FOnTimelineFloat callback{};
    callback.BindUFunction(this, FName{ TEXT("TimelineCallback") });
 
    // Add the float curve to the timeline and connect it to your timelines's interpolation function
    MyTimeline->AddInterpFloat(FloatCurve, callback, FName{ TEXT("MyTimelineAnimation") });
    MyTimeline->Play();
}
 
void AYourClass::TimelineCallback(float val)
{
    // This function is called for every tick in the timeline.
}


If you need to get the value of another curve in your callback function, get the playback position of the timeline and use it to get the corresponding value on the other curve.

OtherFloatCurve->GetFloatValue(MyTimeline->GetPlaybackPosition());
OtherVectorCurve->GetVectorValue(42.0f);

You may want to add the curves to your existing timeline and bind a function to be called back instead.

Relevant API Documentation

spaceharry (talk), Squareys


Rate this Page:
5.00
(3 votes)

Approved for Versions:4.12,4.17