Hi,
I’m currently looking for a reliable way to execute code after the link layer process has fully completed in Unreal Engine. Specifically, I want to run some logic after calling:
void UAnimInstance::LinkAnimClassLayers(TSubclassOf<UAnimInstance> InClass);
However, even when I set up a custom callback that gets triggered in the same frame after the LinkAnimClassLayers call, it doesn’t consistently guarantee that the linked layer has fully finished linking with the main UAnimInstance. (I’m suspecting that the layer linking process maybe happens on a different thread?)
While investigating, I came across the following function in FAnimNode_LinkedAnimLayer
:FSimpleMulticastDelegate& OnInstanceChanged() { return OnInstanceChangedEvent; }
Unfortunately, this appears to be marked as EDITOR_ONLY
, which raises the question: Is there a specific reason this delegate is editor-only? Or am I perhaps overengineering this and there’s a more straightforward or intended approach to reliably detect when the linking process has fully completed?
Any insight or guidance would be greatly appreciated.
Kind regards,
Van Brabant Maxim
Hi, sorry for the delay following up on this. We had a number of people out of office last week so that’s slowed things down.
I think the best option here is likely going to depend on exactly what your use case is. But there should be some events that you can hook into. The primary one would just be the blueprint or native BeginPlay events on the linked anim instance. They should be invoked, during the linking process, once the new anim instance has been instantiated and UAnimInstance::InitializeAnimation has been called. There’s also the BlueprintLinkedAnimationLayersInitialized event which is called at the end of InitializeAnimation, once any subgraphs have been initialized. But I see that we don’t have a native equivalent to that.
In terms of the delay in linking, I’m not aware that any of this code runs asynchronously. But it is possible that initialization of the linked instances can be deferred until some point in the next frame (when the regular animation update runs). So it might be that you’re running into that. But I wouldn’t expect that to happen when you call LinkAnimClassLayers, nothing should be deferred at that point.
In terms of OnInstanceChangedEvent, that’s editor only because it was added to fix an editor specific problem. The instance that a linked layer node uses can be dynamically assigned, and we needed to have the OnInstanceChangedEvent in order to update the UI when debugging. If you’re curious, it was added in CL 9133270. I think that NativeBeginPlay should probably give you what you need, so something like OnInstanceChangedEvent shouldn’t be needed hopefully. But having said that, they’re not quite the same since FAnimNode_LinkedAnimLayer::SetLinkedLayerInstance is called after NativeBeginPlay.
Anyway, let me know if any of those suggestions work. If not, it’d be good to know exactly what the use case is and I can do more digging to find other options.