x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Need help understanding networking code(Shooter Demo)

I came across a code pattern many times while going through the shooter project. But I am not sure yet what exactly does it do.

 void AShooterWeapon::StartFire()
 {
     if (Role < ROLE_Authority)
     {
         ServerStartFire();
     }
 
     if (!bWantsToFire)
     {
         bWantsToFire = true;
         DetermineWeaponState();
     }
 }
 
 
 
 bool AShooterWeapon::ServerStartFire_Validate()
 {
     return true;
 }
 
 void AShooterWeapon::ServerStartFire_Implementation()
 {
     StartFire();
 }

Basically a as much as I could understand, this functions verifies from the server and calls a server version of that function.

But after a chain of functions, it calls this

 void AShooterWeapon::HandleFiring()
 {
     if ((CurrentAmmoInClip > 0 || HasInfiniteClip() || HasInfiniteAmmo()) && CanFire())
     {
         if (GetNetMode() != NM_DedicatedServer)
         {
             SimulateWeaponFire();
         }
 
         if (MyPawn && MyPawn->IsLocallyControlled())
         {
             FireWeapon();
 
             UseAmmo();
             
             // update firing FX on remote clients if function was called on server
             BurstCounter++;
         }
     }
     else if (CanReload())
     {
         StartReload();
     }
     else if (MyPawn && MyPawn->IsLocallyControlled())
     {
         if (GetCurrentAmmo() == 0 && !bRefiring)
         {
             PlayWeaponSound(OutOfAmmoSound);
             AShooterPlayerController* MyPC = Cast<AShooterPlayerController>(MyPawn->Controller);
             AShooterHUD* MyHUD = MyPC ? Cast<AShooterHUD>(MyPC->GetHUD()) : NULL;
             if (MyHUD)
             {
                 MyHUD->NotifyOutOfAmmo();
             }
         }
         
         // stop weapon fire FX, but stay in Firing state
         if (BurstCounter > 0)
         {
             OnBurstFinished();
         }
     }
 
     if (MyPawn && MyPawn->IsLocallyControlled())
     {
         // local client will notify server
         if (Role < ROLE_Authority)
         {
             ServerHandleFiring();
         }
 
         // reload after firing last round
         if (CurrentAmmoInClip <= 0 && CanReload())
         {
             StartReload();
         }
 
         // setup refire timer
         bRefiring = (CurrentState == EWeaponState::Firing && WeaponConfig.TimeBetweenShots > 0.0f);
         if (bRefiring)
         {
             GetWorldTimerManager().SetTimer(this, &AShooterWeapon::HandleFiring, WeaponConfig.TimeBetweenShots, false);
         }
     }
 
     LastFireTime = GetWorld()->GetTimeSeconds();
 }
 
 bool AShooterWeapon::ServerHandleFiring_Validate()
 {
     return true;
 }
 
 void AShooterWeapon::ServerHandleFiring_Implementation()
 {
     const bool bShouldUpdateAmmo = (CurrentAmmoInClip > 0 && CanFire());
 
     HandleFiring();
 
     if (bShouldUpdateAmmo)
     {
         // update ammo
         UseAmmo();
 
         // update firing FX on remote clients
         BurstCounter++;
     }
 }

I am really confused about what exactly does this do.

Whay does the ServerHandleFiring_Implementation() change the ammo and burst counter?

Product Version: Not Selected
Tags:
more ▼

asked Dec 17 '14 at 01:59 PM in C++ Programming

avatar image

envenger
423 77 117 146

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

Hi Envenger,

 1.     if (Role < ROLE_Authority)
 2.    {
 3.    ServerStartFire();
 4.    }

Role==ROLE_Autority means that this code executing on server, otherwise on client. Therefore, when you press fire (on client) you execute StartFire function on client that call ServerStartFire. ServerStartFire initiate remote procedure calling of ServerStartFire_Implementation on server part. If you want to spawn actor on all clients you should spawn it on server. (If you spawn on client, you will have instance of this actor only on this client). That why all projectiles spawns on server. What about HandleFiring it is function to start all firing effects (animation, muzzles, etc.)

Replication is a very large question and it cannot been covered in answer. If you would like to know a little more about replication, please take a look at the post below. https://docs.unrealengine.com/latest/INT/Gameplay/Networking/Replication/index.html https://docs.unrealengine.com/latest/INT/Gameplay/Networking/Replication/index.html

Hope it helps!

more ▼

answered Dec 17 '14 at 05:06 PM

avatar image

Gleb.Bulgakov
1.8k 76 19 143

avatar image envenger Dec 17 '14 at 05:43 PM

Thank you.. After studying the shooter codes some more, I found out why it is used like that.

But I have a small question to ask.

If a normal function is called on the server(not multicast or anything) will that function be also called on the clients automatically?

avatar image Gleb.Bulgakov Dec 17 '14 at 06:29 PM

No, it is not call.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question