use a c++ timeline in umg but it only run once

hi

i just create one timeline in my customized umg… but when i make the timeline play, the timeline event only execute once…
below is my code…
when i use the function timeline->playfromstart of play, the test string only appear once on the screen…
can anybody help me? thanks so much indeed!

h.

protected:
        	virtual void NativeConstruct() override;
        	
        private:
        	class UTimelineComponent* Timeline;
        
        	UPROPERTY(EditAnywhere, meta = (AllowPrivateAcess = "true"))
        	UCurveFloat* Curve;
        	
        	FOnTimelineFloat OnTimelineFloat{};

cpp.

void UTimelineWidget::NativeConstruct()
{
	Timeline = NewObject<UTimelineComponent>();
	OnTimelineFloat.BindDynamic(this, &UTimelineWidget::OnTimelineFloatUpdate);
	if (Timeline)
	{
		Timeline->AddInterpFloat(Curve, OnTimelineFloat);
		Timeline->SetTimelineLength(1.f);
	}
}

void UTimelineWidget::OnTimelineFloatUpdate(float Output)
{
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("From Timeline"));
}

When you are using timeline in blueprint, you actually have to check the “loop” option in the curve. I guess you have to do the exact same thing in C++

According to the documentation → here just use SetLopping on your UTimelineComponent and it should loop ! :slight_smile:

Good luck !
Best regars.

EDIT: I miss understood the question, here is my second answer (I just keep the first one in case…; :D)

So here is my though: The event function is called “OnTimelineFloatUpdate” which probably means it will be called only when the float value is changed.
In your current code, you only set the duration of your timeline but you don’t set keyframes. Which means your current timeline is totally flat. So I think your even function is called only once cause your float value never changes !
To fix this, you may have to add at least one keyframe which is different from the starting one !

Good luck :smiley:

I edited my answer :stuck_out_tongue:

I think that the “CreateDefaultSubobject” will initialize your newly created object with some default values. In the case of a UTimelineComponent, it may create a basic [0; 0] to [1; 1] timeline (this is actually what happens in the editor). So the default timeline wil have at least two differents keyframes. That’s why it’s working.

When you are using “NewObject” it will probably just call the contructor of the object and alloc its memory spot but that’s it, you have to setup everything by hand.

Don’t forget to close your question :).
Good luck !

hi joris
thanks for the reply… but i dont mean the loop option…
as you see, in the timeline event, the function is AddOnScreenDebugMessage… so everytime the timeline is played, in the duration the message should be kept printing to the screen… but actually in my project, it is only printed once…

Joris… thanks… i tested the timeline component in other actor… it works…
But in other actor i use the CreateDefaultSubobject to initialize the timeline… when i replace the CreateDefaultxx by NewObject… it problem occurs again…
so i think the point is the NewObject<>, have to read the doc…
anyway thanks very much

hi joris
thanks for the soon reply… but the problem is still there.
for your reference… in an actor, i use the NewObject(this, UTimelineComponent::staticclass(), EObjectFlag::RF_DefaultSubObject), then the timeline in actor becoms normaly, and it keep printing the message.
however in my userwidget, i use the same method and it only print once…
so sad…

Thanks for the clarification. That’s actually a weird behavior

yes…
hope some staff could reply… anyway, thank you very much for your help!

can anybody help…