Multiplayer: Which one is the best method to handle automatic weapons

Hi,
I’m trying to figure out the best way to handle shooting an automatic weapon in a multiplayer game, currently I have two solutions:
1- Left mouse btn down: Run a timer on client side which keeps calling Server_Fire each few milliseconds and then stop the timer and call Server_StopFire when left mouse btn is up
2- Left mouse btn down: Call server one time only for Server_Fire and let the server run the timer and stop when client sends Server_StopFire

Which one should I go for or is there a better approach ?

Thanks a lot.

I wont be able to tell you which method is the best but this is how i did mine, it seems to work fine and it has no performance issues.



    DeltaSeconds = DeltaTime;

    if (bWantsToFire && CurrentWeapon->WeaponStats.WeaponType == EWeaponType::Weapon_Automatic) {
        StartWeaponFire();
    }




void AFpShooterCharacter::StartWeaponFire()
{
    Server_WeaponFire();
}

void AFpShooterCharacter::Server_WeaponFire_Implementation()
{
case 3:
                    //Case Automatic fire
                    FireTimer += DeltaSeconds;
                    if (!bWantsToFire && bCanFire) {
                        bWantsToFire = true;
                        bCanFire = false;
                        FireTimer = 0;
                        GetWorldTimerManager().SetTimer(TimerHandle_TimeBetweenShots, this, &AFpShooterCharacter::TimeBetweenShots, CurrentWeapon->WeaponStats.FireRate);
                    }
                    if (bWantsToFire)
                    {
                        if (FireTimer >= CurrentWeapon->WeaponStats.FireRate)
                        {
                            CurrentWeapon->WeaponFire();
                            FireTimer = 0;
                            PlayHandAnimations();
                        }
                    }
                    break;




void AFpShooterCharacter::StopWeaponFire()
{
    Server_StopWeaponFire();
}

void AFpShooterCharacter::Server_StopWeaponFire_Implementation()
{
    if (bWantsToFire)
    {
        bWantsToFire = false;
    }
}




void AFpShooterCharacter::TimeBetweenShots() {
    bCanFire = true;
}


Thanks for sharing your code I appreciate it, so is StartWeaponFire called repeatedly by client or only once when the fire button is held ? I understand the timer for the fire rate but I couldn’t tell whether the server is waiting for the next command to fire the next shot or automatically firing multiple times until bWantToFire is false.
Thanks again for your response :smiley: cheers.

Whilst bWantsToFire is True the event tick is constantly calling the StartWeaponFire, Once the FireTimer is >= then the set fire rate (0.1) it will fire one bullet and then repeat the process until there are no more bullets or until the button is released which will stop the tick and set bWantsToFire to false.

I dont think the timer is apart of the automatic fire but im not 100% sure as i cant check at the moment.

I think this is how it should be.



 case 3:
//Case Automatic fire
FireTimer += DeltaSeconds;
if (!bWantsToFire)
{
    bWantsToFire = true;
    bCanFire = false;
    FireTimer = 0;
}


Got it, thanks a lot.