Using NewObject to create specific UParticleSystemComponent

Hey all, so I’m trying to spawn some particle systems, and I’m running into issues.

I’m initializing everything in the Constructor:

	ConstructorHelpers::FObjectFinder<UParticleSystem> line (TEXT("ParticleSystem'/Game/BeamLine.BeamLine'"));
	lineComponent = ObjectInitializer.CreateDefaultSubobject<UParticleSystemComponent>(this, TEXT("Line"));

But I’m unsure how to actually create instances of the UParticleSystemComponent. I’m using NewObject, but nothing is appearing. I’m not sure what I need to reference to get the initialized object I’m looking for. I need a UClass* I believe? I’m pretty sure that right now, I’m not really calling anything other than a basic Particle System Component?

	newLine = NewObject<UParticleSystemComponent>(this, UParticleSystemComponent::StaticClass());

After you spawn your component there are a number of properties you’ll want to set, but the most important is the Template variable.

You’ll need to get a pointer to the specific UParticleSystem you want to use with this component. You can create a UParticleSystem* or TSubclassOf variable and set your it within the editor; or you can dynamically load the particle system with a string reference to the blueprint class.

Once you have the UParticleSystem pointer you just assign it to the Template variable:

// .cpp
newLine = NewObject<UParticleSystemComponent>(this, UParticleSystemComponent::StaticClass());
if (newLine) {
    newLine->Template = ParticleSystem;

}

You’ll also want to attach your particle system to your owning actor or other appropriate parent.

newLine->RegisterComponentWithWorld(Owner->GetWorld());
Owner->AddOwnedComponent(newLine);
newLine->AutoAttachParent = this;

newLine->AttachToComponent(this, FAttachmentTransformRules::SnapToTargetIncludingScale, TEXT("EmitterSocketName"));

You can activate the emitter with:

newLineTemplate->ActiveSystem(true);

And you can stop emitting with:

newLine->DeactivateSystem();

Thank you! This works perfectly!

Funny enough, I asked around and someone suggested I use SpawnEmitterAtLocation() instead.

I used this:

	newLine = UGameplayStatics::SpawnEmitterAtLocation
		(GetWorld(),
			lineComponent->Template,
			A,
			FRotator::ZeroRotator,
			false
			);

and it worked perfectly. My question is now, which one should I use, and what are the pros and cons of each? I’m assuming that creating a UObject via NewObject() is different than creating an emitter?

Glad everything’s working for you. Happy to help!


Spawning it as a component creates it as a sub-object of another actor.

Spawning it as a standalone emitter creates a new actor in the scene.

Actors are more expensive than components, but have more capabilities. Which one you should use depends on what your intent is with the emitter.

For example, if you have a graphical cloud effect that you want to be able to move independently in the scene as a single entity, you want it to be an actor (although you could have a managing actor that different clouds are components of).

Whereas if you have a torch that has a mesh, a light, and a fire and smoke effect; you’d want the mesh, light and particle emitter(s) to be components of a Torch actor (which could itself exist as an actor when it is by itself in the level, and be a component of your character when it’s being held by that character).


Please mark as resolved so people with similar questions can find the answer.