Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Hold Network.Instantiate until level has loaded

Discussion in 'Multiplayer' started by Dark-Protocol, Dec 25, 2014.

  1. Dark-Protocol

    Dark-Protocol

    Joined:
    Nov 19, 2011
    Posts:
    279
    Merry Christmas! I'm using the standard Unity networking to make a multiplayer game. In my case when the player joins the server, they go into the lobby where they have to wait for the current game to finish. However, the players currently playing instantiate objects with Network.Instnatiate which appear in the Lobby and I don't want this to happen. Is there a way to separate the Network.Instantiate commands so that they execute only for the players with a given level prefix ?
    I know that Network.Instantiate takes a group parameter but I couldn't find if I can utilize that to achieve what I need in the docs.

    Cheers and happy holidays!
     
  2. Dark-Protocol

    Dark-Protocol

    Joined:
    Nov 19, 2011
    Posts:
    279
    The only solution I have found so far is using individual RPCs to spawn objects and set their newtorkView ids locally. That's a really bad solution for me. There's got to be another way!
     
  3. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
  4. Dark-Protocol

    Dark-Protocol

    Joined:
    Nov 19, 2011
    Posts:
    279
    Yes, I'm using this for level initialization but that doesn't prevent network instantiated objects from leaking into other scenes (in this case - the lobby scene). Or do you have something special in mind ?
     
  5. Dark-Protocol

    Dark-Protocol

    Joined:
    Nov 19, 2011
    Posts:
    279
    As a matter of fact, all buffered RPCs get received by the client as soon as they join the server. I tried SetNetworkPrefix, tried SetScope..still no luck.
     
  6. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Of cause they do. If they should not receive it at start then don't use AllBuffered. Its more a flaw in your design then.

    If you are using a a room as a lobby and the same room for games then there is no way around it.

    ... Well you could write your own messages class and send it as RPC's when needed. But its going to be a mess.
     
  7. Dark-Protocol

    Dark-Protocol

    Joined:
    Nov 19, 2011
    Posts:
    279
    Yeah, in the last week I actually did exactly what you said and that solved most of my issues but still Network.Instantiate seems to have AllBuffered RPC Mode and I do not know how to restrict this. The only thing that comes to mind is to use a group for the instantiation and then right after the instantiation remove the RPCs in this group. That way only the players who have alredy joined the server will receive the instantiation and not the new ones who join later. Problem with that is that the players who are already in the server but are still in the lobby and not in game (the ones who for some reason haven't started the game) will receive the instantiation.
     
  8. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Well if you use a room as a lobby, and you use the same room for the lobby and game, then your design might be a bit flawed.
     
  9. Dark-Protocol

    Dark-Protocol

    Joined:
    Nov 19, 2011
    Posts:
    279
    No, I'm using different scenes for the lobby and the game so it's not that. Network instantiations leak through scenes no matter what's the level prefix.
     
  10. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Well then you DID misunderstand how it works. You can be in the same room but 100 different scenes, it will still call all messages in all scenes. However if are in 100 different rooms but the same scene it won't happen.

    A room is just a "collection of players" being able to interact over the internet. They dont care about scenes.
     
  11. DryTear

    DryTear

    Joined:
    Nov 30, 2012
    Posts:
    312
    on the players that spawned, in one of their scripts, in the awake function, you can check what scene IS loaded, and if its loaded in the lobby scene, then you can hide them somewhere, so no one knows that theyre there. or you can do a forloop through all of the players connected, and send the instantiate rpc to every player but the ones who havent loaded the current scene.
     
  12. Dark-Protocol

    Dark-Protocol

    Joined:
    Nov 19, 2011
    Posts:
    279
    No, I do understand that completely. Can you be more specific with what you mean by "room". The built-in networking system does not have anything that goes by the name room as far as I know so now I'm really confused. I have different scenes and all of the new players who come to the lobby receive the buffered Network.Instantiations that have been fired during the current game phase. I try to deal with this by setting different level prefixes on the players who are in the lobby and the ones who are in the game phase - that does not work for me. I want to know if there is a way to say group the buffered RPCs from the game phase and say "hey, these RPCs should stay within the range of the players who are in the game phase and anyone who joins later should disregard them". You will probably ask why am I using buffered RPCs then. It's simple - different load times. The ones who load the game phase scene later need to receive the information that they have missed.

    DryTear, that's what I'm almost about to do but the problem here is that this will be a nightmare to manage and the server will not be the real owner of the instantiated objects.

    Thank you both for your replies so far!