Using ConstructorHelpers::FObjectFinder for UParticleSystem - Solution

Hi Everyone,

I was having trouble using the ConstructorHelpers::FObjectFinder for a UParticleSystem. The key component was including the


#include "Particles/ParticleSystem.h"

in my cpp file.

So in my header file I declared the UParticleSystem object.


UPROPERTY(EditDefaultsOnly, Category = "Particle Effects")
UParticleSystem* BloodSplatter;

In my cpp file at the top…


#include "Particles/ParticleSystem.h"

Then in my constructor for the projectile…


static ConstructorHelpers::FObjectFinder<UParticleSystem> BloodSplatterObject(TEXT("ParticleSystem'/Game/WeaponEffects/BloodImpact/P_blood_splash_02.P_blood_splash_02'"));
if (BloodSplatterObject.Succeeded())
{
BloodSplatter = BloodSplatterObject.Object;
}


I looked everywhere for how to do this and could not find it, so I thought I would make a post to help other folks out.

For the people reading this in the future, I’d like to mention that hard coding assets into a system is never a good idea in my book (apart from a few exceptions). Your system becomes a lot more rigid. If you do this, you can’t really have different blood splatter effects for different enemies for example. You’ll also have to watch out not to move the asset, or if you do, you’ll have to update the code manually.

A much better solution would be to just mark the variable as editable via a UPROPERTY. It will show up in the editor, where you can assign the desired particle. If you have different enemies in the game, you can now easily give them different effects as well.



// .h
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Effects")
UParticleSystem* BloodSplatterEffect;

// .cpp
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), BloodSplatterEffect, DesiredLocation);


Thank you for clarifying this information STRiFE.x.

I normally use the procedure you posted above, but also wanted to show a hardcoded example.

I had the same issue trying to use FObjectFinder for a UParticleSystem. Turns out it was a case of bad template error messages. It was just undefined. I was just missing an include. I added the include for ParticleSystem as mentioned in this thread and it then compiled just fine.

#include "Particles/ParticleSystem.h"