Search Unity

Unity server - separate project?

Discussion in 'Multiplayer' started by Pia, Feb 16, 2009.

  1. Pia

    Pia

    Joined:
    Feb 16, 2009
    Posts:
    78
    Hello,

    i want to learn about multiplayer programming. For the start i set myself the goal to provide a dedicated server and a client. The client(s) should be able to connect to the server. For each client the server should spawn a simple cube, which the respective client then should be able to move around over a plane.

    I want the whole "game logic" to reside on the server. Now i am having a few troubles: I know about the multiplayer example where they provided an authorative server, which is basically what i want to do. However, their client and server code is kind of mixed up. I cannot figure out which part of the code belongs to the server and which belongs to the clients.

    My question comes down to the following: When creating a server with the unity engine, should i follow the example and merge client and server into one project? This way they would have access to the same scripts and prefabs, which kinda makes sense. Or should i rather create two projects, one for the clients and one for the server, to minimize the overhead for the server?
     
  2. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,307
    I'd use a single project because if you don't you might get messed up NetworkViewIDs very quickly. There's a couple of approaches you could follow for making this "manageable":

    a) Proper object oriented separation. Unfortunately, since you'll have to rely heavily on RPCs this will not be easy. In particular, keep in mind that any RPCs that the clients call on the server need to be present (at least their method declaration) on the client, too. Also, it's somewhat likely that some of the code used on the server will also be used on the clients (e.g. when you check for something to be a valid action ... you'd do this both on the client for immediate feedback and on the server for "final say" e.g. before passing the action on to other clients).

    b) Using some singleton that has a "I am the server" flag. Then, you can do conditional checks for different codepaths where ever necessary.

    c) Conditional compilation. Unfortunately, conditional compilation symbols are currently not supported in Unity (except for the built-in UNITY_IPHONE and maybe one or two more, not sure). However, you kind of need that because otherwise, you distribute your complete server code with the clients. I've used per-class symbol definitions that I comment-in/out via global search replace (from within Visual Studio). I've also created some little tools for this to automate inside my build-scripts. This is just a workaround until Unity supports this right in the editor (which may still take a little while, though).

    Personally, I use a combination of (b) and (c). There's only few cases where I see (a) would really benefit my project. I started with only (b), which was okay because I had results much more quickly. But in the end, it cost me quite a bit of time later (partly because I rolled my own "conditional compilation approach" which I later found out was not the most elegant way to do it).

    Sunny regards,
    Jashan
     
  3. Pia

    Pia

    Joined:
    Feb 16, 2009
    Posts:
    78
    Sounds like a serious multiplayer project with many players would require a server outside the unity framework. But this certainly helps for my exercises, thank you.