Timelines

From Unreal Engine Documents: Timeline nodes are special nodes within Blueprints that allow for simple time-based animation to be quickly designed and played back based on in-game events. Timelines are somewhat like simple Matinee sequences in that they allow simple values to be animated and events to be fired off over time. These can be edited directly inside the Blueprint editor by Double-clicking on the Timeline in the Graph tab or in the My Blueprint tab. They are specifically built for handling simple, non-cinematic tasks such as opening doors, altering lights, or performing other time-centric manipulations to Actors within a scene.


Using Timelines in C++

In this example. We are going to control light intensity and color of a PointLight using the timeline. We start by creating a new C++ class based on Actor. We create Scene component for root and Pointlight component for our light. Also, we need to add header TimelineComponent.h .

#include "TimelineComponent.h"
public:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
USceneComponent* Root;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
UPointLightComponent* Light;

Next, we create our timeline and create functions for running the timeline and launching the event. Finally we create a UCurveFloat for handling how the timeline runs.

protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Name of our timeline
FTimeline LightTimeline;
// This function will be run constantly when timeline is running.
UFUNCTION()
void HandleProgress(float Value);
// This function runs every time the event fires
UFUNCTION
void ChangeColorFunction();
// Our curve. Variable is EditAnywhere, so we can set it in blueprint.
UPROPERTY(EditAnywhere, Category = "Timeline")
UCurveFloat* CurveFloat;

Lets go inside our .CPP file. Here we bind the functions timeline should run and of course, construct our objects.
In Constructor:

virtual void PostInitializeComponents() override;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
USceneComponent* Root;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
UPointLightComponent* Light;

In BeginPlay()

Super::BeginPlay();
if (CurveFloat)
{
// We create two objects, one from FOnTimelineFloat and one from FOnTimelineEvent class. These objects will handle our function binding.
FOnTimelineFloat ProgressFunction;
FOnTimelineEvent Event1;
// This is where we bind our newly created object. We bind them to function by giving them the name of the function.
ProgressFunction.BindUFunction(this, FName("HandleProgress"));
Event1.BindUFunction(this, FName("ChangeColorFunction"));
// Now, we add our binded event and UFunction to our timeline.
LightTimeline.AddEvent(2.0f, Event1);
LightTimeline.AddInterpFloat(CurveFloat, ProgressFunction);		
// We set timeline to be looping and start it!
LightTimeline.SetLooping(true);		
LightTimeline.PlayFromStart();
}

Nothing more to do in BeginPlay(). Lets create declarations for our functions.

void AC_PostInitActor::HandleProgress(float Value)
{
// Brightness is our float value multiplied by 3000. So it goes from 0 - 6000 -0.
Light->SetIntensity(Value * 3000);
}
void AC_PostInitActor::ChangeColorFunction()
{
// When event fires. We get random values and put that to lighColor
FLinearColor NewColor;
NewColor.R = FMath::RandRange(0.0f, 1.0f);
NewColor.G = FMath::RandRange(0.0f, 1.0f);
NewColor.B = FMath::RandRange(0.0f, 1.0f);
Light->SetLightColor(NewColor);
}

Last thing to do in code is to set timeline to run in every tick if it is on. So in our Tick function.

// Called every frame
void AC_PostInitActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// We only tick timeline if the timeline is actually playing.
if (LightTimeline.IsPlaying())
LightTimeline.TickTimeline(DeltaTime);
}

Our coding is done but our timeline dont know how to act because we dont have curve for it to follow. Go ahead and create a LinearCurve. Set it to go from 0-1-0 in some time period. I set it to be two seconds.a1

Now, if you havent already, then create a blueprint based of our class. Set the CurveFloat to be the curveFloat we just created.
a1

Congratulations! You have set a working timeline with a custom event in it! It might seem to be a lot of hassle, but when you sit down and start examinate the code, you will see that it is very simple after all.

gifi

Vastaa

Sähköpostiosoitettasi ei julkaista. Pakolliset kentät on merkitty *