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 system works if i just throw it in the world
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);

}

Post the video.

1 Like

i replied with the solution. But its gone.
The issue is with the SceneComponent im attaching it to. Is not initialized.
So if attaching it to the root component of the actor it will work.
Thanks though.

1 Like

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