Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Setting Up An Authoritative Server

Discussion in 'Multiplayer' started by JarrodParkes, Jun 24, 2010.

  1. JarrodParkes

    JarrodParkes

    Joined:
    Mar 29, 2009
    Posts:
    15
    I have read several forum posts and still have a couple holes in my head on using an authoritative server. I understand that the authoritative server is essentially the game, where... (1) clients request movement or other inputs (2) the server checks their requests validity (3) the server sends back something based on the requests. Now my questions lies in how to implement something like this?

    Does this involve creating a separate Unity project like MyProjectServer (to run on the server) and another project like MyProjectClient (to run on all clients)? Or do I even need another Unity project to run just on the server? I have seen things about running a headless Unity project on the server, which I think is nearly what I said above. If that is the case, how do I make sure that the project is running on my server. Does it run as a stand-alone app? Does it need to be running all the time? Or if maybe if it is not running that would be the time to tell the clients "Server is down"?

    Maybe this is not even the right approach at all, but essentially my goal is to use an authoritative server to network the most simple game possible. I don't really care if it is just boxes moving around on a plane. I would just like to know that by using an authoritative server I am networking in a way that restricts clients from cheating, etc.

    If anyone has tried this or has a simple example, I would greatly appreciate any help on this topic. Thanks!
     
  2. JarrodParkes

    JarrodParkes

    Joined:
    Mar 29, 2009
    Posts:
    15
    After some thought and insight from several friendly forum members, it seems I can still get the "authoritative" behavior I want using SFS and Server-Side Extensions. The extensions would handle the game logic and the clients are simply visualizers. I have already started to dig into some of the examples from the SFS website.

    Now my only question is (and maybe it has a simple answer)... if I am using SFS, do I run SFS on a server anywhere, or does it need to be the same server where my web/SQL stuff is?
     
  3. JarrodParkes

    JarrodParkes

    Joined:
    Mar 29, 2009
    Posts:
    15
    After a week or thinking on the subject, I have began looking through the SFS tutorials (by Thomas Lund) about how to use server side extensions to handle game logic. This is definitely along the lines of what I would like to do for my simple networked games.

    Now my questions are shifting a little from the set-up to the implementation of some actual server side code. Now, in Thomas Lund's tic-tac-toe example there is no real need for any readily synced type of data like player transforms or rotations. Let's say I would like to create a simple pong game in which both clients transforms would need to be synced across the network.

    What types of things would the server side scripting be doing in a case like this? Does this require those scripts on the server to handle interpolation or simply validate movements as long as they are within a certain range, then let the clients do the interpolation?
     
  4. Discord

    Discord

    Joined:
    Mar 19, 2009
    Posts:
    1,008
    The clients would definitely handle the interpolation. As you said previously, the client is simply a visualizer. The client would have access to the paddle's current position, receive a packet with the paddle's new position from the server, and interpolate from the old position to the new position. That's what I would do anyway. :p If anyone thinks differently, I would love to know what they would do.
     
  5. JarrodParkes

    JarrodParkes

    Joined:
    Mar 29, 2009
    Posts:
    15
    that sounds like a great idea, and good starting point for me. thanks for the help.

    i was also curious, and let's stick with the pong example...

    for the ball that bounces around in pong, if i update its position in the same way, would that insure that the collision events handle nearly the same on both clients?

    for example, what if the ball collides with clientA and calculates in a different direction on clientB's game? how do you even handle something like that? is that even possible?
     
  6. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    did you check out the authorative example in the network example project.

    it should give you a solid base to avoid such things, but might want to integrate some "if difference on client > max allowed diff -> force sync to server data" and before that massive enforcement, you might want to work with lerp on vector3 and quaternion
     
  7. JarrodParkes

    JarrodParkes

    Joined:
    Mar 29, 2009
    Posts:
    15
    which example are you talking about? or do you have a link to the project you are talking about?

    thanks :D
     
  8. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    unity3d.com -> support -> resources -> example projects -> networking example
     
  9. Discord

    Discord

    Joined:
    Mar 19, 2009
    Posts:
    1,008
    As to your earlier question about the ball being the same on both clients, maybe you should try simulating the ball movement on the server? If the server keeps track of the ball's speed (which could be kept constant for simplicity's sake), and the server also knows the paddle position for both players, the server should be able to detect collisions fairly easily based on the ball's speed and direction. The server then just sends a packet to both clients with the ball's position and the ball should appear to be the same across both clients. That's what I'm thinking anyway. If anyone else has a differing opinion, I would like to hear it. :)
     
  10. GaborD

    GaborD

    Joined:
    Jul 9, 2007
    Posts:
    42
    I agree Discord, if you go authoritative, the main simulation should completely run on the server and the clients just show an (approximated) image of what's happening on the server.

    The big problem is that you want instant paddle feedback for movement-controls on your clients or the game feels sluggish and unfun, so the own paddle of a client has to kinda always be ahead of the "real world" (server) while the paddles of other players and the ball are always running a bit late (the server state needs to be replicated down to the clients).
    This can lead to visual discrepancies between server and clients (and between the clients) that need to be adressed.
    Otherwise a player would eventually see himself catching the ball but the server would say "GOAL!" and such.

    So you need to extrapolate/interpolate wisely and do some clever guesswork of what's going to happen in order to keep things relatively consistent on every client.
    Everyone who has played an MMO or FPS side by side with 2 clients connected to internet servers knows how big the differences between what each player sees can be, even with AAA networking solutions and lots of optimizations.

    Luckily, in your Pong example, movement and physics are nicely simple, so you shouldn't really have any differences between server and client positions besides the mentioned time difference, so you could extrapolate ball movement (it's nicely deterministic afterall) to have an approximated "up to date" simulation where your instant-paddle and authoritatively controlled ball seem to work like they should.
    You are kinda guessing and simulating what is happening on the server "right now".
    Still has some caveats, like the server ofcourse getting your movement updates a bit later (you could ofcourse compensate for this too, knowing it WILL happen)... so there will be small differences, but it should work well enough.

    For other games, it can get more complicated and you could eventually end up having to store and keep redoing the last movement&physics updates of your player object with the incoming serverside positions to have up to date server certified approximated positions to which you can interpolate to if needed.

    You can find tons of info about game networking and things like Client-Side Prediction on the web.
    A good place to start is here, has info on authoritative approaches too:
    http://gafferongames.com/networking...ogrammer-needs-to-know-about-game-networking/
     
  11. JarrodParkes

    JarrodParkes

    Joined:
    Mar 29, 2009
    Posts:
    15
    Couple more questions or things I need reassurance on:

    (1) the server maintains the positions of both paddles
    (2) the server maintains the x-axis position's of both GOALS
    (3) the server initiates the ball's movement, and then maintains the ?average? position of both clients?
    (4) because the server has both the paddle and ball positions, it can detect collisions...but then what?

    I guess I'm still a little hairy on how this should be constructed, and at this point I am patiently developing how this should look in my head before I ever implement anything.

    Thanks for all the help!
     
  12. GaborD

    GaborD

    Joined:
    Jul 9, 2007
    Posts:
    42
    You could just run the game on the server as if it was a single player game, but taking control inputs from the clients and send the ball and paddle positions back to the clients, where all the funky stuff is calculated to make it all seem in sync.

    This has the additional bonus of improved security (kinda the main point of running an authoritative server), because nothing is really handled on the clients. They just relay input states to the server.
    (up down buttons or whatever you use for control)
     
  13. JarrodParkes

    JarrodParkes

    Joined:
    Mar 29, 2009
    Posts:
    15
    running the game on the server would require (i'm assuming) a headless version of the game?

    if that is the case, how would I use SFS and server-side scripting? wouldn't that not really follow the architecture set up in Thomas Lund's SFS tutorials?
     
  14. JarrodParkes

    JarrodParkes

    Joined:
    Mar 29, 2009
    Posts:
    15
    new train of thought for a multiplayer pong game that uses SFS server and its server side scripting to control the possibility of cheating (which in the case of pong is not really that much, but i figured that experimenting with this "server-side protection" would be a good idea at the most simple level):

    Here are the steps taken:

    (1) the server initializes the players' starting paddle positions
    (2) the server generates the ball and its starting direction or force
    (3) the server receives inputs from both players for their requested paddle positions

    ...

    (4) if the requested positions and the ball position which is stored on the server collide, then the server will calculate a new direction for the ball
    (5) if the ball's position falls outside a certain bounds then one of the players has scored a point

    ...

    (6) give the correct player a point and re-initialize the ball

    ...

    (7) if one of the player's scores reaches maybe 5 points, then the game is over

    ...

    (8) stat tracking for fun (mainly just to test it with something simple before i go larger in scale)

    let me know what you think :mrgreen: