最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Unreal Engine and Niagara component simulation in a Level - Stack Overflow

programmeradmin1浏览0评论

So I am trying to render a Niagara Component in a world only when a prompt a specific thing which in my case is a diagnosis - it registers in UE_Logs but doesn't show up, it only appears after the level has ended.

I think the problem might be with this line of the code:

UWorld\* World = GEngine-\>GetWorldContexts()\[0\].World();

And if I use diffrent ways to load the world the Niagara doesn't work.

Might suggest that I am not using a Blueprint to run the Niagara rather than accessing it directly and loading it in the world.

Any suggestions?

I have already tried loading diffrent worlds and different ticking systems, manually prompting Niagara trough C++.

Down below is my code of the class how far I have made, I have made some changes to see if the code would run if if I would specifically run it in a different world, but still the result was the same.

void UCPP_SimulationManager::ChangeDiagnosis(EDiagnosisType SelectedDiagnosis)
{
    UE_LOG(LogTemp, Warning, TEXT("ChangeDiagnosis() called with DiagnosisType: %d"), (int32)SelectedDiagnosis);

    SelectedDiagnosisType = SelectedDiagnosis;

    // Trigger the ChangeDiagnosisEventDelegate
    UCPP_Diagnosis& SelectedDiagnosisObj = DiagnosisRegistery.GetDiagnosisByType(SelectedDiagnosis);
    ChangeDiagnosisEventDelegate.Broadcast(SelectedDiagnosisObj);

    // ✅ Trigger the Sweat Effect if it's Cardiogenic Shock
    if (SelectedDiagnosisType == EDiagnosisType::CardiogenicShock)
    {
        UE_LOG(LogTemp, Warning, TEXT("Cardiogenic Shock selected! Triggering SweatEffect."));

        // Find your BP_Bones actor
        UWorld* World = GEngine->GetWorldContexts()[0].World();
        if (!World)
        {
            UE_LOG(LogTemp, Error, TEXT("World not found!"));
            return;
        }

        AActor* TargetActor = nullptr;

        for (TActorIterator<AActor> It(World); It; ++It)
        {
            if (It->GetName().Contains("BP_Bones"))  // This will work even if named BP_Bones_1, BP_Bones_2, etc.
            {
                TargetActor = *It;
                break;
            }
        }

        if (TargetActor)
        {
            UE_LOG(LogTemp, Warning, TEXT("BP_Bones found successfully! Triggering SweatEffect."));

            // Finding the correct Skeletal Mesh Component with 'SKM_bones-joined'
            USkeletalMeshComponent* MatchingMesh = nullptr;
            TArray<USkeletalMeshComponent*> Components;
            TargetActor->GetComponents<USkeletalMeshComponent>(Components);

            for (USkeletalMeshComponent* Component : Components)
            {
                if (Component && Component->SkeletalMesh && 
                    Component->SkeletalMesh->GetName().Contains("SKM_bones-joined"))
                {
                    MatchingMesh = Component;
                    break;
                }
            }

            if (MatchingMesh)
            {
                UE_LOG(LogTemp, Warning, TEXT("Matching 'SKM_bones-joined' mesh found! Triggering SweatEffect."));
                DiagnosisRegistery.TriggerSweatEffect(TargetActor);  // Trigger effect now that we found the correct mesh

                // ✅ On-Screen Debug Message
                if (GEngine)
                {
                    GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Green, TEXT("SweatEffect Triggered Successfully!"));
                }
            }
            else
            {
                UE_LOG(LogTemp, Error, TEXT("SKM_bones-joined mesh not found in BP_Bones!"));
            }
        }
        else
        {
            UE_LOG(LogTemp, Error, TEXT("BP_Bones not found in the scene! Make sure it's correctly placed and named."));
        }
    }
    else
    {
        UE_LOG(LogTemp, Warning, TEXT("Non-Cardiogenic Diagnosis selected. Niagara Effect will not be triggered."));
    }
}
发布评论

评论列表(0)

  1. 暂无评论