Search Unity

Third Party (Solved) Players that join a game can't see other previously instantiated objects (PUN)

Discussion in 'Multiplayer' started by Aaron_T, May 8, 2015.

  1. Aaron_T

    Aaron_T

    Joined:
    Sep 30, 2014
    Posts:
    123
    I'm having an issue and I think that I know what the problem is, but I do not know how to fix it. Let me explain simply how my game works.

    When a player connects to the server, they are in a scene called MainLobby where they can create rooms or join rooms which have already been created. When a room is created, a scene called RoomLobby is loaded and players can join the room and will load that scene. The master client can start the game, forcing all users in the RoomLobby to load the DeathMatch scene. Everything in the NetworkDeathMatch scene works perfectly for all the players that were in the RoomLobby with the master client.

    The problem is with players that join the room at this point. I have it so that they join the room while in the MainLobby scene they send an RPC call to the master client requesting which scene to load, the master client sends an RPC call to the player instructing him to load the DeathMatch scene, and then the player loads the DeathMatch scene. They load the scene just fine and can spawn a ship, but they can not see any of the other players' ships until those players' ships are destroyed and they spawn new ones. The other players that were already in the room can see the new player's ship the whole time.

    The way that I have ships spawn is with this simple code:

    GameObject shipGO = PhotonNetwork.Instantiate("Ships/" + ship.GetName(), spawnLocation, Quaternion.Euler(0, Random.Range(0, 359), 0), 0);
    shipGO.GetComponent<PhotonView>().RPC("SetPlayerID", PhotonTargets.AllBuffered, player);

    To me, it seems like the players that join late are not receiving any of the information that should be in the buffer from PhotonNetwork.Instantiate and my RPC call to PhotonTargets.AllBuffered.
    If anybody can offer assistance I would appreciate it greatly, thanks!
     
  2. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    As soon as you join the room, you will get all messages sent. However as you are asking the master client for the scene name, you are likely receiving all the messages before you get an answer and loads the correct scene, which means that you actually load the prefabs from the resource folder in you lobby scene, before you then load the next scene and loses them until they are respawned.
     
  3. Aaron_T

    Aaron_T

    Joined:
    Sep 30, 2014
    Posts:
    123
    That's what I was thinking was happening. Is there any way to store the information on the scene to load somewhere so that players can receive that info and load the correct scene before they connect to the room?
     
  4. Aaron_T

    Aaron_T

    Joined:
    Sep 30, 2014
    Posts:
    123
    I solved my own problem using custom room properties that are able to instruct newly connected players to load the correct scene.
     
    tobiass likes this.
  5. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Thats a very good way to do it. We do the same in our game, works like a charm :)
     
  6. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    Maybe you didn't find it but PhotonNetwork.LoadLevel() is supposed to support you doing in-room level loading. It uses exactly the same workflow as briefly described by @Aaron_T. The level is stored in a room property and available even before the in-room events. When we join, this is checked and the message queue is paused if the client needs to load something. When loading is done, the queue continues and you get all in-room events.
    This is not fully bullet proof but when you know how it works in principle, you should be able to fix remaining issues quickly, I hope.
     
  7. Aaron_T

    Aaron_T

    Joined:
    Sep 30, 2014
    Posts:
    123
    I have the scene saved in a customroomproperty that is set by the master client every time a new scene is loaded, and accessed like this by new players from the lobby:
    Code (csharp):
    1.  
    2. void OnJoinedRoom() {
    3.         PhotonNetwork.LoadLevel(PhotonNetwork.room.customProperties[RoomProperty.Scene].ToString());
    4.     }
    5.  
    Is there a better way?
     
    Trikis likes this.
  8. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    @Aaron_T: You could leave the task to PUN, if you like.
    But if it's working, this is totally good enough.