LinkedAnimGraphs within a LinkedAnimClassLayer not being removed when calling UnlinkAnimClassLayer

(On 4.27)

Every time my characters switch weapons, they unlink the old weapon’s set of AnimClassLayers and link in the new weapon’s set of AnimClassLayers, via UnlinkAnimClassLayers(Class) and LinkAnimClassLayers(Class) respectively.

However I found that LinkedAnimGraphs that some of those AnimClassLayers might utilize are not successfully unlinked, and linger on the USkeletalMeshComponent, and are there to stay.

After switching weapons a bunch of times, the list logged by this code

	if (const USkeletalMeshComponent* SkelMeshComp = GetSkelMeshComponent())
	{
		for (auto It = SkelMeshComp->GetLinkedAnimInstances().CreateConstIterator(); It; ++It)
		{
			const UAnimInstance* LinkedAnimInstance = *It;
			UE_LOG(LogTemp, Display, TEXT("Linked anim instance is: %s"), *GetNameSafe(LinkedAnimInstance))
		}
	}

… becomes really big, bringing down my FPS, as shown by UnrealInsights:

Is linking and unlinking anim class layers when switching weapons no longer a valid usecase? Or is that I’m not supposed to be using LinkedAnimGraphs within those class layers? And if this is a valid usecase of both, what’s the best way to clear out those lingering LinkedAnimGraphs via C++?

Thanks for the help!

For future seekers, this bit of code (being called from the master AnimInstance) seems to have solved the issue

	// Linked instances like to linger in our skeletal mesh unless we destroy them manually
	if (USkeletalMeshComponent* SkelMeshComp = GetSkelMeshComponent())
	{
		// ... so do just that (ClearAnimScriptInstance will internally call ResetLinkedAnimInstances which is what we're really after...)
		SkelMeshComp->ClearAnimScriptInstance();

		// ...but this nullifies ourselves as the master anim instance, so undo that side effect
		SkelMeshComp->SetAnimInstanceClass(GetClass());
		SkelMeshComp->AnimScriptInstance = this;
	}

It’s not ideal, it feels wrong (especially considering the fact that ResetLinkedAnimInstances is a private member of USkeletalMeshComponent and we’re hacking our way into calling it) but it oddly seems to be the only way to work around this bug?

2 Likes

I’m looking for a solution for this too. Did you ever find a better solution to this?

Anyone with an answer to this? Any suggestions would be really appreciated.