Multiplayer Question On Switch Has Authority

Hi, I am now to the point where I’m doing replication and the networking part of my project. I think I have a good grasp on how this works but have a simple question, maybe. I have a sort of a shooter game and my characters fire bullets, should I use switch has authority when spawning the projectiles? or is just making a custom event and set to run on server then multicast a better way? or do both together? But from what I have read is that switch has authority should be used to prevent cheating. I have taken a look at a few projects from the marketplace that have multiplayer enabled and in their whole project they only call switch has authority a few times in the whole project… this seems strange because wouldn’t you want everything you could to be ran through the switch to prevent cheating?

All comments welcome to help me figure out the best plan, thanks

Had similar “fun” with replication few months ago, turns out if you want to handle shooting on server to prevent cheating just use an event which is set to run on the server, handle there all the calculations and apply damage and multicast effects to all. Most of the time you don’t really need that switch. Just watch out for actor ownership if the actor is not owned by a controller and you call any replication event, the event just won’t work and nothing will happend (learned that the hard way) in that case the switch might come in handy.

Awesome input! Thanks

A basic implementation of spawning a bullet could look like this.
**Pawn **Client-side “Fire Button Pressed” input event > RunOnServer Event “FireButtonPressed”
Pawn Server-side “FirebuttonPressed” received > SpawnActorOfClass (Make sure the Bullet Blueprint is set to Replicate)

There is no need to also make a Multicast event back to the client that called it or the other clients, since it is implicitly called with BeginPlay when the bullet is replicated to the clients.

This method will however not be a very good experience for the player that fired the bullet since the bullet will not appear instantly when pulling the trigger.
In some cases you can obscure this by triggering a VFX locally when the trigger is pulled but if that is not good enough you would spawn a bullet locally and use lag compensation on the server to determine the hit location.

“Movement prediction” and “Lag compensation” is something every fast paced networked game has to deal with to give a better multiplayer experience and is a topic in itself.

“SwitchHasAuthority” in itself is not what prevents cheating it just helps distinguish Server from Client. To prevent cheating you need to sanitize the input coming from the clients and reject it if it trying to do something illegal.

If you have something that the clients should not be able to tamper with it will exist only on the server and then there is no need for “SwitchHasAuthority”.

Thank you for the explanations!

I wanted to share this for future Devs. This is what I found to be the secret sauce.

P should only be connected to RunOnServer and it would work exactly the same. No need for the “Switch Has Authority” and extra complexity.

Also remember to Set a replicated variable and use OnRep except if the event is purely cosmetic like a particle effect or instant sound rather than using Multicast RPC’s.