UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
AnimatedAttribute.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Misc/Attribute.h"
8
12template< typename NumericType >
14{
15public:
16
24 using FGetter = TDelegate<NumericType()>;
25
26protected:
27
28 struct FPrivateToken { explicit FPrivateToken() = default; };
29
32
33public:
34
43 template< typename InterpolatorSettings >
45 : Interpolator( MakeUnique<typename InterpolatorSettings::InterpolatorType>(InSettings) )
46 , Getter()
47 , DesiredValue()
48 {
49 }
50
57 template< typename InterpolatorSettings, typename OtherType >
65
73 template< typename InterpolatorSettings >
82
90 template< typename InterpolatorSettings >
99
108 template< typename OtherType >
109 void Set( const OtherType& InNewValue )
110 {
111 Getter.Unbind();
112 if(DesiredValue.IsSet() && !Interpolator->DesiredValue.IsSet())
113 {
114 Interpolator->SetValue(DesiredValue.GetValue());
115 }
116 DesiredValue = InNewValue;
117 Interpolator->SetValue(DesiredValue.GetValue());
118 }
119
128 void Set( NumericType&& InNewValue )
129 {
130 Getter.Unbind();
131 if(DesiredValue.IsSet() && !Interpolator->DesiredValue.IsSet())
132 {
133 Interpolator->SetValue(DesiredValue.GetValue());
134 }
135 DesiredValue = InNewValue;
136 Interpolator->SetValue(DesiredValue.GetValue());
137 }
138
145 void SetValueAndStop( const NumericType& InNewValue )
146 {
148 Interpolator->Reset();
149 }
150
157 void SetValueAndStop( NumericType&& InNewValue )
158 {
160 Interpolator->Reset();
161 }
162
164 bool IsSet() const
165 {
166 return DesiredValue.IsSet() || IsBound();
167 }
168
175 const NumericType& Get() const
176 {
177 // If we have a getter delegate, then we'll call that to generate the value
178 if( Getter.IsBound() )
179 {
180 // Call the delegate to get the value. Note that this will assert if the delegate is not currently
181 // safe to call (e.g. object was deleted and we could detect that)
182
183 // NOTE: We purposely overwrite our value copy here so that we can return the value by address in
184 // the most common case, which is an attribute that doesn't have a delegate bound to it at all.
185 DesiredValue = Getter.Execute();
186
187 // Update the interpolator
188 if(DesiredValue.IsSet())
189 {
190 Interpolator->SetValue(DesiredValue.GetValue());
191 }
192 }
193
194 // first see if the interpolator has a value
195 if(Interpolator->IsSet())
196 {
197 return Interpolator->Get();
198 }
199
200 if(DesiredValue.IsSet())
201 {
202 // Return the stored value
203 return DesiredValue.GetValue();
204 }
205
206 static const NumericType EmptyResult = NumericType();
207 return EmptyResult;
208 }
209
214 const NumericType& Get( const NumericType& DefaultValue ) const
215 {
216 return IsSet() ? Get() : DefaultValue;
217 }
218
222 const NumericType& GetDesiredValue() const
223 {
224 if(DesiredValue.IsSet())
225 {
226 return DesiredValue.GetValue();
227 }
228 static const NumericType EmptyResult = NumericType();
229 return EmptyResult;
230 }
231
235 double GetOverAllDeltaTime() const
236 {
237 return Interpolator->GetOverAllDeltaTime();
238 }
239
244 {
245 return Interpolator->Delay;
246 }
247
252 {
253 Interpolator->SetDelayOneShot(InDelay);
254 }
255
259 void SetTolerance( double Tolerance )
260 {
261 Interpolator->SetTolerance(Tolerance);
262 }
263
267 bool IsPlaying() const
268 {
269 return Interpolator->IsPlaying();
270 }
271
275 void EnableInterpolation(bool bEnabled = true)
276 {
277 Interpolator->SetEnabled(bEnabled);
278 }
279
284 {
285 EnableInterpolation(false);
286 }
287
293 bool IsBound() const
294 {
295 return Getter.IsBound();
296 }
297
305 {
306 if(!Interpolator->IdenticalTo(InOther.Interpolator.Get()))
307 {
308 return false;
309 }
310
311 const bool bIsBound = IsBound();
312 if ( bIsBound == InOther.IsBound() )
313 {
314 if ( bIsBound )
315 {
316 return Getter.GetHandle() == InOther.Getter.GetHandle();
317 }
318 return IsSet() == InOther.IsSet() && DesiredValue == InOther.DesiredValue;
319 }
320
321 return false;
322 }
323
324 /*
325 * Returns the delegate to react to the interpolator starting to interpolate
326 */
328 {
329 return Interpolator->OnInterpolationStarted();
330 }
331
332 /*
333 * Returns the delegate to react to the interpolator ending to interpolate
334 */
336 {
337 return Interpolator->OnInterpolationStopped();
338 }
339
340private:
341
345 virtual void Tick(float InDeltaTime) override
346 {
347 if( Getter.IsBound() )
348 {
349 Interpolator->SetValue(Getter.Execute());
350 }
351 if(DesiredValue.IsSet())
352 {
353 Interpolator->SetValue(DesiredValue.GetValue());
354 }
355 Interpolator->Tick(InDeltaTime);
356 }
357
358 // We declare ourselves as a friend (templated using OtherType) so we can access members as needed
359 template< class OtherType > friend class TAnimatedAttribute;
360
363
367 FGetter Getter;
368
370 mutable TOptional< NumericType > DesiredValue;
371};
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
UE_FORCEINLINE_HINT TUniquePtr< T > MakeUnique(TArgs &&... Args)
Definition UniquePtr.h:918
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
Definition AnimatedAttributeManager.h:12
Definition AnimatedAttribute.h:14
double GetOverAllDeltaTime() const
Definition AnimatedAttribute.h:235
void SetDelayOneShot(double InDelay)
Definition AnimatedAttribute.h:251
void SetValueAndStop(const NumericType &InNewValue)
Definition AnimatedAttribute.h:145
static TSharedRef< TAnimatedAttribute > Create(const InterpolatorSettings &InSettings, OtherType &&InInitialValue)
Definition AnimatedAttribute.h:58
const NumericType & Get(const NumericType &DefaultValue) const
Definition AnimatedAttribute.h:214
void Set(NumericType &&InNewValue)
Definition AnimatedAttribute.h:128
void EnableInterpolation(bool bEnabled=true)
Definition AnimatedAttribute.h:275
bool IsSet() const
Definition AnimatedAttribute.h:164
void Set(const OtherType &InNewValue)
Definition AnimatedAttribute.h:109
TAttributeInterpolator< NumericType >::FInterpolatorEvent & OnInterpolationStopped()
Definition AnimatedAttribute.h:335
void SetTolerance(double Tolerance)
Definition AnimatedAttribute.h:259
TAttributeInterpolator< NumericType >::FInterpolatorEvent & OnInterpolationStarted()
Definition AnimatedAttribute.h:327
TOptional< NumericType > GetDelay() const
Definition AnimatedAttribute.h:243
void SetValueAndStop(NumericType &&InNewValue)
Definition AnimatedAttribute.h:157
static TSharedRef< TAnimatedAttribute > CreateWithGetter(const InterpolatorSettings &InSettings, FGetter &&InGetter, const TOptional< NumericType > &InDefaultValue=TOptional< NumericType >())
Definition AnimatedAttribute.h:91
bool IsPlaying() const
Definition AnimatedAttribute.h:267
void DisableInterpolation()
Definition AnimatedAttribute.h:283
TAnimatedAttribute(FPrivateToken, const InterpolatorSettings &InSettings)
Definition AnimatedAttribute.h:44
bool IsBound() const
Definition AnimatedAttribute.h:293
const NumericType & Get() const
Definition AnimatedAttribute.h:175
const NumericType & GetDesiredValue() const
Definition AnimatedAttribute.h:222
static TSharedRef< TAnimatedAttribute > CreateWithGetter(const InterpolatorSettings &InSettings, const FGetter &InGetter, const TOptional< NumericType > &InDefaultValue=TOptional< NumericType >())
Definition AnimatedAttribute.h:74
TAnimatedAttribute()=delete
bool IdenticalTo(const TAnimatedAttribute &InOther) const
Definition AnimatedAttribute.h:304
Definition AttributeInterpolator.h:20
Definition SharedPointer.h:153
Definition UniquePtr.h:107
void Reset(T *InPtr=nullptr)
Definition UniquePtr.h:346
UE_FORCEINLINE_HINT T * Get() const
Definition UniquePtr.h:324
Definition AnimatedAttribute.h:28
Definition Optional.h:131
constexpr OptionalType & GetValue()
Definition Optional.h:443
constexpr bool IsSet() const
Definition Optional.h:69