Hi,all.
I’m working on a multiplayer game, and i want to spawn a emitter and a sound when weapon fire.
SpawnEmitterAttached and SpawnSoundAttached spawned instance can only be played and displayed on the server.
Here is my code:.
bool AWeaponBase::SimulateWeaponFire_Validate()
{
return true;
}
void AWeaponBase::SimulateWeaponFire_Implementation()
{
if (CurrentWeaponState != EWeaponState::Firing)
{
return;
}
if (MuzzleFX && MuzzlePSC == nullptr && MyPawn != nullptr)
{
MuzzlePSC = UGameplayStatics::SpawnEmitterAttached(MuzzleFX, WeaponMesh, MuzzleAttachPoint);
}
if (FireAnim)
{
PlayWeaponAnimation(FireAnim);
}
if (FireLoopSound && FireAC == nullptr)
{
FireAC = PlayWeaponSound(FireLoopSound);
}
if (FireCameraShake != nullptr)
{
SimulateFireShake();
}
if (FireForceFeedback != nullptr)
{
SimulateFireFeedback();
}
}
UAudioComponent* AWeaponBase::PlayWeaponSound(USoundCue* Sound)
{
UAudioComponent* AC = nullptr;
if (Sound && MyPawn)
{
AC = UGameplayStatics::SpawnSoundAttached(Sound, this->GetRootComponent(), MuzzleAttachPoint);
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Yellow, FString::Printf(TEXT("Spawned")));
if (Role != ROLE_Authority)
{
PlayWeaponSound_Server(Sound);
}
}
return AC;
}
bool AWeaponBase::PlayWeaponSound_Server_Validate(USoundCue* Sound)
{
return true;
}
void AWeaponBase::PlayWeaponSound_Server_Implementation(USoundCue* Sound)
{
UGameplayStatics::SpawnSoundAttached(Sound, MyPawn->GetRootComponent());
}
Can anyone help me?Plz.