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."));
}
}