Trying to spawn niagara system in C++, crashing

Hello!

I am relatively in-experienced in C++ in general, but have done a few courses. I am attempting to spawn a niagara system similar to the way one would spawn a normal particle system with UGameplayStatics. I have found a few reddit threads with example code, but i still get a crash for an unhandled exception because I believe I am not properly setting up my system class pointer used in the UNiagaraFunctionLibrary::SpawnSystemAtLocation() function.

I have Niagara added to my build module list. I am using this in my header :



UPROPERTY(EditAnywhere, Category = "Effects")
UNiagaraSystem* OpenEffect;

I am forward declaring the UNiagaraSystem class in my header. Then I have these includes in my cpp:



#include "NiagaraComponent.h"
#include "NiagaraFunctionLibrary.h"
#include "NiagaraSystem.h"

Then I use this in my implementation:



if (OpenEffect->IsValid())
    {
        UNiagaraComponent* NewEffect = UNiagaraFunctionLibrary::SpawnSystemAtLocation(
            this, 
            OpenEffect, 
            GetOwner()->GetActorLocation(), 
            FRotator(1),
            FVector(1),
            true,
            true,
            ENCPoolMethod::AutoRelease,
            true);
    }

I get a crash whenever that code is run in runtime that gives me the unhandled exception at a mem address and references the line with the if statement, so I am assuming that my OpenEffect pointer is somehow not resolving properly. I’m sure I am doing somethign very basically wrong, but without any examples for using Niagara in C++ in any of the documentation I am at a loss. I can succesfully add a Niagara component to my Actor class and use the component’s Activate() function to get what I need for now, but am very curious how I can go about spawning without the built in Actor component as done with the UGameplayStatics method for triggering old cascade particles.

Any pointers would be much appreciated! Thanks

P

Does this change prevent it from crashing?

if (OpenEffect && OpenEffect->IsValid())

Hi @nullterm adding that extra check does get me past that line, and if I haven’t defined the system in BP it does not crash, but if i define a system in that property then I will get the crash in the line calling the SpawnSystemAtLocation function instead. I had seen some examples using the LoadObject function in a hard set path sort of way for a system, which I will try, but my assumption was that using the UPROPERTY syntax in the header takes care of this need, but I may be misinformed. Going to keep digging, but would love to hear other thoughts!

Cheers
P

Ok, upon further investigation, my error is in the Location argument of the Spawn function, my GetOwner()->GetActorLocation() seems to have been inappropriate! Obviously very silly mistake, since i’m in my main Actor class I guess GetOwner() doesn’t actually give me an actor to then GetActorLocation with, so replacing with just GetActorLocation() resolves my crash. Sorry for the total C++ noob post, but maybe it helps someone else!

Cheers
P