Search Unity

Some Networked Strangnesses

Discussion in 'Multiplayer' started by nickavv, Nov 23, 2007.

  1. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    Thanks to you guys I've gotten much farther towards making online games. :D Now I have some more questions.

    Here's the test so far: http://www.wanilla.net/random/mastertest/mastertest.html

    The things that are bothering me are:

    1. When the second player joins, two blue cubes are instantiated, even though I've checked and double checked that the code only calls for one.

    2. When a player leaves, even though I have OnPlayerDisconnected attempting to clean up objects, nothing gets cleaned up.

    3. I can't find any way to set the maximum number of players per game to 2 rather than 33.

    Thanks for all the help!
     
  2. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    There is obviously a problem with your code, so we'd need to see it to be of much help.

    Again, we'd need to see code. ;-) Perhaps you are doing your Destroy on the wrong client, or you are not using Network.Destroy, or not sending RPC calls correctly to take care of it, etc.

    You pass the maximum number when you are calling Network.InitializeServer(allowedConnections, listenPort);

    Check the docs:
    http://unity3d.com/support/documentation/ScriptReference/Network.InitializeServer.html

    HTH,
    -Jeremy
     
  3. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    Thanks for the ideas. I'll take a look at it later (messing around with orbital gravity from the Wiki right now. :D)
     
  4. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    I was able to get the player max to two (don't know how I missed that!). Here are the sections of code for the other parts.

    Cube Instantiate Code (Creates two cubes at the same place):
    Code (csharp):
    1. function OnConnectedToServer () {
    2.         networkView.RPC ("CreateBlue", RPCMode.All);
    3.         gameJoiner = false;
    4.         gameInPlay = true;
    5. }
    Delete Disconnected Player Objects (Doesn't delete anything at all):
    Code (csharp):
    1.  
    2. function OnPlayerDisconnected (player: NetworkPlayer) {
    3.     Network.RemoveRPCs (player);
    4.     Network.DestroyPlayerObjects (player);
    5. }
    Thanks for all the help!
     
  5. larus

    larus

    Unity Technologies

    Joined:
    Oct 12, 2007
    Posts:
    280
    Hmmm, what does CreateBlue do? Does it call Network.Instantiate by any chance? Are you doing manual instantiations like in the RPC example http://unity3d.com/support/documentation/ScriptReference/NetworkView.RPC.html ?
     
  6. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    CreateBlue is an RPC function a little further down the script that just Network.Instantiates a blue cube prefab. I have the exact same thing (called CreateRed) that gets called when the user creates a game, and that works just fine.
     
  7. larus

    larus

    Unity Technologies

    Joined:
    Oct 12, 2007
    Posts:
    280
    You should not wrap Network.Instantiate inside an RPC function. Network.Instantiate instantiates something on all machines, and you are telling everyone to do exactly that. This has the effect of something being instantiated once per network entity. So with a server and a client you will get two instantiations, for a server with 2 clients you get three instantiations, etc.

    So just do a Network.Instantiate("CreateBlue", etc etc). Don't wrap it inside an RPC, or do a manual instantiation with RPC (using Object.Instantiate).

    http://unity3d.com/support/documentation/ScriptReference/Network.Instantiate.html