I made a assault rifle in my multriplayer game with network replication, but one question is still bothering me.
My network replication is based upon multicast event. When one of the players press Fire button, I send an RPC to the server from player that want to shoot. After that, server execute multicast which run timer on every client (and server):
void UAssaultRifle::MulticastStartFire_Implementation()
{
GetWorld()->GetTimerManager().SetTimer(TimerHandle_HandleFiring, this, &UAssaultRifle::HandleFiring, WeaponDamage.TimeBetweenShoots, true, 0.f);
}
So, every client now have information that one of the players want to shoot. I also replicate WeaponState variable which determines which gun is in shooting state.
Handle_Firing
handles actual weapon fire code, mostly based on epic shooter game example:
void UAssaultRifle::HandleFiring()
{
if (CanFire())
{
// we made actuall damage from player that started fire
if (PlayerPawn && PlayerPawn->IsLocallyControlled())
{
Fire();
ConsumeAmmo();
}
// and we simulate weapon fire (to spawn effects and play sound on every client)
SimulateWeaponFire();
}
else
{
StopFire();
}
}
What I’m afraid off is that how PlayerPawn->IsLocallyControlled()
works on client and server side. As far as I understand this code should work two times in row → one time from server side and second time from client (client will send RPC in Fire() code). I dont know if I’m right and how I could debug this to understand…I tryied to display some debug messages, but that did not helped me much.