The particle effect is attached with my weapon, and weapon is replicated. Everything working as expected, except the particle spawning.
- Client use the weapon, particle spawns and visible on both client and server.
- Server use the weapon, particle only spawn on server, nothing visible on client.
FEquipment.cpp
// Sets default values
AFEquipment::AFEquipment()
{
MeshComponent = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("MeshComponent"));
EquipmentSocket = "EquipmentSocket";
bReplicates = true;
//bNetUseOwnerRelevancy = true;
SetReplicates(true);
}
// Called when the game starts or when spawned
void AFEquipment::BeginPlay()
{
Super::BeginPlay();
}
void AFEquipment::Start()
{
if (GetLocalRole() < ROLE_Authority)
{
ServerStart();
}
AActor* MyOwner = GetOwner();
if (MyOwner)
{
SpawnedParticleInstance = UGameplayStatics::SpawnEmitterAttached(ParticleEffectOnUse, MeshComponent, ParticleSpawnSocket);
APawn* OwningPlayer = Cast<APawn>(MyOwner);
StartTool(OwningPlayer);
}
}
void AFEquipment::ServerStart_Implementation()
{
Start();
}
bool AFEquipment::ServerStart_Validate()
{
return true;
}
void AFEquipment::Stop()
{
if (GetLocalRole() < ROLE_Authority)
{
ServerStop();
}
AActor* MyOwner = GetOwner();
if (MyOwner)
{
if (SpawnedParticleInstance)
{
SpawnedParticleInstance->Deactivate();
}
// Additional functionality can be implemented by calling the StopTool event in blueprint.
APawn* OwningPawn = Cast<APawn>(MyOwner);
StopTool(OwningPawn);
}
}
void AFEquipment::ServerStop_Implementation()
{
Stop();
}
bool AFEquipment::ServerStop_Validate()
{
return true;
}
FEquipment.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "FEquipment.generated.h"
class USkeletalMeshComponent;
class UParticleSystem;
class UParticleSystemComponent;
UCLASS()
class FIRESAVIOURS20_API AFEquipment : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AFEquipment();
protected:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
USkeletalMeshComponent* MeshComponent;
UPROPERTY(EditDefaultsOnly, Category = "FX")
UParticleSystem* ParticleEffectOnUse;
UPROPERTY(EditDefaultsOnly, Category = "FX")
FName ParticleSpawnSocket;
UParticleSystemComponent* SpawnedParticleInstance;
// Public properties
public:
UPROPERTY(EditDefaultsOnly, Category = "Equipments")
FName EquipmentSocket;
// Public functions
public:
// [Client] Start using tool
void Start();
// [Client] Stop using tool
void Stop();
// Protected Methods
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Blueprint implementabe for extra functionality
UFUNCTION(BlueprintImplementableEvent, Category = "Equipments")
void StartTool(APawn* OwningPawn);
// Blueprint implementable for extra functionality
UFUNCTION(BlueprintImplementableEvent, Category = "Equipments")
void StopTool(APawn* OwningPawn);
// [Server] Start using tool
UFUNCTION(Server, Reliable, WithValidation)
void ServerStart();
// [Server] Stop using tool
UFUNCTION(Server, Reliable, WithValidation)
void ServerStop();
};
Tried to search lot of solutions, but none worked for me. It did work for me using some complex method of Netmulticast and Server writing around 4 seperate functions, which i think probably was not efficient or if i am wrong somehow.
I did work on other game template, which spawns a small particle like fire at fire gun, i tried to apply that technique but it doesn’t work.
Any help or suggestion will be appreciated.