Download

[Networking] Firing a projectile

I am putting my hands on networking and I need to understand some concepts.
I have a weapon that fires projectiles, and I need these projectiles to be spawned on every client.

Suppose I have this situation, the server, client1 and client2. client1 fires.

The weapon class has:



void Fire();

UFUNCTION(reliable, server, WithValidation)
void ServerFire(...);


client1 calls Fire which calculates where to spawn the projectile and the direction where it will fly. Then it calls ServerFire() to spawn this projectile on the server.



void Fire()
{
     ... calculations ...
     
     ServerFire(...params...);
}

void ServerFire_Implementation()
{
     ... actual spawning ...
}


At this point what happened?
I suppose we have this situation:
server -> has spawned the projectile
client1 -> hasn’t (he just executed Fire(), the spawning has happened only on the server)
client2 -> hasn’t (not involved at all)

Is this true?
If it is, how do I replicate the spawning on all the clients connected?

First off I should mention that I am not all to familiar with UE4 and am still very much a beginner in that regard, though I do have game development experience and experience with C++.

Now I assume that UE4 follows the same way most games handle networking. By that I mean each client is basically responsible for 1. Getting input from the player and 2. Giving output to the player (This includes drawing the gameworld, producing sounds and any other feedback). There is other things but that is the main things the client handles. Pretty much everything else happens on the server. The server will contain the gameworld and send the information needed to complete 2. (Rendering, audio, ect) to each client.

So in your case of client1 firing a projectile it would probably go something like this.

Lets say that both client1 and client2 are facing each other and are about 10 meters away from each other. Client1 presses the left mouse button, the client recognizes that this button is mapped to firing projectiles. The client then sends a request to the server that client1 wishes to fire a projectile, the server will take care of checking whether client1 can fire a projectile, if it can spawning the projectile, adding it to the game world, ect ect. So let’s say the server decides it is possible for client1 to fire a projectile and has spawned and added it to the gameworld and everything else. On the next frame of the game when the server sends out the information to each client it will send the information to draw the projectile, any sounds it might make, ect along with all the other data that is needed to output the game world (From that client’s point of view) to both client1 and client2 (Remember they are both standing next to each other so both can see the projectile).

Though then again UE4 might do things a bit differently or totally differently from this so don’t take this as “This is exactly how it is done in UE4”. I just thought maybe a general overview of how a lot of client/server games handle things might help. Maybe someone more experience with the UE4 networking model might be able to give a more concrete answer to your questions.

Hopefully this was able to help a bit and if you have any questions about anything I mentioned feel free to ask either through this post or through a PM. Anyways wish you the best of luck with your project.

No, thats pretty much it.

  • Client #1 fires.
  • Server spawns projectile.
  • Server ticks (updates) the projectile on the server.
  • Projectile is replicated (sent across network) to anyone that it is relevant for (e.g. can be seen), such as client #1 & #2.
  • Client #1 & #2 both receive relevant info for them to simulate the projectile (update it client side).

If the projectile has any random elements, you may need to take steps to ensure that server & client stay sync’d up, but you get the idea.

@Zereo thanks for the detailed answer.

@Kris you mean, with my code what you describe should already happen? Or are you speaking in general terms how I would handle everything?

Zereo and Kris give excellent examples of how this can be done (client asks server to spawn, server eventually replicates projectile to all clients through standard replication path).

If you don’t see the projectile spawn on the clients, one thing to look for is make sure you have either bReplicates set to true in the projectile constructor, or call SetReplicates with true on the projectile after spawning.

Hope this helps!

Just how projectiles are generally handled.