Announcement

Collapse
No announcement yet.

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

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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.
    Last edited by Kaidoom15; 11-14-2017, 05:40 PM. Reason: Tags

    #2
    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.

    Code:
        DeltaSeconds = DeltaTime;
    
        if (bWantsToFire && CurrentWeapon->WeaponStats.WeaponType == EWeaponType::Weapon_Automatic) {
            StartWeaponFire();
        }
    Code:
    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;
    Code:
    void AFpShooterCharacter::StopWeaponFire()
    {
        Server_StopWeaponFire();
    }
    
    void AFpShooterCharacter::Server_StopWeaponFire_Implementation()
    {
        if (bWantsToFire)
        {
            bWantsToFire = false;
        }
    }
    Code:
    void AFpShooterCharacter::TimeBetweenShots() {
        bCanFire = true;
    }

    Comment


      #3
      Originally posted by rapiidzz901 View Post
      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.

      Code:
      DeltaSeconds = DeltaTime;
      
      if (bWantsToFire && CurrentWeapon->WeaponStats.WeaponType == EWeaponType::Weapon_Automatic) {
      StartWeaponFire();
      }
      Code:
      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;
      Code:
      void AFpShooterCharacter::StopWeaponFire()
      {
      Server_StopWeaponFire();
      }
      
      void AFpShooterCharacter::Server_StopWeaponFire_Implementation()
      {
      if (bWantsToFire)
      {
      bWantsToFire = false;
      }
      }
      Code:
      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 cheers.

      Comment


        #4
        Originally posted by SenToRious View Post

        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 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.
        Code:
         case 3:
        //Case Automatic fire
        FireTimer += DeltaSeconds;
        if (!bWantsToFire)
        {
            bWantsToFire = true;
            bCanFire = false;
            FireTimer = 0;
        }
        Last edited by rapiidzz901; 11-15-2017, 10:52 AM.

        Comment


          #5
          Originally posted by rapiidzz901 View Post

          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.
          Code:
          case 3:
          //Case Automatic fire
          FireTimer += DeltaSeconds;
          if (!bWantsToFire)
          {
          bWantsToFire = true;
          bCanFire = false;
          FireTimer = 0;
          }
          Got it, thanks a lot.

          Comment

          Working...
          X