UNet Server Authoritative process

Hi all,

I was thinking on how to make a server authoritative script in order to handle player stats, damage, health and many other things, so I came up with this, which for me looks good but i wanted to discuss it with you before losing time coding something that will end up being too laggy.

This is the situation: A Player attacks an NPC mob that was spawned by the Server

  • Client sends a “Damage request function” to the server with the NPC in question - e.g: DamageNPC(NetworkID of the NPC)

  • Server checks if that NPC is actually near the Player

  • Server asks via a PHP+MySQL server to get Client’s stats (DMG, Critical % and Critical chance)

  • Server does the calculations (Attack 50 * Armor 0.5 = 25 Damage to NPC)

  • Server broadcasts the remaining NPC’s HP via a [SyncVar]

  • Repeat

Am I on the right track here? If so, wouldn’t this require huge amount of resources to calculate each hit by each player on a server with say, 50 people?

I plan to do something similar for crafting, trading and many other basic functions but with a Webdev backend I cant imagine a PHP+MySQL server handling that amount of requests on a server with 2.0Ghz processor…

If im wrong I would really appreciate if you point me toward the right direction.

Keep the player’s record in memory and fork a thread process to save it back to the DB every so often. Your dedicated hosted unity server can talk directly to MySQL using a MySQL library. Don’t call a web based PHP script - that’ll be nightmarishly slow.

Wouldn’t that require me to save the MySQL user and password on a Client?

If you’re going to have a dedicated authoritative server, it will be hosted and won’t also be a client. You lose all authoritativeness by having a client that is also the server, as there’s nothing to stop cheating.

My bad, I misread your last post and thought you were suggesting that every Client would connect itself to the MySQL server.

All I need to do is avoid WWW requests and use MySQL requests directly from the Unity Server, but, is the rest of the process right? It seems OK to me…

I also read that by using

#If Server

#endif

I can protect the server code (MySQL password in it) by not including it on the build, but i haven’t tested this either.

It depends on what you want to do, but for a dedicated authoritative server, I would have two different projects. One for the server and one for the client. Clients never get anywhere near the server code, which keeps everything safe (for a given value of ‘safe’).