I want to create a function for interpolate a float without a timeline and not in tick.
I created it using timer, but the problem is, it’s not updating the original float (Passed in Blueprints)
Can someone help?
> UFUNCTION(BlueprintCallable, Category="Tween")
> static void InterpolateFloat(UObject* Context, UPARAM(ref) float& InFloat, float TargetValue, float Duration, UCurveFloat* Curve, FOnTweenCompleted OnCompleted);
> void UTweenFunctionLibrary::InterpolateFloat(UObject* Context, float& InFloat, float TargetValue, float Duration,UCurveFloat* Curve, FOnTweenCompleted OnCompleted)
> {
> if (UWorld* World = Context->GetWorld())
> {
> float StartValue = InFloat;
> float ElapsedTime = 0;
> float Alpha = 0;
>
> FTimerManager& TimerManager = World->GetTimerManager();
> TSharedPtr<FTimerHandle> TimerHandle = MakeShared<FTimerHandle>();
>
> UE_LOG(LogTemp, Type::Warning, TEXT("Start Tween"));
> TimerManager.SetTimer(*TimerHandle, [Context, UPARAM(ref) &InFloat, StartValue, TargetValue, ElapsedTime, Duration, Alpha, OnCompleted, TimerHandle]() mutable
> {
> if (Alpha < 1)
> {
> ElapsedTime += Context->GetWorld()->GetDeltaSeconds();
> Alpha = ElapsedTime / Duration;
>
> const float NewValue = FMath::Lerp(StartValue, TargetValue, Alpha);
> InFloat = NewValue;
>
> UE_LOG(LogTemp, Type::Warning, TEXT("The value is %f"), InFloat);
> }
> else
> {
> Context->GetWorld()->GetTimerManager().ClearTimer(*TimerHandle);
> if (OnCompleted.IsBound()) OnCompleted.Execute();
>
> UE_LOG(LogTemp, Type::Warning, TEXT("Stopped Tween. Float is %f"), InFloat);
> }
> },
> World->GetDeltaSeconds(), true);
> }
> }