How to spawn Niagara system in c++ inside an actor?

Im trying to spawn a niagara system in cpp. I didnt find many examples online, but so far i could put this together:
It compiles but when i try it the arrows are nowhere to be seen. What am i doing wrong?

The niagara system works if i just throw it in the world in the editor
But not if im spawning it from my actor.

If needed i can post videos.

\h:

#pragma once
#include "CoreMinimal.h"
#include "MainBat.h"
#include "Components/SceneComponent.h"
#include "NiagaraFunctionLibrary.h"
#include "NiagaraComponent.h"
#include "ArcherBat.generated.h"

UCLASS()
class K2K_TEST_CPP_API AArcherBat : public AMainBat
{
    GENERATED_BODY()
    public:
    AArcherBat();
    virtual void BeginPlay() override;
    class UNiagaraSystem* ArrowsNiagara;
    USceneComponent* NiagaraSceneComp;
    class UNiagaraComponent* NiagaraComp;
};

\cpp:

#include "ArcherBat.h"

AArcherBat::AArcherBat()
{
}

void AArcherBat::BeginPlay()
{
    Super::BeginPlay();
FString NiagaraPath = "/Game/TopDownBP/Archer/niagara_arrow/NS_Arrow.NS_Arrow";
ArrowsNiagara = Cast<UNiagaraSystem>(StaticLoadObject(UNiagaraSystem::StaticClass(), nullptr, *NiagaraPath));
NiagaraComp = UNiagaraFunctionLibrary::SpawnSystemAttached(ArrowsNiagara, NiagaraSceneComp, NAME_None, FVector(0.f,0.f,400.f), FRotator(0.f), EAttachLocation::Type::KeepRelativeOffset, true);

}

Found the issue.
Im attaching it to a USceneComponent that was not spawned in the actor.
So must either spawn the USceneComponent ```
NiagaraSceneComp

Or attach it to the rootcomponent of the actor you are spawning it in.

So in this line:
```NiagaraComp = UNiagaraFunctionLibrary::SpawnSystemAttached(ArrowsNiagara, NiagaraSceneComp, NAME_None, FVector(0.f,0.f,400.f), FRotator(0.f), 

Do instead:
NiagaraComp = UNiagaraFunctionLibrary::SpawnSystemAttached(ArrowsNiagara, this->RootComponent, NAME_None, FVector(0.f,0.f,400.f), FRotator(0.f),

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.