Search Unity

SetScope Problem

Discussion in 'Multiplayer' started by DocEvil, Aug 19, 2014.

  1. DocEvil

    DocEvil

    Joined:
    Aug 11, 2014
    Posts:
    3
    Hi,

    I am struggeling with the network component of my epic :)D) space game. I want the server to load several "Star Systems" in one scene and only transmit the gameobjects of the system to a player, he is currently visiting.

    Is this a viable solution for a small scale game with about 10-20 players?
    Is there another possibility (eg a unity net api with local server and some kind of scene management) I missed to acieve my goal with minimal effort?

    My problem is, that I just can't make a GameObject disappear for my players by changing its scope... I created a script, attached it to my scene and put a setscope in its update-Method:
    Code (CSharp):
    1. void Update () {
    2.     GameObject station = GameObject.Find ("SpaceStation");
    3.     NetworkView nv = (NetworkView)station.GetComponent(typeof(NetworkView));
    4.     foreach (NetworkManager.Player p in NetworkManager.players) {
    5.         nv.SetScope(p.net, false);
    6.     }
    7. }
    As far I understand this scope-thingie, my "SpaceStation" should disappear for all connected players. But its displayed in both (client & server) instances.

    Any clues, hints or useful links wold be appreciated!
    Thanks for your help!
    Malte
     
  2. Cha0os

    Cha0os

    Joined:
    Feb 22, 2014
    Posts:
    17
    SetScope only changes the scope for future State Sync Updates from that NetworkView. This means that if you have already instantiated the Space Station on your Client it won't disappear, it will just stop receiving Updates from the Server.

    You will have to call a seperate RPC to destroy the Space Station on the Client or destroy it client-side if I understand your problem correctly.
     
  3. DocEvil

    DocEvil

    Joined:
    Aug 11, 2014
    Posts:
    3
    So if a client connects to an existing game, I must destroy all GameObjects and recreate then with a "proper" scope?
    Mhmm, the station has a "rotate"-Script attached, I just checked, the rotation is still visible in both clients, shouldnt the rotation "stop" after changing the scope?

    Here is what will happen in my game and what I want to achieve
    - A player logs in, the server instantiates all gameobjects of the system the player is inside
    - Another player logs in, he is in another system. Now i want to initialize all GameObjects in the second system. These Objects shall only be visible to the second player.
     
  4. Cha0os

    Cha0os

    Joined:
    Feb 22, 2014
    Posts:
    17
    No, the rotation would continue in both scenes, because the Update-function of the Rotate-Script is called in the client as well. So the client runs his own, independent but equal simulation of the rotation. You can test this by adding

    Code (CSharp):
    1. if(!Network.isServer)
    2.   return;
    at the beginning of the Rotate-Script's Update-function. Now, the rotation should stop in the Client once you set the scope of the corresponding NetworkView to false on the Server.

    What you want to do will be very hard to achieve with the standard Network.Instantiate as the calls are buffered and when a new client connects it will receive all of the instantiated Objects and I can't think of any way to prevent this without losing all of the buffered calls (which you would need for a player connecting to the same system as another player).

    The only way, that I can think of, to do what you want is to buffer the GameObjects yourself and assign them to the right systems (maybe using a Dictionary?). Then, when a player connects to that system you send all the buffered calls manually via RPC so that the Client can instantiate the correct GameObjects with the correct NetworkViewIDs. On the server, you would set the scope of all objects that are in another system to false for that player.
    This would require a lot of additional programming though.

    I'm currently trying to do something similiar and am still thinking about how to do this intelligently...