diff --git a/Source/VAPlugin/Private/SoundSource/VAAbstractSourceComponent.cpp b/Source/VAPlugin/Private/SoundSource/VAAbstractSourceComponent.cpp index fc32c5118f2cf7593152bceed44a9ca7b4f1363b..4554a922c941381ceac360d008c37d62b488a992 100644 --- a/Source/VAPlugin/Private/SoundSource/VAAbstractSourceComponent.cpp +++ b/Source/VAPlugin/Private/SoundSource/VAAbstractSourceComponent.cpp @@ -87,6 +87,38 @@ void UVAAbstractSourceComponent::TickComponent(const float DeltaTime, const ELev } +void UVAAbstractSourceComponent::UpdateSkeletalMeshIfAttachToBone() +{ + if (MovementSetting == EMovement::AttachToBone) + { + AActor* Owner = GetOwner(); + bool bFoundSkelMesh = false; + while(!bFoundSkelMesh && Owner != nullptr) + { + TArray<UActorComponent*> MeshComponents = Owner->GetComponentsByClass(USkeletalMeshComponent::StaticClass()); + for (UActorComponent* Component : MeshComponents) + { + USkeletalMeshComponent* MeshComp = Cast<USkeletalMeshComponent>(Component); + if (MeshComp && MeshComp->DoesSocketExist(FName(*BoneName))) + { + //found the right mesh component + SkeletalMeshComponent = MeshComp; + bFoundSkelMesh = true; + break; + } + } + Owner = Owner->GetAttachParentActor(); + } + + if (SkeletalMeshComponent == nullptr) + { + FVAUtils::OpenMessageBox("[UVASourceComponent::Initialize()]: Could not find bone " + + BoneName + ", using MoveWithObject instead.", true); + MovementSetting = EMovement::MoveWithObject; + } + } +} + void UVAAbstractSourceComponent::Initialize() { if (!FVAPlugin::GetUseVA() || bInitialized) @@ -130,25 +162,7 @@ void UVAAbstractSourceComponent::Initialize() UpdateRate = ReceiverActorTmp->GetUpdateRate(); - if (MovementSetting == EMovement::AttachToBone) - { - TArray<UActorComponent*> MeshComponents = GetOwner()->GetComponentsByClass(USkeletalMeshComponent::StaticClass()); - for(UActorComponent* Component : MeshComponents) - { - USkeletalMeshComponent* MeshComp = Cast<USkeletalMeshComponent>(Component); - if(MeshComp && MeshComp->DoesSocketExist(FName(*BoneName))) - { - //found the right mesh component - SkeletalMeshComponent = MeshComp; - } - } - if (SkeletalMeshComponent == nullptr) - { - FVAUtils::OpenMessageBox("[UVASourceComponent::Initialize()]: Could not find bone " + - BoneName + ", using MoveWithObject instead.", true); - MovementSetting = EMovement::MoveWithObject; - } - } + UpdateSkeletalMeshIfAttachToBone(); SpawnPosition = GetOwner()->GetTransform().GetLocation(); @@ -375,6 +389,7 @@ bool UVAAbstractSourceComponent::SetMovementSetting(const EMovement::Type NewMov } MovementSetting = NewMovementSetting; + UpdateSkeletalMeshIfAttachToBone(); UpdatePose(); return true; diff --git a/Source/VAPlugin/Public/SoundSource/VAAbstractSourceComponent.h b/Source/VAPlugin/Public/SoundSource/VAAbstractSourceComponent.h index aeaca2e554db9a43442ffcd254f095b0faf5051d..c2c95bd4a8772b92716d2be0c83be9e6e8552a99 100644 --- a/Source/VAPlugin/Public/SoundSource/VAAbstractSourceComponent.h +++ b/Source/VAPlugin/Public/SoundSource/VAAbstractSourceComponent.h @@ -185,6 +185,7 @@ protected: // initialize Sound Source with the settings set // virtual void Initialize(); + void UpdateSkeletalMeshIfAttachToBone(); void UpdatePose(); bool SetSignalSourceID(const std::string& ID);