How to create a Timeline in C++?

Hi,

i’m trying to set up a timeline in c++.

.h:

FTimeline TimeLine;
UFUNCTION()
	void EffectProgress(float Value);

.cpp:

Constructor:

	const ConstructorHelpers::FObjectFinder<UCurveFloat> Curve(TEXT("CurveFloat'/Game/OwnStuff/EffectCurve.EffectCurve'"));
	TimeLine = FTimeline{};
	FOnTimelineFloat progressFunction{};
	progressFunction.BindUFunction(this, "EffectProgress");
	TimeLine.AddInterpFloat(Curve.Object, progressFunction, FName{TEXT("EFFECTFADE")});

BeginPlay:

	TimeLine.PlayFromStart();

The function which should get called from the timeline:

    void AEffectBallSpeed::EffectProgress(float Value)
    {
    	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("EFFECT PROGRESS"));
    }

The function is called once. How can it be triggered all the time?
thx :slight_smile:

edit: changed the name of the question to make it more clear and easier to find for others.

2 Likes

Hey oOo.DanBO.oOo-

Timelines only run once for each time they are called. With the timeline being called within BeginPlay, that means that it will only trigger at the start of the level that is being loaded. You would have to call it from another event that can be re-triggered if you want the timeline to repeat.

Cheers

Hi ,
Yes the timeline runs once. But what i want it that as long as the timeline is running, it should call some function or set some value. I would expect, that EffectProgress gets called each tick, as long as the timeline is running and this doesn’t happen.

1 Like

Thats exactly what I want to know too =)

A timeline is used to spread out an event over the length of the timeline (think a drawbridge going up) rather than continuously preforming that event while the timeline is playing. To get the behavior you’re looking for, however, it may be possible to set a boolian to true at the start of the timeline (and reset to false at the end), then use the event Tick to check the boolian and call your EffectProgress method.

This is my actual solution

Effect.h:

/************************************************************************/
/* TIMELINE                                                             */
/************************************************************************/
/** Timeline for the effectprogress*/
FTimeline TimeLine;

/** Deltatime(stepsize) for the timer calling the timeline tick */
static const float DELTATIME;

/** Function which gets called from the Timer to call EffectProgress */
void TickTimeline();

/** Function which gets called from the Timeline on a Tick */
UFUNCTION()
void EffectProgress(float Value);

Effect.cpp:

  • Constructor:

     const ConstructorHelpers::FObjectFinder<UCurveFloat> Curve(TEXT("CurveFloat'/Game/OwnStuff/EffectCurve.EffectCurve'"));
     TimeLine = FTimeline{};
     FOnTimelineFloat progressFunction{};
     progressFunction.BindUFunction(this, "EffectProgress"); // The function EffectProgress gets called
     TimeLine.AddInterpFloat(Curve.Object, progressFunction, FName{TEXT("EFFECTFADE")});
    
  • BeginPlay:

     TimeLine.PlayFromStart();
     FTimerHandle TimerHandle;
     GetWorldTimerManager().SetTimer(TimerHandle, this, &AEffectBallSpeed::TickTimeline, DELTATIME, true, 0.0f);
    
  • Called Function from Timeline

    /** The function which gets called from the timeline tick */
    void AEffectBallSpeed::EffectProgress(float Value)
    {
    GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT(“EffectProgress: timeline: %f value:%f”), TimeLine.GetPlaybackPosition(), Value));
    // do something
    }

  • Called Function from Timer

    /** Function which gets called from the Timer to call EffectProgress */
    void AEffectBallSpeed::TickTimeline()
    {
    if (TimeLine.IsPlaying())
    {
    TimeLine.TickTimeline(DELTATIME);
    }
    else
    {
    GetWorldTimerManager().ClearTimer(this, &AEffectBallSpeed::TickTimeline);
    SetLifeSpan(0);
    }
    }

5 Likes

Hi ,
thanks very much :slight_smile: you brought me on the right track :slight_smile:
i got it all working now.

is also fine for me to check if im at the end of the Timeline.

Thx for the help :slight_smile:

2 Likes

Hi,

I’m trying to transcribe this script for a BPFunction… I Receive error when trying access GetWorldTimerManager() from UBlueprintFunctionLibrary and not found…

I Necessary Create This to Modify parameters why Dynamic Curves Floats… various equals curves in Diff Seconds…

How I Change this line to found method??

GetWorldTimerManager().SetTimer(TimerHandle, xSpotLight, &UVJWorkspace::TickTimeline, DELTATIME, true, 0.0f);

Hey CrashAngel-

GetWorldTimerManager is a function defined in the Actor class. Including Actor.h in the source file that accesses GetWorldTimerManager should allow it to be recognized.

Cheers

This is still Nr1 on Google for c++ timelines and this answer is a bit misleading for using tick of the actor for the timeline. The timeline component itself has already a tick which can be used for this I think. I wrote an answer with a code example here:

And what if you want to use a timeline in an actor component? If i want to use a timeline on an actor then i would use your code :slight_smile:

I write this here, but is EXTREMELY IMPORTANT that the HandleFunction that you bind to the FOnTimelineFloat isa UFUNCTION, if it isn’t UE4 won’t pick it up and it will never trigger. Probably because it needs the reflection given by UFUNCTION to find the method

I followed both this and the other tutorial for Timelines in C++ but I am getting a “Triggered breakpoint” error: Timeline in C++ triggers breakpoint - Programming & Scripting - Unreal Engine Forums

I got it working :smiley:

I added my edits to the wiki, additional settings needed to be set in BeginPlay() to get it to tick and work correctly.

https://wiki.unrealengine.com/Timeline_in_c%2B%2B#How_to_use_Timeline_in_C.2B.2B

For seekers of the old knowledge, it is here: Timeline in C++ (archive)

Copied from: SpaceHarry’s Wiki post, originally sourced from this linked answer.

4 Likes