How to Network A Shooting Script?

Hi; thank you in advance for taking the time to read my post. I am curious on how to network a shooting script. I have the function, but I would like it to be replicated to the clients (will be using dedicated server), and as server-side as possible (to prevent cheating). If you can offer any assistance, that would be greatly appreciated! Here is my scripts :


The gist of it is:

  • client executes fire command on server using an RPC.
  • server updates authoritative things like bullet count.
  • server traces the shot.
  • server applies damage where applicable.
  • server multicasts the information and result of the trace to all clients - where the weapon fired from and and what angle, what was hit, hit location, etc.
  • each client executes cosmetic aspects of the shot, both the effects of firing a gun like muzzle flashes and audio of the weapon firing, and effects of the hit like spawning a particle system where the bullet hit.

I don’t have experience with high latency environments but possibly one would like the firing client to execute the cosmetic aspects of firing the weapon before receiving the final multicast back from the server. In this case these aspects would run on the client before executing the server sequence (or immediately after).

How does the client execute the command using an RPC? I’m not sure what you mean by that; do I run the command locally and if it’s a remote then call a “ServerFire” script (which runs on server)?

Normally there would be some ‘fire’ input in your game, which should be handled by an event in the playercontroller or pawn.
This should in turn call the ‘run on server’ fire event.

Right, but aren’t you supposed to use an RPC somewhere in there like you were saying? So I could do Input Fire -> Fire (Run On Server). When would I use the SwitchHasAuthority?

Yes. Calling an event which has the ‘run on server’ flag set *is *executing an RPC.

Re SwitchHasAuthority, you should use it inside any execution path which can technically be reached from both the server and client and you want to make sure it can run on only one of them. for example, I have something that I want done in BeginPlay but only on the server. Since BeginPlay is an event which is run on both server and client, I can use a SwitchHasAuthority node before my server code to make sure that part is only run on the server.
This is redundant on nodes which are set to ‘run on server’ because they can not be run on the client anyways.