To replicate Niagara particles to all clients from the server, you can follow these steps:
- Spawn the Niagara component on the server when needed, such as during ability activation, and make sure it's set to replicate.
void UMyReplicatedNiagaraAbility::ActivateAbility(const FGameplayAbilitySpecHandle Handle, const FGameplayAbilityActorInfo *ActorInfo, const FGameplayTagContainer *SourceTags, const FGameplayEventData *EventData)
{
Super::ActivateAbility(Handle, ActorInfo, SourceTags, EventData);
// Get the owning character for the ability
ACharacter *Character = Cast<ACharacter>(GetAvatarActorFromActorInfo());
if (Character && Character->HasAuthority()) // Only spawn on the server
{
UNiagaraSystem* NiagaraSystem = LoadObject<UNiagaraSystem>(NULL, TEXT("NiagaraSystem'/Game/Path/To/ParticleSystem.ParticleSystem'"));
// Spawn position, rotation and scale as needed
FTransform SpawnTransform = Character->GetActorTransform();
// Spawn the Niagara System at the character's location
UNiagaraComponent* NiagaraComponent = UNiagaraFunctionLibrary::SpawnSystemAtLocation(Character->GetWorld(), NiagaraSystem, SpawnTransform);
// Set NiagaraComponent to replicate
NiagaraComponent->SetIsReplicated(true);
NiagaraComponent->SetNetAddressable();
NiagaraComponent->SetOnlyOwnerSee(false);
// Attach the Niagara Component to the character or desired component
NiagaraComponent->AttachToComponent(Character->GetRootComponent(), FAttachmentTransformRules::KeepWorldTransform);
}
}
- Ensure your spawned Actor, which holds the NiagaraComponent, has its
bReplicates property set to true. If your Actor is a character or another type of Pawn, this is usually done by default when you create a new Pawn class.
ACharacter::ACharacter()
{
// Ensure the Actor replicates
bReplicates = true;
bAlwaysRelevant = true;
}
- In the case where you need to spawn the Niagara particle system dynamically, create a custom server function (RPC) to call a multicast function to spawn the particle system on all clients. Override the
GetLifetimeReplicatedProps function in your custom Actor class to include the reference to the NiagaraComponent for replication.
// NiagaraComponent is a member variable of your Actor or Character class
UPROPERTY(ReplicatedUsing = OnRep_NiagaraComponent)
UNiagaraComponent* NiagaraComponent;
// Server function to call multicast function
UFUNCTION(Server, Reliable, WithValidation, BlueprintCallable, Category = "Niagara")
void Server_SpawnNiagara();
// Multicast function to spawn particles on all connected clients
UFUNCTION(NetMulticast, Reliable, BlueprintCallable, Category = "Niagara")
void Multicast_SpawnNiagara();
// RepNotify function for the NiagaraComponent replication
UFUNCTION()
void OnRep_NiagaraComponent();
Implement the server and multicast functions in your custom Actor or Character class.
void AMyCharacter::Server_SpawnNiagara_Implementation()
{
Multicast_SpawnNiagara();
}
bool AMyCharacter::Server_SpawnNiagara_Validate()
{
return true;
}
void AMyCharacter::Multicast_SpawnNiagara_Implementation()
{
if (NiagaraSystem && NiagaraComponent)
{
NiagaraComponent->SetAsset(NiagaraSystem);
NiagaraComponent->Activate();
}
}
void AMyCharacter::OnRep_NiagaraComponent()
{
// Do any required setup or activation here when the NiagaraComponent is replicated
// Usually just setting the Niagara System asset and activating it is enough
}
Add the NiagaraComponent to the LifetimeReplicatedProps.
void AMyCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(AMyCharacter, NiagaraComponent);
}
- Call the custom server function to spawn Niagara particles on all clients, for example, during your gameplay ability or from a gameplay event.
MyCharacter->Server_SpawnNiagara();