How would I architecture my code

Options
I'm switching from UNet to PUN and I have some questions on how to architecture my code. In UNet I had a pretty simple formula: player sends input to server, server does stuff, server updates relevant stuff on all clients. But in PUN there is no server, it's just a bunch of clients talking to each other. I have a few ideas though. For example, let's say I shoot an enemy:
1. Should I apply the damage to the enemy on the client that shot the enemy and then send the new health value to all players?
2. Should I send an RPC to all clients telling them to apply X amount of damage to the enemy (would likely cause some de-sync issues)
3. Should I send an RPC to the master of the room, apply the damage there and send it to all clients. I think this would be the best approach to the problem since it is the closer I can get to an authoritative server.

I'm trying to find the most optimized way with code easy to follow. In UNet I could always be sure stuff would run on the server using the [Server] tag or in a client using the [Client] tag. In PUN I'm never 100% sure (mistakes can happen) and there is a lot of room for variables to de-sync. So in PUN I need to constantly worry about where the code is running.

Comments

  • Hi @ItsaMeTuni,

    the third option seems to be the one which is 'most safe' in terms of synchronization and anti-cheat, at least as long as the MasterClient is trustable, however this might add some lag to the game. because the MasterClient has to update the information on each client as well. As you already mentioned this is most likely the closest option to an authoritative server.

    Another option would be to handle operations like damage-dealing on the client who actually got hit by something. This would require that each client runs more or less the same simulation. To get into that direction, you can take a look at the Lag Compensation documentation page. Please note, that this doesn't make sure, that each client's simulation is the same, but this is a way to come closer to it. Please also note, that a hacked client has the possibility to avoid damage and may 'destroy' the player's experience.

    If you have a very ambitious project, you can also have a look at the Photon Server where you have two options to run server-side logic. The first is to create the server application on your own, the other is to use Plugins where you can extend existing server-side logic by injecting custom code to it. However this might be some kind of future thoughts.
  • ItsaMeTuni
    Options
    Hello @Christian_Simon!

    Since it took some time until someone (you) replied here or in the post I made in Unity's forum I kinda went with my first option. When a bullet hits an enemy the client who shot the enemy applies the damage and sends the enemie's health (and other values) to the other clients. Anything that doesn't have an owner (like AI) is handled on the master client.

    I went with this option because it reduces the lag by a lot (very important, since it is a fast-paced game with a shit ton of things going on at the same time) and I don't need to use lag compensation (which is kinda hard to implement). I decided I don't really care about cheating because it is a 4 player max. game that is meant to be played with friends.

    Thanks for the answer!