Loading...
Loading...
Use this skill when working with Niagara particle systems, VFX, effects, emitter, Niagara component, or Niagara parameter in Unreal Engine C++. Covers spawning systems, setting parameters, data interfaces (SkeletalMesh, StaticMesh, Curve, Array), OnSystemFinished delegate, and performance tuning. See references/niagara-parameter-types.md for type mapping and references/niagara-data-interfaces.md for data interface catalogue. For particle materials, see ue-materials-rendering.
npx skill4agent add quodsoler/unreal-engine-skills ue-niagara-effects.agents/ue-project-context.mdNiagaraPlugins/FX/NiagaraBuild.cs"Niagara""NiagaraCore"PublicDependencyModuleNamesUNiagaraSystem (asset: UNiagaraSystem)
└── UNiagaraEmitter[] (per-emitter asset, referenced via FNiagaraEmitterHandle)
└── UNiagaraScript[] (Spawn / Update / Event scripts; authored in Niagara editor)
└── Modules (stack of NiagaraScript nodes; not C++ classes)
Runtime instances:
UNiagaraComponent (scene component that drives one UNiagaraSystem instance)
└── FNiagaraSystemInstance (internal runtime state; access via GetSystemInstanceController())User.User.*#include "NiagaraFunctionLibrary.h"
#include "NiagaraComponent.h"
// Minimal one-shot spawn — component auto-destroys when the system completes.
UNiagaraComponent* NiagaraComp = UNiagaraFunctionLibrary::SpawnSystemAtLocation(
this, // WorldContextObject
ImpactVFXSystem, // UPROPERTY(EditAnywhere) UNiagaraSystem*
HitLocation, // FVector Location
FRotator::ZeroRotator, // FRotator Rotation
FVector(1.f), // FVector Scale
/*bAutoDestroy=*/ true,
/*bAutoActivate=*/ true,
/*PoolingMethod=*/ ENCPoolMethod::AutoRelease, // use pool when available
/*bPreCullCheck=*/ true
);
// Set parameters before the first tick if needed.
if (NiagaraComp)
{
NiagaraComp->SetVariableVec3(FName("User.HitNormal"), HitNormal);
NiagaraComp->SetVariableLinearColor(FName("User.HitColor"), DamageColor);
}// Attaches to a socket and stays active until manually deactivated.
UNiagaraComponent* TrailComp = UNiagaraFunctionLibrary::SpawnSystemAttached(
TrailVFXSystem,
WeaponMesh, // USceneComponent* AttachToComponent
FName("MuzzleSocket"), // FName AttachPointName
FVector::ZeroVector,
FRotator::ZeroRotator,
EAttachLocation::SnapToTarget,
/*bAutoDestroy=*/ false,
/*bAutoActivate=*/ true,
ENCPoolMethod::ManualRelease,
/*bPreCullCheck=*/ true
);// In header:
UPROPERTY(VisibleAnywhere)
TObjectPtr<UNiagaraComponent> EngineTrailVFX;
// In constructor:
EngineTrailVFX = CreateDefaultSubobject<UNiagaraComponent>(TEXT("EngineTrailVFX"));
EngineTrailVFX->SetupAttachment(GetRootComponent());
EngineTrailVFX->SetAutoActivate(false); // start inactive; activate via gameplay
// In gameplay code:
EngineTrailVFX->SetAsset(EngineTrailSystem); // swap asset without destroying component
EngineTrailVFX->Activate(/*bReset=*/ true);NiagaraComp->Activate(/*bReset=*/ false); // activate; resume if paused
NiagaraComp->Activate(/*bReset=*/ true); // activate with full reset
NiagaraComp->Deactivate(); // stop spawning, let particles drain
NiagaraComp->DeactivateImmediate(); // kill all particles immediately
NiagaraComp->ResetSystem(); // restart from time 0
NiagaraComp->ReinitializeSystem(); // full re-init + restart (expensive; prefer ResetSystem)
NiagaraComp->SetPaused(true); // pause simulation
NiagaraComp->SetAutoDestroy(true); // destroy component when system finishesFNameUser.// Scalar types
NiagaraComp->SetVariableFloat(FName("User.DamageAmount"), 150.f);
NiagaraComp->SetVariableInt(FName("User.ProjectileCount"), 12);
NiagaraComp->SetVariableBool(FName("User.bIsCritical"), bIsCriticalHit);
// Vector types
NiagaraComp->SetVariableVec2(FName("User.UVOffset"), FVector2D(0.5, 0.25));
NiagaraComp->SetVariableVec3(FName("User.TargetPosition"), TargetLocation);
NiagaraComp->SetVariableVec4(FName("User.CustomData"), FVector4(1, 0.5, 0, 1));
NiagaraComp->SetVariableLinearColor(FName("User.TintColor"), FLinearColor::Red);
NiagaraComp->SetVariableQuat(FName("User.Orientation"), GetActorQuat());
// Object / Actor references (binds DI override)
NiagaraComp->SetVariableObject(FName("User.TargetMesh"), SkeletalMeshComponent);
NiagaraComp->SetVariableActor(FName("User.SourceActor"), this);
// Position (LWC-aware alias for vec3)
NiagaraComp->SetVariablePosition(FName("User.WorldOrigin"), WorldSpaceOrigin);
// Material / Texture overrides
NiagaraComp->SetVariableMaterial(FName("User.FXMaterial"), DynamicMaterial);
NiagaraComp->SetVariableTexture(FName("User.FlowMap"), FlowTexture);
// Read a float parameter back (returns bIsValid=false when name not found)
bool bIsValid = false;
float CurrentValue = NiagaraComp->GetVariableFloat(FName("User.EmitRate"), bIsValid);// Old FString signatures still work but are slower due to FName conversion.
NiagaraComp->SetNiagaraVariableFloat(TEXT("User.SpeedScale"), 2.f);
NiagaraComp->SetNiagaraVariableVec3(TEXT("User.ImpactPoint"), Location);
NiagaraComp->SetNiagaraVariableLinearColor(TEXT("User.Color"), FLinearColor::Blue);| Namespace prefix | Settable from C++ | Description |
|---|---|---|
| Yes | User-exposed; main runtime override |
| No (read-only) | System-level built-ins (Age, DeltaTime, etc.) |
| No (internal) | Per-emitter variables |
| No (internal) | Per-particle variables |
references/niagara-parameter-types.mdUObjectUser.*SetVariableObject#include "NiagaraFunctionLibrary.h"
// Override the "User.SourceMesh" skeletal mesh DI on a running component.
UNiagaraFunctionLibrary::OverrideSystemUserVariableSkeletalMeshComponent(
NiagaraComp,
TEXT("User.SourceMesh"), // must match the DI's User parameter name in the asset
GetMesh() // USkeletalMeshComponent*
);
// Restrict which bones spawn from (destructive — modifies the DI instance data).
UNiagaraFunctionLibrary::SetSkeletalMeshDataInterfaceFilteredBones(
NiagaraComp,
TEXT("User.SourceMesh"),
{ FName("hand_l"), FName("hand_r") }
);
// Restrict which sampling regions to use.
UNiagaraFunctionLibrary::SetSkeletalMeshDataInterfaceSamplingRegions(
NiagaraComp,
TEXT("User.SourceMesh"),
{ FName("HeadRegion") }
);// Override via component reference.
UNiagaraFunctionLibrary::OverrideSystemUserVariableStaticMeshComponent(
NiagaraComp,
TEXT("User.ScatterMesh"),
StaticMeshComp
);
// Override with a raw UStaticMesh asset pointer.
UNiagaraFunctionLibrary::OverrideSystemUserVariableStaticMesh(
NiagaraComp,
TEXT("User.ScatterMesh"),
LoadedStaticMesh
);#include "NiagaraDataInterfaceArrayFunctionLibrary.h"
// Push a new float array into the effect (e.g., damage heatmap values).
TArray<float> HeatValues = ComputeHeatValues();
UNiagaraDataInterfaceArrayFunctionLibrary::SetNiagaraArrayFloat(
NiagaraComp, FName("User.HeatData"), HeatValues
);
// Update a single element without replacing the whole array.
UNiagaraDataInterfaceArrayFunctionLibrary::SetNiagaraArrayFloatValue(
NiagaraComp, FName("User.HeatData"), /*Index=*/ 5, /*Value=*/ 0.9f, /*bSizeToFit=*/ false
);
// Other strongly-typed array setters available:
// SetNiagaraArrayVector, SetNiagaraArrayVector4, SetNiagaraArrayColor,
// SetNiagaraArrayQuat, SetNiagaraArrayInt32, SetNiagaraArrayBool, etc.// Retrieve the actual DI UObject to mutate its properties directly.
// Template variant resolves the cast automatically.
UNiagaraDataInterfaceCurve* CurveDI =
UNiagaraFunctionLibrary::GetDataInterface<UNiagaraDataInterfaceCurve>(
NiagaraComp, FName("User.SpeedCurve")
);
if (CurveDI)
{
// Mutate curve keyframes at runtime (rebuilds LUT internally).
CurveDI->Curve.AddKey(0.f, 0.f);
CurveDI->Curve.AddKey(1.f, 500.f);
// UpdateLUT() is WITH_EDITORONLY_DATA — only call in editor builds.
#if WITH_EDITORONLY_DATA
CurveDI->UpdateLUT();
#endif
}
// Non-template variant when the DI class is only known at runtime.
UNiagaraDataInterface* GenericDI =
UNiagaraFunctionLibrary::GetDataInterface(
UNiagaraDataInterfaceStaticMesh::StaticClass(),
NiagaraComp,
FName("User.ImpactMesh")
);references/niagara-data-interfaces.mdUNiagaraDataInterfaceGetFunctions()GetVMExternalFunction()ProvidePerInstanceDataForRenderThread()StartupModule// Bind a C++ delegate to fire when the Niagara system finishes all particles.
// FOnNiagaraSystemFinished is DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(, UNiagaraComponent*)
NiagaraComp->OnSystemFinished.AddDynamic(this, &UMyComponent::OnVFXFinished);
// The callback signature:
UFUNCTION()
void UMyComponent::OnVFXFinished(UNiagaraComponent* FinishedComponent)
{
// Called on game thread when every particle has expired and the system is done.
FinishedComponent->DestroyComponent();
// or return it to pool, notify gameplay, etc.
}
// Unbind when the owner is destroyed to avoid stale delegates.
NiagaraComp->OnSystemFinished.RemoveDynamic(this, &UMyComponent::OnVFXFinished);ENCPoolMethodAutoReleasebAutoDestroy=trueManualReleaseReleaseToPool()NonebAutoDestroy=true// AutoRelease: most common for one-shots (explosions, impacts).
UNiagaraComponent* Comp = UNiagaraFunctionLibrary::SpawnSystemAtLocation(
this, ExplosionSystem, Location, FRotator::ZeroRotator,
FVector(1.f), /*bAutoDestroy=*/true, /*bAutoActivate=*/true,
ENCPoolMethod::AutoRelease
);
// ManualRelease: for effects you pause/resume (e.g., a beam while a button is held).
// Reclaim by calling ReleaseToPool() when done.
TrailComp->ReleaseToPool();
// Prime the pool before a gameplay-critical moment via FNiagaraWorldManager.
if (FNiagaraWorldManager* NiagaraWorldMan = FNiagaraWorldManager::Get(GetWorld()))
{
NiagaraWorldMan->GetComponentPool()->PrimePool(ExplosionSystem, GetWorld());
}UNiagaraSystemFX.NiagaraComponentPool.EnableFX.NiagaraComponentPool.KillUnusedTime// Allow the scalability manager to cull this component based on distance and budget.
NiagaraComp->SetAllowScalability(true); // default true; disable for gameplay-critical VFX
// Adjust tick behavior to avoid unnecessary dependency resolution.
// ENiagaraTickBehavior::UsePrereqs — default; ticks after its prerequisites
// ENiagaraTickBehavior::ForceTickFirst — useful for VFX that leads all tick groups
NiagaraComp->SetTickBehavior(ENiagaraTickBehavior::UsePrereqs);UNiagaraEffectTypeUNiagaraSystemFixedTickDeltaNiagaraComp->SetDesiredAge(2.5f); // age in seconds
NiagaraComp->SeekToDesiredAge(2.5f); // perform seek immediately (skips simulation steps)
// FFXSystemSpawnParameters (used by SpawnSystemAtLocationWithParams) also exposes DesiredAge.SpawnSystemAtLocationnullptr!IsRunningDedicatedServer()NiagaraComp->SetVariableBool(FName("User.bJustDied"), true);
// Niagara reads this flag on the next spawn script tick and fires the burst.
// There is no C++ API to inject raw Niagara events directly — use User parameters as the bridge.PublicDependencyModuleNames.AddRange(new string[]
{
"Core",
"CoreUObject",
"Engine",
"Niagara", // UNiagaraComponent, UNiagaraFunctionLibrary, UNiagaraSystem
"NiagaraCore", // UNiagaraDataInterface base (NiagaraCore module)
});// BAD: Creates a new UNiagaraComponent each frame. Destroys performance.
void AMyActor::Tick(float DeltaTime)
{
UNiagaraFunctionLibrary::SpawnSystemAtLocation(this, TrailFX, GetActorLocation());
}
// GOOD: Create the component once in BeginPlay or constructor; activate/deactivate as needed.// BAD: "Emitter.Speed" is an internal emitter parameter; cannot be set from C++.
NiagaraComp->SetVariableFloat(FName("Emitter.Speed"), 300.f);
// GOOD: The Niagara author must expose the parameter under "User.*".
NiagaraComp->SetVariableFloat(FName("User.Speed"), 300.f);// BAD: Calling SetVariableVec3 on a parameter that is typed as "Color" in Niagara.
// Silently fails — no runtime error, parameter is just not updated.
NiagaraComp->SetVariableVec3(FName("User.TintColor"), FVector(1, 0, 0));
// GOOD: Match the C++ call to the Niagara parameter type.
NiagaraComp->SetVariableLinearColor(FName("User.TintColor"), FLinearColor::Red);// The component is valid but the system instance may be inactive. Check before setting.
if (NiagaraComp && NiagaraComp->IsActive())
{
NiagaraComp->SetVariableFloat(FName("User.Intensity"), NewIntensity);
}UNiagaraComponent* Comp = UNiagaraFunctionLibrary::SpawnSystemAtLocation(...);
// Comp can be nullptr on dedicated server or when bPreCullCheck rejects the spawn.
if (Comp)
{
Comp->SetVariableFloat(FName("User.Scale"), 2.f);
}// BAD: OnSystemFinished fires after owner is garbage collected → crash.
// GOOD: Always RemoveDynamic in BeginDestroy or EndPlay.
void AMyActor::EndPlay(const EEndPlayReason::Type Reason)
{
if (NiagaraComp)
{
NiagaraComp->OnSystemFinished.RemoveDynamic(this, &AMyActor::OnVFXFinished);
}
Super::EndPlay(Reason);
}ue-actor-component-architectureue-materials-renderingue-cpp-foundations