UAnimInstance is executing for only one character and not working for all other

I’m working on a Behaviour system and I have a Behaviour Tree Node. It’s playing specific animation montage and when Montage ended the node should be exiting with success.

The bug I’ve got: montage is playing on each character and it’s good, all working. But I have a function for when montage stops (it should end BT’s node) and it triggers only on one Instance of character. What I ended up with: I’ve got an array of same characters, all of them are stuck at one BT node and one of them is just executing his logic and everything is OK with him. Also, I’ve debugged it and I get the following info: all characters are unique, but ALL of them have the same UAnimInstance object.

My Solutions:
I’ve got two of them: first one is I’m binding function OnEndMontage to AnimInstance->OnMontageEnded


EBTNodeResult::Type UEnemyAnimationAttackBTTaskNode::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) { BTComp = &OwnerComp; AEnemyAIController *Controller = Cast<AEnemyAIController>(OwnerComp.GetAIOwner());
if (Controller != nullptr && Controller->GetEnemyRef() != nullptr) {

  [INDENT=2]BlackboardComponent = Controller->GetBlackboardComponent();

//Pick Attack Target and Set in Enemy class
AActor *AttackTarget = Cast<AActor>(BlackboardComponent->GetValueAsObject(AttackTargetKey.SelectedKeyName));
Controller->GetEnemyRef()->SetCurrentTarget(AttackTarget);

//Get Animation Instance
AnimInstance = Controller->GetEnemyRef()->GetMesh()->GetAnimInstance();
if (AnimInstance) {[/INDENT]
  [INDENT=3]
//Playing Attack montage
AnimInstance->Montage_Play(AttackMontage);
AnimInstance->OnMontageEnded.AddUniqueDynamic(this, &UEnemyAnimationAttackBTTaskNode::OnEndMontage);

return EBTNodeResult::InProgress;
}[/INDENT]
  [INDENT=2]return EBTNodeResult::Failed;
}[/INDENT]
  return EBTNodeResult::Failed;
}

And the second one is then I’m starting timer to fire up function to end this BT node.



EBTNodeResult::Type UEnemyAnimationAttackBTTaskNode::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) {
  BTComp = &OwnerComp; AEnemyAIController *Controller = Cast<AEnemyAIController>(OwnerComp.GetAIOwner());
if (Controller != nullptr && Controller->GetEnemyRef() != nullptr) {


  [INDENT=2]BlackboardComponent = Controller->GetBlackboardComponent();

//Pick Attack Target and Set in Enemy class
AActor *AttackTarget = Cast<AActor>(BlackboardComponent->GetValueAsObject(AttackTargetKey.SelectedKeyName));
Controller->GetEnemyRef()->SetCurrentTarget(AttackTarget);

//Get Animation Instance
AnimInstance = Controller->GetEnemyRef()->GetMesh()->GetAnimInstance();
if (AnimInstance) {
[/INDENT]
  [INDENT=3]//Playing Attack montage
AnimInstance->Montage_Play(AttackMontage);

const float MontageTime = AttackMontage->SequenceLength;
Controller->GetWorld()->GetTimerManager().SetTimer(MontageEndTimerHandle, this, &UEnemyAnimationAttackBTTaskNode::OnEndMontage, MontageTime, false);

return EBTNodeResult::InProgress;[/INDENT]
  [INDENT=2]}

return EBTNodeResult::Failed;[/INDENT]
  }

return EBTNodeResult::Failed;
  }

Each solution gives the same result as was described later.

How I want it to work: each character will play it’s montage via BT’s node and each character will end his BT"s node via montage end.