How to acquire the reference to a Niagara system in a Blueprint via C++?

I have a C++ Class that is intended to reference an instance of a Niagara system inside a Blueprint. The idea here is we have a tractor beam visual effect inside the Blueprint, and I want to Activate and Deactivate it via C++. Where I am getting stuck however is how to reference the instance of the Niagara system inside the Blueprint.

In my C++ code I have this in the header:

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UFO")
	class UNiagaraSystem* BeamParticles;

And this works great - however it does not let me apply the reference of the the Niagara system, instead it only lets me apply the source Niagara system.

What do I need to do to be able to reference the instance of the system inside the Blueprint?

Thanks in advance!

When you instantiate a system, you use a component for the system.
So, create a default subobject and attach it, that’s the particle system component.

Could you elaborate on this, especially how to do this in C++? I’m not even seeing a field show up in the Blueprint for my BeamParticles to apply that component to. (I’m thinking there should be like an empty slot that I can drag/drop the Niagara system I placed in the Blueprint to).

Thanks!

You need to add a UNiagaraComonent UPROPERTY to your object, and create the default subobject for it in the constructor, and attach the transform appropriately (as for any kind of component, like a static mesh or whatever.)

This is super helpful, thank you! I think I’m almost there now…

I have this in the TractorBeam.h:

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UFO")
	class UNiagaraComponent* BeamParticles;

And in the TractorBeam.cpp constructor I have this:

ATractorBeam::ATractorBeam()
{
	PrimaryActorTick.bCanEverTick = true;
	BeamParticles = CreateDefaultSubobject<UNiagaraComponent>("TractorBeamParticles");
}

That all compiles fine. The only thing I’m still not understanding is where to attach the transform within the Blueprint, as I don’t see any fields labeled TractorBeamParticles in the Blueprint that derives from the TractorBeam C++ class.

Shouldn’t the name be "BeamParticles" ?
Anyway, you should see the component in the scene hierarchy for the actor, assuming you attach it correctly.
Once you see it, you should be able to configure it just like you’d configure a StaticMesh or whatever.
If that is all the code you wrote then you haven’t yet attached it correctly.

You probably want some other component in between, too, if you’re going to be building a complex weapon out of it.

	UPROPERTY(EditAnywhere)
		USceneComponent* SceneRoot;
	UPROPERTY(EditAnywhere)
		UNiagaraComponent* BeamParticles;
// Sets default values
AMyActor::AMyActor()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	SceneRoot = CreateDefaultSubobject<USceneComponent>("SceneRoot");
	SceneRoot->SetupAttachment(this->GetRootComponent());
	BeamParticles = CreateDefaultSubobject<UNiagaraComponent>("BeamParticles");
	BeamParticles->SetupAttachment(SceneRoot);
}

1 Like

AH! This was exactly what I needed. I’m coming from Unity and I think I was looking at this in a very Unity fashion where I had thought we would create a slot, then I’d apply a previously placed object to it. But now I see the Unreal way of doing things is you create not just a slot but an entire object, and then tell that object what to populate it with.

This is now working exactly as I was hoping and I learned a good bit doing it. Thank you very much for your detailed assistance!

1 Like