Hello community,
I’m trying to spawn projectiles that contain both Meshes and NiagaraSystems, I have a DataAsset that contains the relevant classes and properties for the spawning projectile which provides the variables via the parameter NewParams
(using FProjectileParams
struct).
When I spawn said projectile, the StaticMesh works fine, when I try to spawn the NiagaraSystem the function UNiagaraFunctionLibrary::SpawnSystemAttached
always returns null and no effect appears.
Please see the code below:
void AProjectileBase::SetProjectileParams(const FProjectileParams& NewParams)
{
if (IsValid(NewParams.ProjectileEffect))
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow,
FString::Printf(
TEXT("%s() Creating Niagara System, System='%s'"), *FString(__FUNCTION__),
*NewParams.ProjectileEffect->GetName()
));
// const UNiagaraComponent* NiagaraComp = UNiagaraFunctionLibrary::SpawnSystemAttached(
// NewParams.ProjectileEffect,
// MeshComponent,
// NAME_None,
// FVector(0.f),
// FRotator(0.f),
// EAttachLocation::Type::KeepRelativeOffset,
// true);
const UNiagaraComponent* NiagaraComp = UNiagaraFunctionLibrary::SpawnSystemAtLocation(
GetWorld(),
NewParams.ProjectileEffect,
MeshComponent->GetComponentLocation(),
MeshComponent->GetComponentRotation(),
NewParams.Scale,
false);
// ParticleSystemComponent = NiagaraComp;
if (IsValid(NiagaraComp))
{
// Set scale
if (!NewParams.Scale.IsZero())
{
ParticleSystemComponent->SetRelativeScale3D(NewParams.Scale);
}
}
}
if (IsValid(MeshComponent) && IsValid(NewParams.ProjectileMesh))
{
MeshComponent->SetStaticMesh(NewParams.ProjectileMesh);
}
if (IsValid(ProjectileMovementComponent))
{
ProjectileMovementComponent->InitialSpeed = NewParams.Speed;
ProjectileMovementComponent->MaxSpeed = NewParams.Speed;
ProjectileMovementComponent->ProjectileGravityScale = NewParams.Gravity;
ProjectileMovementComponent->bShouldBounce = NewParams.bShouldBounce;
ProjectileMovementComponent->Bounciness = NewParams.Bounciness;
}
}
From the commented-out section, you can see I’ve tried both SpawnSystemAttached
and SpawnSystemAtLocation
- neither return a resulting UNiagaraComponent
and without the following IsValid
the engine crashes.
The DebugMessage does in fact show the System is correct and when I pause the debugger I can see all objects are not nullptrs.
This is for a multiplayer game using GAS to trigger the ability that spawns these projectiles.
Unreal Engine 5.5.4
// Header file for ProjectileBase
{
GENERATED_BODY()
public:
AProjectileBase();
void SetProjectileParams(const FProjectileParams &NewParams);
protected:
UPROPERTY()
TObjectPtr<class UProjectileMovementComponent> ProjectileMovementComponent;
UPROPERTY()
TObjectPtr<class UNiagaraComponent> ParticleSystemComponent;
UPROPERTY()
TObjectPtr<class UStaticMeshComponent> MeshComponent;
};
// FProjectileParams struct
struct FProjectileParams
{
GENERATED_BODY()
UPROPERTY(EditDefaultsOnly, Category = "Projectile")
TSubclassOf<AProjectileBase> ProjectileClass;
UPROPERTY(EditDefaultsOnly, Category = "Projectile")
TObjectPtr<UStaticMesh> ProjectileMesh;
UPROPERTY(EditDefaultsOnly, Category = "Projectile")
TObjectPtr<UNiagaraSystem> ProjectileEffect;
UPROPERTY(EditDefaultsOnly, Category = "Projectile")
TObjectPtr<UShapeComponent> Collider;
UPROPERTY(EditDefaultsOnly, Category = "Projectile")
FVector Scale;
UPROPERTY(EditDefaultsOnly, Category = "Projectile")
float Speed = 1000.0f;
UPROPERTY(EditDefaultsOnly, Category = "Projectile")
float Gravity = .5f;
UPROPERTY(EditDefaultsOnly, Category = "Projectile")
float Range = 2000.0f;
UPROPERTY(EditDefaultsOnly, Category = "Projectile")
bool bShouldBounce = false;
UPROPERTY(EditDefaultsOnly, Category = "Projectile")
float Bounciness = 0.6f;
};
Any help would be greatly appreciated.
Thanks!