UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
AnimNodeMessages.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
10struct FAnimNode_Base;
11class IAnimNotifyEventContextDataInterface;
12
13// Simple RTTI implementation for graph messages
14
15// Macro to be used in declaration of any new IGraphMessage types
16#define DECLARE_ANIMGRAPH_MESSAGE(ClassName) \
17 public: \
18 static FName GetStaticTypeName() { return ClassName::TypeName; } \
19 virtual FName GetTypeName() const final { return ClassName::GetStaticTypeName(); } \
20 private: \
21 static const FName TypeName;
22
23#define DECLARE_ANIMGRAPH_MESSAGE_API(ClassName, ModuleApi) \
24 public: \
25 static FName GetStaticTypeName() { return ClassName::TypeName; } \
26 virtual FName GetTypeName() const final { return ClassName::GetStaticTypeName(); } \
27 private: \
28 ModuleApi static const FName TypeName;
29
30// Macro to be used in the implementation of any new IGraphMessage types
31#define IMPLEMENT_ANIMGRAPH_MESSAGE(ClassName) \
32 const FName ClassName::TypeName = TEXT(#ClassName);
33
34#define IMPLEMENT_NOTIFY_CONTEXT_INTERFACE(ClassName) \
35 IMPLEMENT_ANIMGRAPH_MESSAGE(ClassName);
36
37#define DECLARE_NOTIFY_CONTEXT_INTERFACE(ClassName) \
38 DECLARE_ANIMGRAPH_MESSAGE(ClassName)
39
40#define DECLARE_NOTIFY_CONTEXT_INTERFACE_API(ClassName, ModuleApi) \
41 DECLARE_ANIMGRAPH_MESSAGE_API(ClassName, ModuleApi)
42
43namespace UE { namespace Anim {
44
46{
47public:
49 // RTTI support functions
51 {
52 return BaseTypeName;
53 }
54
55 virtual FName GetTypeName() const = 0;
56
57 template<typename Type>
58 bool Is() const
59 {
60 return GetTypeName() == Type::GetStaticTypeName();
61 }
62
63 template<typename Type>
64 const Type& As() const
65 {
66 return *static_cast<const Type*>(this);
67 }
68
69 template<typename Type>
70 Type& As()
71 {
72 return *static_cast<Type*>(this);
73 }
74private:
75 static ENGINE_API const FName BaseTypeName;
76};
77
78// Base class for all messages/events/scopes that are fired during execution
79// Note that these messages are events/callbacks only, no blending is involved.
81{
82public:
83 virtual ~IGraphMessage() = default;
84
85public:
86 // RTTI support functions
88 {
89 return BaseTypeName;
90 }
91
92 virtual FName GetTypeName() const = 0;
93
94 template<typename Type>
95 bool Is() const
96 {
97 return GetTypeName() == Type::GetStaticTypeName();
98 }
99
100 template<typename Type>
101 const Type& As() const
102 {
103 return *static_cast<const Type*>(this);
104 }
105
106 template<typename Type>
107 Type& As()
108 {
109 return *static_cast<Type*>(this);
110 }
111
112 UE_DEPRECATED(5.0, "No longer in use, please use MakeUniqueEventContextData")
118
123
124private:
125 static ENGINE_API const FName BaseTypeName;
126};
127
128// Pushes a simple tag onto the shared context stack
130{
133
134private:
135 // Shared context that we push messages into
136 FAnimationUpdateSharedContext* SharedContext;
137
138 // The tag that we pushed
139 FName Tag;
140};
141
142// Base helper class
144{
145protected:
147
148 // Helper functions
150 ENGINE_API void PopMessage();
151
152 // Shared context that we push messages into
154
155 // The type of the message we pushed
157};
158
159// Pushes a message onto the shared context stack
160template<typename TGraphMessageType>
162{
163 template<typename... TArgs>
166 {
167 static_assert(TIsDerivedFrom<TGraphMessageType, IGraphMessage>::IsDerived, "Argument TGraphMessageType must derive from IGraphMessage");
168 static_assert(!std::is_same_v<TGraphMessageType, IGraphMessage>, "Argument TGraphMessageType must not be IGraphMessage");
169
171 PushMessage(InContext, Message, Message->GetTypeName());
172 }
173
175 {
176 PopMessage();
177 }
178};
179
180// Optionally pushes a message onto the shared context stack
181template<typename TGraphMessageType>
183{
184 template<typename... TArgs>
187 , bCondition(bInCondition)
188 {
189 static_assert(TIsDerivedFrom<TGraphMessageType, IGraphMessage>::IsDerived, "Argument TGraphMessageType must derive from IGraphMessage");
190 static_assert(!std::is_same_v<TGraphMessageType, IGraphMessage>, "Argument TGraphMessageType must not be IGraphMessage");
191
192 if(bInCondition)
193 {
195 PushMessage(InContext, Message, Message->GetTypeName());
196 }
197 }
198
200 {
201 if(bCondition)
202 {
203 PopMessage();
204 }
205 }
206
207private:
208 // Record of condition
209 bool bCondition;
210};
211
212// Stack of tags & events used to track context during graph execution
214{
215public:
216 friend struct FScopedGraphTag;
217 friend struct FScopedGraphMessage;
218
219 FMessageStack() = default;
220
221 // Non-copyable
224
227
228 // Return value for the various ForEach* enumeration functions
229 enum class EEnumerate
230 {
231 Stop,
232 Continue,
233 };
234
235 // Info about a node
241
242 // Call the supplied function with each node subscribed to the specified message in this stack
243 // @param InFunction The function to call with the specified message. Function should return whether to continue or stop the enumeration.
244 template<typename TGraphMessageType>
246 {
247 static_assert(TIsDerivedFrom<TGraphMessageType, IGraphMessage>::IsDerived, "Argument TGraphMessageType must derive from IGraphMessage");
248 static_assert(!std::is_same_v<TGraphMessageType, IGraphMessage>, "Argument TGraphMessageType must not be IGraphMessage");
249
250 ForEachMessage(TGraphMessageType::GetStaticTypeName(), [&InFunction](IGraphMessage& InMessage)
251 {
252 return InFunction(static_cast<TGraphMessageType&>(InMessage));
253 });
254 }
255
256 // Call the supplied function on the top-most node subscribed to the specified message in this stack
257 // @param InFunction The function to call with the specified message.
258 template<typename TGraphMessageType>
260 {
261 static_assert(TIsDerivedFrom<TGraphMessageType, IGraphMessage>::IsDerived, "Argument TGraphMessageType must derive from IGraphMessage");
262 static_assert(!std::is_same_v<TGraphMessageType, IGraphMessage>, "Argument TGraphMessageType must not be IGraphMessage");
263
264 TopMessage(TGraphMessageType::GetStaticTypeName(), [&InFunction](IGraphMessage& InMessage)
265 {
266 InFunction(static_cast<TGraphMessageType&>(InMessage));
267 });
268 }
269
270 // Call the supplied function on the top-most node subscribed to the specified message in this stack
271 // @param InFunction The function to call with the specified message.
272 template<typename TGraphMessageType>
274 {
275 static_assert(TIsDerivedFrom<TGraphMessageType, IGraphMessage>::IsDerived, "Argument TGraphMessageType must derive from IGraphMessage");
276 static_assert(!std::is_same_v<TGraphMessageType, IGraphMessage>, "Argument TGraphMessageType must not be IGraphMessage");
277
278 TopMessageSharedPtr(TGraphMessageType::GetStaticTypeName(), [&InFunction](TSharedPtr<IGraphMessage>& InMessage)
279 {
282 InFunction(WeakPtr);
283 });
284 }
285
286 // @return true if a message of the specified type is present
287 template<typename TGraphMessageType>
288 bool HasMessage() const
289 {
290 static_assert(TIsDerivedFrom<TGraphMessageType, IGraphMessage>::IsDerived, "Argument TGraphMessageType must derive from IGraphMessage");
291 static_assert(!std::is_same_v<TGraphMessageType, IGraphMessage>, "Argument TGraphMessageType must not be IGraphMessage");
292
293 return HasMessage(TGraphMessageType::GetStaticTypeName());
294 }
295
296 // Call the supplied function with each node tagged with the specified tag
297 // @param InTagId The tag to check
298 // @param InFunction The function to call with the specified tagged node. Function should return whether to continue or stop the enumeration.
300
301 // Copies the relevant parts of each stack only, ready for cached update.
302 // @param InStack The stack to copy from
303 ENGINE_API void CopyForCachedUpdate(const FMessageStack& InStack);
304
305 // Call MakeEventContextData for the top entry of each MessageType, returning interfaces for types that return event data
306 // @param ContextData An array of valid IAnimNotifyEventContextDataInterface. Only one entry will exist per message type
308
309private:
310 // Push a message onto the stack
311 // @param InNode The node that this message was pushed from
312 // @param InMessage The message to push
314
315 // Pop a message off the stack
316 ENGINE_API void PopMessage(FName InMessageType);
317
318 // Helper function for the templated function of the same name above
320
321 // Helper function for the templated function of the same name above
323
324 // Helper function for the templated function of the same name above
325 void TopMessageSharedPtr(FName InMessageType, TFunctionRef<void(TSharedPtr<IGraphMessage>&)> InFunction);
326
327 // Helper function for the templated function of the same name above
328 ENGINE_API void BottomMessage(FName InMessageType, TFunctionRef<void(IGraphMessage&)> InFunction) const;
329
330 // @return true if a message of the specified type is present
332
333 // Push a tag onto the stack
334 // @param InNode The node that this tag was pushed from
335 // @param InTag The tag to push
337
338 // Pop a tag off the stack
339 // @param InTag The tag to pop
340 ENGINE_API void PopTag(FName InTag);
341
342private:
343 // Holds a message that has been pushed onto the stack.
344 // Message is held by a shared ptr because the same stack scope can potentially be
345 // referenced by multiple cached poses (pesky DAG)
346 struct FMessageEntry
347 {
349 FNodeInfo NodeInfo;
350 };
351
352 using FMessageStackEntry = TArray<FMessageEntry, TInlineAllocator<8>>;
354
355 // Message stack
356 FMessageMap MessageStacks;
357
358 using FTagStackEntry = TArray<FNodeInfo, TInlineAllocator<4>>;
360
361 // Tag stack
362 FTagMap TagStacks;
363};
364
365}} // namespace UE::Anim
#define UE_DEPRECATED(Version, Message)
Definition CoreMiscDefines.h:302
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
Definition NameTypes.h:617
Definition Array.h:670
Definition AssetRegistryState.h:50
Definition UnrealString.h.inl:34
Definition SharedPointer.h:692
Definition SharedPointer.h:153
Definition UniquePtr.h:107
Definition SharedPointer.h:1295
Definition AnimNodeMessages.h:46
bool Is() const
Definition AnimNodeMessages.h:58
static FName GetStaticTypeName()
Definition AnimNodeMessages.h:50
const Type & As() const
Definition AnimNodeMessages.h:64
Type & As()
Definition AnimNodeMessages.h:70
Definition AnimNodeMessages.h:81
virtual ~IGraphMessage()=default
virtual TSharedPtr< const IAnimNotifyEventContextDataInterface > MakeEventContextData() const
Definition AnimNodeMessages.h:113
static FName GetStaticTypeName()
Definition AnimNodeMessages.h:87
Type & As()
Definition AnimNodeMessages.h:107
virtual TUniquePtr< const IAnimNotifyEventContextDataInterface > MakeUniqueEventContextData() const
Definition AnimNodeMessages.h:119
bool Is() const
Definition AnimNodeMessages.h:95
const Type & As() const
Definition AnimNodeMessages.h:101
virtual FName GetTypeName() const =0
Definition Class.h:1720
Definition AdvancedWidgetsModule.cpp:13
Definition AnimNodeBase.h:853
Definition AnimNodeBase.h:159
Definition AnimNodeBase.h:131
Definition UnrealTypeTraits.h:40
Definition AnimNodeMessages.h:237
FAnimNode_Base * Node
Definition AnimNodeMessages.h:238
const UScriptStruct * NodeStruct
Definition AnimNodeMessages.h:239
Definition AnimNodeMessages.h:214
bool HasMessage() const
Definition AnimNodeMessages.h:288
void ForEachMessage(TFunctionRef< EEnumerate(TGraphMessageType &)> InFunction) const
Definition AnimNodeMessages.h:245
FMessageStack(FMessageStack &&InMessageStack)=default
FMessageStack & operator=(const FMessageStack &)=delete
FMessageStack(FMessageStack &)=delete
EEnumerate
Definition AnimNodeMessages.h:230
ENGINE_API void ForEachTag(FName InTagId, TFunctionRef< EEnumerate(FNodeInfo)> InFunction) const
Definition AnimNodeMessages.cpp:171
FMessageStack & operator=(FMessageStack &&)=default
ENGINE_API void MakeEventContextData(TArray< TUniquePtr< const IAnimNotifyEventContextDataInterface > > &ContextData) const
Definition AnimNodeMessages.cpp:213
void TopMessage(TFunctionRef< void(TGraphMessageType &)> InFunction) const
Definition AnimNodeMessages.h:259
void TopMessageWeakPtr(TFunctionRef< void(TWeakPtr< TGraphMessageType > &)> InFunction)
Definition AnimNodeMessages.h:273
ENGINE_API void CopyForCachedUpdate(const FMessageStack &InStack)
Definition AnimNodeMessages.cpp:188
Definition AnimNodeMessages.h:144
ENGINE_API void PopMessage()
Definition AnimNodeMessages.cpp:67
ENGINE_API void PushMessage(const FAnimationBaseContext &InContext, TSharedRef< IGraphMessage > InMessage, FName InMessageType)
Definition AnimNodeMessages.cpp:45
FName MessageType
Definition AnimNodeMessages.h:156
FAnimationUpdateSharedContext * SharedContext
Definition AnimNodeMessages.h:153
Definition AnimNodeMessages.h:130
ENGINE_API ~FScopedGraphTag()
Definition AnimNodeMessages.cpp:34
Definition AnimNodeMessages.h:183
TOptionalScopedGraphMessage(bool bInCondition, const FAnimationBaseContext &InContext, TArgs &&... Args)
Definition AnimNodeMessages.h:185
~TOptionalScopedGraphMessage()
Definition AnimNodeMessages.h:199
Definition AnimNodeMessages.h:162
TScopedGraphMessage(const FAnimationBaseContext &InContext, TArgs &&... Args)
Definition AnimNodeMessages.h:164
~TScopedGraphMessage()
Definition AnimNodeMessages.h:174