Creating Timelines
You can create and instantiate your own Timeline Component in an Actor class by following the steps below.
Navigate to your C++ Classes folder and click the Add+. From the drop down menu select New C++ Class.
Select Actor class as a Parent Class.
Name created Actor class Timeline Actor.
Navigate to your
TimelineActor.h
file and include followingTimelineComponent
class library.TimelineActor.h
C++#include "Components/TimelineComponent.h"
Implement the following class declaration inside the TimelineActor class defintion:
TimelineActor.h
C++protected: UPROPERTY(EditAnywhere, BlueprintReadWrite) UTimelineComponent* ExampleTimelineComp;
In this code sample, you use the Property Specifier Tags, EditAnywhere, and BlueprintReadWrite.
Navigate to the
TimelineActor.cpp
file, then add the following code to your TimelineActor constructorATimelineActor::ATimelineActor()
TimelineActor.cpp
C++ATimelineActor::ATimelineActor() { // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. PrimaryActorTick.bCanEverTick = true; ExampleTimelineComp = CreateDefaultSubobject<UTimelineComponent>(TEXT("TimelineComponent")); }
Compile your code.
Navigate to the C++ classes folder, right-click on your TimelineActor and create a Blueprint based on your TimelineActor class. Name it Bp_TimelineActor.
Once you have created your TimelineActor Blueprint, you can view the Class Defaults. From the Components tab, you should see your example Timeline Component.
Work-In-Progress Code
TimelineActor.h
#pragma once
#include "Components/TimelineComponent.h"
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "TimelineActor.generated.h"
UCLASS()
class CPPTIMELINE_API ATimelineActor : public AActor
{
GENERATED_BODY()
TimelineActor.cpp
#include "TimelineActor.h"
// Sets default values
ATimelineActor::ATimelineActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
ExampleTimelineComp = CreateDefaultSubobject<UTimelineComponent>(TEXT("TimelineComponent"));
}
Timeline Variables
When you create a Timeline Component in C++ using UProperty Specifiers
, it will become available as a variable in your Components tab. This may be useful for designers who would
like to continue to make iterations to your TimelineComponent via Blueprint Scripting.
Image above shows using the native C++ Timeline Variable to get the Current Play Rate value of the Timeline in Blueprint.
For a complete list of the available Blueprint Timeline nodes and their functionality please see the Timeline nodes page.
Creating a FTimeLineEvent
Timeline Events (FOnTimelineEvent
) are Dynamic Delegates that provide functionality to a Timeline Component to handle an Event.
Follow the steps below to create your own FTimeLineEvent
to be bound to your Timeline Component's finished functionality.
Navigate to
TimelineActor.h
file and declare the following code in the Class Definition:TimelineActor.h
C++protected: //Delegate signature for the function which will handle our Finished event. FOnTimelineEvent TimelineFinishedEvent; UFUNCTION() void TimelineFinishedFunction();
Navigate to
TimelineActor.cpp
and implement the following code:TimelineActor.cpp
C++void ATimelineActor::TimelineFinishedFunction() { UE_LOG(LogTemp, Warning, TEXT("Finished Event Called.")); }
Navigate to
ATimelineActor::BeginPlay()
method, and implement the following code:TimelineActor.cpp
C++// Called when the game starts or when spawned void ATimelineActor::BeginPlay() { Super::BeginPlay(); TimelineFinishedEvent.BindUFunction(this, FName("TimelineFinishedFunction")); ExampleTimelineComp->SetTimelineFinishedFunc(TimelineFinishedEvent); ExampleTimelineComp->PlayFromStart(); }
You have now successfully bound your
TimelineFinished
Event to your customTimelineFinished
function.Compile your code. Open Editor and navigate to the Content Browser. Find your BP_TimelineActor and drag off it into the Level.
Press Play button. You should see the following message in the Output Log window:
Finished Code
TimelineActor.h
#pragma once
#include "Components/TimelineComponent.h"
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "TimelineActor.generated.h"
UCLASS()
class CPPTIMELINE_API ATimelineActor : public AActor
{
GENERATED_BODY()
TimelineActor.cpp
#include "TimelineActor.h"
// Sets default values
ATimelineActor::ATimelineActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
ExampleTimelineComp = CreateDefaultSubobject<UTimelineComponent>(TEXT("TimelineComponent"));
}