UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
AttributeInterpolator.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
7
9{
10 Easing,
11 Arrive,
12 Verlet
13};
14
18template< typename NumericType >
20{
21
22public:
23
25
26 /*
27 * Default constructor
28 */
30 : bEnabled(true)
31 , Tolerance(0.001f)
32 {
33 }
34
35 /*
36 * Default destructor
37 */
39 {
40 }
41
49 static bool Equals(const NumericType& InA, const NumericType& InB, double InTolerance = 0.001)
50 {
51 return InA.Equals(InB, InTolerance);
52 }
53
61
68 virtual bool IdenticalTo(const TAttributeInterpolator* InOther) const = 0;
69
74 bool IsSet() const
75 {
76 return DesiredValue.IsSet();
77 }
78
82 void Reset() const
83 {
84 const bool bFireStopEvent = Stop();
88 Delay.Reset();
90 {
92 }
93 }
94
95 /*
96 * Returns true if the interpolator is enabled
97 */
98 bool IsEnabled() const
99 {
100 return bEnabled;
101 }
102
103 /*
104 * Enables (or disables) the interpolator
105 */
106 void SetEnabled(bool InEnabled = true)
107 {
108 if(bEnabled != InEnabled)
109 {
111
112 // if we disabled or re-enabled the interpolator set
113 // the interpolated values based on the desired value
114 if(DesiredValue.IsSet())
115 {
117 }
118 }
119 }
120
126 void SetValue(const NumericType& InValue) const
127 {
128 if(DesiredValue.IsSet())
129 {
131 {
132 return;
133 }
134
135 if(!LastValue.IsSet())
136 {
138 }
140 }
141 else
142 {
144 }
145 }
146
152 void SetValueAndStop(const NumericType& InValue) const
153 {
154 const bool bFireStopEvent = Stop();
157 {
158 InterpolationStopped.Broadcast(InValue);
159 }
160 }
161
167 const NumericType& Get() const
168 {
169 check(IsSet());
170 if(!IsEnabled())
171 {
172 return DesiredValue.GetValue();
173 }
175 {
177 }
178 if(LastValue.IsSet())
179 {
180 return LastValue.GetValue();
181 }
182 return DesiredValue.GetValue();
183 }
184
188 void Tick(float InDeltaTime)
189 {
190 if(!IsPlaying() && LastValue.IsSet())
191 {
192 const NumericType& CurrentValue = InterpolatedValue.IsSet() ? InterpolatedValue.GetValue() : DesiredValue.GetValue();
193 if(Equals(CurrentValue, LastValue.GetValue(), Tolerance))
194 {
196 return;
197 }
198 }
199
200 if(Delay.IsSet())
201 {
202 const float DelayLeft = Delay.GetValue() - InDeltaTime;
204 {
206 return;
207 }
208 Delay.Reset();
210 }
211
213 Interpolate();
214
216 {
218 }
219 }
220
224 double GetOverAllDeltaTime() const
225 {
226 if(IsPlaying())
227 {
229 {
230 return OverallDeltaTime.GetValue();
231 }
232 }
233 return 0.0f;
234 }
235
239 void SetDelayOneShot(float InDelay) const
240 {
241 Delay = InDelay;
242 }
243
248 {
250 }
251
252 /*
253 * Returns the delegate to react to the interpolator starting to interpolate
254 */
256
257 /*
258 * Returns the delegate to react to the interpolator ending to interpolate
259 */
261
262protected:
263
264 // Interpolates the internal value against the new value
265 // This method is expected to set the Value member
266 virtual void Interpolate() const = 0;
267
268 // Returns true if this interpolator is currently playing
269 bool IsPlaying() const
270 {
271 return InterpolatedValue.IsSet();
272 }
273
274 // Starts the animation and records time
275 virtual bool Start() const
276 {
278 OverallDeltaTime = 0.f;
279 return true;
280 }
281
282 virtual bool PlayIfStopped() const
283 {
284 if(!IsPlaying())
285 {
286 return Start();
287 }
288 return false;
289 }
290
291 // Stops the animation and stops recording time
292 virtual bool Stop() const
293 {
294 Delay.Reset();
296 if(IsPlaying())
297 {
299 return true;
300 }
301 return false;
302 }
303
304 // restart the animation
305 virtual bool Restart() const
306 {
307 return Start();
308 }
309
317
320
321 template<typename> friend class TAnimatedAttribute;
322};
323
324template<>
325inline bool TAttributeInterpolator<float>::Equals(const float& InA, const float& InB, double InTolerance)
326{
327 return FMath::IsNearlyEqual(InA, InB, (float)InTolerance);
328}
329
330template<>
331inline bool TAttributeInterpolator<double>::Equals(const double& InA, const double& InB, double InTolerance)
332{
334}
335
370
372
373template< typename NumericType >
375
381template< typename NumericType >
383{
384private:
385
387
388public:
389
403
405 : Super()
407 {
408 }
409
411 // TAttributeInterpolator interface
412
413 virtual bool IsTypeOf(EAttributeInterpolatorType InType) const override
414 {
416 }
417
418 virtual bool IdenticalTo(const TAttributeInterpolator<NumericType>* InOther) const override
419 {
421 {
422 return false;
423 }
424
425 // C++ cast due to polymorphism dynamic_cast is not available
428
429 return FMath::IsNearlyEqual(Settings.Duration, CastInterpolator->Settings.Duration);
430 }
431
432protected:
433
434 virtual void Interpolate() const override
435 {
438
439 const float DeltaTime = (float)Super::GetOverAllDeltaTime();
440 if(Settings.Duration <= 0.f || DeltaTime > Settings.Duration)
441 {
443 return;
444 }
445
446 const float Ratio = FMath::Clamp<float>(DeltaTime / Settings.Duration, 0.f, 1.f);
448 Super::InterpolatedValue = FMath::Lerp<NumericType>(Super::LastValue.GetValue(), Super::DesiredValue.GetValue(), EasedRatio);
449 }
450
452};
453
457template< typename NumericType >
459{
460private:
461
463
464public:
465
479
481 : Super()
483 , LastDeltaTime(0)
484 {
485 }
486
488 // TAttributeInterpolator interface
489
490 virtual bool IsTypeOf(EAttributeInterpolatorType InType) const override
491 {
493 }
494
495 virtual bool IdenticalTo(const TAttributeInterpolator<NumericType>* InOther) const override
496 {
498 {
499 return false;
500 }
501
502 // C++ cast due to polymorphism dynamic_cast is not available
505
506 return (Settings.Iterations == CastInterpolator->Settings.Iterations) &&
508 }
509
510protected:
511
512 virtual bool Start() const override
513 {
514 LastDeltaTime = 0.0;
515 return Super::Start();
516 }
517
518 virtual bool Restart() const override
519 {
520 const bool bWasPlaying = Super::IsPlaying();
522
523 const bool bResult = Super::Restart();
524 if(bWasPlaying)
525 {
526 if(PreviousValue.IsSet())
527 {
529 }
530 }
531 return bResult;
532 }
533
534 virtual void Interpolate() const override
535 {
538
541
542 const float Blend = FMath::Max<float>(Settings.Strength, 0) * 8.f * LocalDeltaTime;
543
544 for(int32 Iteration = 0; Iteration < Settings.Iterations; Iteration++)
545 {
546 Super::InterpolatedValue = FMath::Lerp<NumericType>(
549 Blend);
550 }
551
553
555 {
557 }
558 }
559
561 mutable float LastDeltaTime;
562};
563
567template< typename NumericType >
569{
570private:
571
573
574public:
575
577 {
579
581 : Blend(InBlend)
584 {
585 }
586
587 float Blend;
588 float Strength;
589 float Damping;
590 };
591
593 : Super()
595 , LastDeltaTime(0)
596 {
597 }
598
600 // TAttributeInterpolator interface
601
602 virtual bool IsTypeOf(EAttributeInterpolatorType InType) const override
603 {
605 }
606
607 virtual bool IdenticalTo(const TAttributeInterpolator<NumericType>* InOther) const override
608 {
610 {
611 return false;
612 }
613
614 // C++ cast due to polymorphism dynamic_cast is not available
617
618 return FMath::IsNearlyEqual(Settings.Blend, CastInterpolator->Settings.Blend) &&
621 }
622
623protected:
624
625 virtual bool Start() const override
626 {
627 LastDeltaTime = 0.0;
628 Velocity = NumericType(0);
629 return Super::Start();
630 }
631
632 virtual bool Restart() const override
633 {
634 const bool bWasPlaying = Super::IsPlaying();
637
638 const bool bResult = Super::Restart();
639 if(bWasPlaying)
640 {
641 if(PreviousValue.IsSet())
642 {
644 }
645 if(PreviousVelocity.IsSet())
646 {
648 }
649 }
650 return bResult;
651 }
652
653 virtual void Interpolate() const override
654 {
657
660
661 const float U = FMath::Clamp<float>(FMath::Max<float>(Settings.Blend, 0) * 8.f * LocalDeltaTime, 0, 1);
662 const float ScaleDown = FMath::Clamp<float>(1.f - Settings.Damping, 0.f, 1.f);
663
665 const NumericType Force = (Super::DesiredValue.GetValue() - PreviousValue) * Settings.Strength;
666 const NumericType PreviousVelocity = Velocity.GetValue();
667
668 Velocity = FMath::Lerp<NumericType>(PreviousVelocity, Force, U) * ScaleDown;
670
672
675 {
677 }
678 }
679
681 mutable float LastDeltaTime;
683};
OODEFFUNC typedef void(OODLE_CALLBACK t_fp_OodleCore_Plugin_Free)(void *ptr)
#define check(expr)
Definition AssertionMacros.h:314
EEasingInterpolatorType
Definition AttributeInterpolator.h:337
SLATE_API float EaseInterpolatorRatio(EEasingInterpolatorType InEasingType, float InRatio)
Definition AttributeInterpolator.cpp:6
EAttributeInterpolatorType
Definition AttributeInterpolator.h:9
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
return true
Definition ExternalRpcRegistry.cpp:601
USkinnedMeshComponent float
Definition SkinnedMeshComponent.h:60
#define SMALL_NUMBER
Definition UnrealMathUtility.h:66
uint8_t uint8
Definition binka_ue_file_header.h:8
Definition AnimatedAttribute.h:14
Definition AttributeInterpolator.h:459
virtual bool IsTypeOf(EAttributeInterpolatorType InType) const override
Definition AttributeInterpolator.h:490
virtual void Interpolate() const override
Definition AttributeInterpolator.h:534
float LastDeltaTime
Definition AttributeInterpolator.h:561
virtual bool Restart() const override
Definition AttributeInterpolator.h:518
virtual bool Start() const override
Definition AttributeInterpolator.h:512
virtual bool IdenticalTo(const TAttributeInterpolator< NumericType > *InOther) const override
Definition AttributeInterpolator.h:495
TArriveAttributeInterpolator(const FSettings &InSettings)
Definition AttributeInterpolator.h:480
FSettings Settings
Definition AttributeInterpolator.h:560
Definition AttributeInterpolator.h:20
bool IsEnabled() const
Definition AttributeInterpolator.h:98
TOptional< float > Delay
Definition AttributeInterpolator.h:312
void Reset() const
Definition AttributeInterpolator.h:82
double GetOverAllDeltaTime() const
Definition AttributeInterpolator.h:224
bool IsSet() const
Definition AttributeInterpolator.h:74
void SetValueAndStop(const NumericType &InValue) const
Definition AttributeInterpolator.h:152
static bool Equals(const NumericType &InA, const NumericType &InB, double InTolerance=0.001)
Definition AttributeInterpolator.h:49
TOptional< float > OverallDeltaTime
Definition AttributeInterpolator.h:313
FInterpolatorEvent & OnInterpolationStopped()
Definition AttributeInterpolator.h:260
const NumericType & Get() const
Definition AttributeInterpolator.h:167
DECLARE_MULTICAST_DELEGATE_OneParam(FInterpolatorEvent, TOptional< NumericType >)
TOptional< NumericType > DesiredValue
Definition AttributeInterpolator.h:316
void SetValue(const NumericType &InValue) const
Definition AttributeInterpolator.h:126
virtual bool PlayIfStopped() const
Definition AttributeInterpolator.h:282
TOptional< NumericType > LastValue
Definition AttributeInterpolator.h:315
FInterpolatorEvent InterpolationStopped
Definition AttributeInterpolator.h:319
virtual ~TAttributeInterpolator()
Definition AttributeInterpolator.h:38
bool IsPlaying() const
Definition AttributeInterpolator.h:269
void SetEnabled(bool InEnabled=true)
Definition AttributeInterpolator.h:106
FInterpolatorEvent & OnInterpolationStarted()
Definition AttributeInterpolator.h:255
bool bEnabled
Definition AttributeInterpolator.h:310
FInterpolatorEvent InterpolationStarted
Definition AttributeInterpolator.h:318
virtual bool Restart() const
Definition AttributeInterpolator.h:305
TAttributeInterpolator()
Definition AttributeInterpolator.h:29
void Tick(float InDeltaTime)
Definition AttributeInterpolator.h:188
virtual bool Start() const
Definition AttributeInterpolator.h:275
virtual void Interpolate() const =0
void SetDelayOneShot(float InDelay) const
Definition AttributeInterpolator.h:239
void SetTolerance(float InTolerance)
Definition AttributeInterpolator.h:247
virtual bool IdenticalTo(const TAttributeInterpolator *InOther) const =0
TOptional< NumericType > InterpolatedValue
Definition AttributeInterpolator.h:314
virtual bool Stop() const
Definition AttributeInterpolator.h:292
float Tolerance
Definition AttributeInterpolator.h:311
virtual bool IsTypeOf(EAttributeInterpolatorType InType) const =0
Definition AttributeInterpolator.h:383
virtual bool IdenticalTo(const TAttributeInterpolator< NumericType > *InOther) const override
Definition AttributeInterpolator.h:418
TEasingAttributeInterpolator(const FSettings &InSettings)
Definition AttributeInterpolator.h:404
FSettings Settings
Definition AttributeInterpolator.h:451
virtual void Interpolate() const override
Definition AttributeInterpolator.h:434
virtual bool IsTypeOf(EAttributeInterpolatorType InType) const override
Definition AttributeInterpolator.h:413
Definition AttributeInterpolator.h:569
virtual bool IsTypeOf(EAttributeInterpolatorType InType) const override
Definition AttributeInterpolator.h:602
virtual bool Start() const override
Definition AttributeInterpolator.h:625
virtual void Interpolate() const override
Definition AttributeInterpolator.h:653
TVerletAttributeInterpolator(const FSettings &InSettings)
Definition AttributeInterpolator.h:592
virtual bool Restart() const override
Definition AttributeInterpolator.h:632
float LastDeltaTime
Definition AttributeInterpolator.h:681
virtual bool IdenticalTo(const TAttributeInterpolator< NumericType > *InOther) const override
Definition AttributeInterpolator.h:607
TOptional< NumericType > Velocity
Definition AttributeInterpolator.h:682
FSettings Settings
Definition AttributeInterpolator.h:680
static UE_FORCEINLINE_HINT bool IsNearlyEqual(float A, float B, float ErrorTolerance=UE_SMALL_NUMBER)
Definition UnrealMathUtility.h:388
Definition AttributeInterpolator.h:467
int32 Iterations
Definition AttributeInterpolator.h:476
FSettings(int32 InIterations, float InStrength)
Definition AttributeInterpolator.h:470
float Strength
Definition AttributeInterpolator.h:477
TArriveAttributeInterpolator< NumericType > InterpolatorType
Definition AttributeInterpolator.h:468
Definition AttributeInterpolator.h:391
TEasingAttributeInterpolator< NumericType > InterpolatorType
Definition AttributeInterpolator.h:392
float Duration
Definition AttributeInterpolator.h:401
EEasingInterpolatorType EasingType
Definition AttributeInterpolator.h:400
FSettings(EEasingInterpolatorType InEasingType, float InDuration)
Definition AttributeInterpolator.h:394
Definition Optional.h:131
constexpr OptionalType & GetValue()
Definition Optional.h:443
constexpr bool IsSet() const
Definition Optional.h:69
void Reset()
Definition Optional.h:306
Definition AttributeInterpolator.h:577
float Strength
Definition AttributeInterpolator.h:588
TVerletAttributeInterpolator< NumericType > InterpolatorType
Definition AttributeInterpolator.h:578
float Damping
Definition AttributeInterpolator.h:589
FSettings(float InBlend, float InStrength, float InDamping)
Definition AttributeInterpolator.h:580
float Blend
Definition AttributeInterpolator.h:587