Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

How to implement character selection menu in photon

Discussion in 'Connected Games' started by Tifoo, Dec 27, 2015.

  1. Tifoo

    Tifoo

    Joined:
    Jul 31, 2015
    Posts:
    4
    Hi,

    I developed a little multiplayer game which work fines in local. I try to go "Online" with photon free.
    I am stuck on how to implement a character selection menu like this :
    upload_2015-12-27_16-7-24.png

    I have got a script which handle all the stuff (how many players are in, which character they select etc) on a "Scripts" GameObject which also add a PhotonView, with this script observed.

    I have a NetworkManager Script which handle the connection to a room (it works fine).

    I tried to make my Scripts GameObject instantiate by Photon in NetworkManager Script and share some data with :

    Code (CSharp):
    1. public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    2.     {
    3.  
    4.         if (stream.isWriting)
    5.         {
    6.             Debug.Log("Writing " + myNumber);
    7.             stream.SendNext((bool[])starts);
    8.             stream.SendNext((bool[])readys);
    9.             stream.SendNext((int[])playerSamouraiCurrent);
    10.             stream.SendNext((bool[])sealedsamourai);
    11.             stream.SendNext((int[])playerSamouraiSelected);
    12.         }
    13.  
    14.         else
    15.         {
    16.             Debug.Log("Reading " + myNumber);
    17.             starts = (bool[])stream.ReceiveNext();
    18.             readys = (bool[])stream.ReceiveNext();
    19.             playerSamouraiCurrent = (int[])stream.ReceiveNext();
    20.             sealedsamourai = (bool[])stream.ReceiveNext();
    21.             playerSamouraiSelected = (int[])stream.ReceiveNext();
    22.         }
    23.     }
    myNumber is the current character number, which should be set in :
    Code (CSharp):
    1.     public void JoinPlayer()
    2.     {
    3.         for(int i = 0; i < 4; i++)
    4.         {
    5.             if(!starts[i])
    6.             {
    7.        
    8.                 myNumber = i;
    9.                 starts[myNumber] = true;
    10.                 Debug.Log("Joined " + myNumber);
    11.                 return;
    12.             }
    13.         }
    14.     }
    JoinPlayer is called on Joining the Room.
    Code (CSharp):
    1.    void OnJoinedRoom()
    2.     {
    3.         GameObject scripts = PhotonNetwork.Instantiate("Prefabs/Scripts", Vector3.zero, Quaternion.identity, 0);
    4.         scripts.GetComponent<MultiMenu_Script>().JoinPlayer();
    5.     }
    When a second player enter, I see another Scripts gameobject, and OnPhotonSerializeView is triggered, but both have "my number value" to 0 because the starts[] is not beeing updated for the second player.

    I am thinking that I am going terribly wrong, it's my first time with networking.

    Which would be the best approach?

    Thank you so much
     
    Last edited: Dec 27, 2015
  2. Tifoo

    Tifoo

    Joined:
    Jul 31, 2015
    Posts:
    4
    I'm now trying to use RPC methods instead of OnSerialize. It's better.

    I have made one script to handle the graphics changing (not with Photon), and a player is now represented by a instantiable gameobject with a player script on it (Observed by a Photon View).
    This script keeps records on value such as "are you ready"? "Which character did you lock"?.

    Code (CSharp):
    1.   [PunRPC]
    2.     public void StartPlayer()
    3.     {
    4.         if (!starts)
    5.         {
    6.             name = "Player " + myNumber;
    7.             starts = true;
    8.             Debug.Log("Joined " + myNumber);
    9.             menuScript.AddPlayer(this);
    10.             return;
    11.         }
    12.     }
    13.  
    14.     [PunRPC]
    15.     void SelectSamourai(int samouraiIndex)
    16.     {
    17.         playerSamouraiCurrent = samouraiIndex;
    18.     }
    19.  
    20.     [PunRPC]
    21.     void SubmitSamourai(int samouraiIndex)
    22.     {
    23.         playerSamouraiCurrent = samouraiIndex;
    24.         if (!menuScript.sealedsamourai[samouraiIndex])
    25.         {
    26.             menuScript.sealedsamourai[samouraiIndex] = true;
    27.             playerSamouraiSelected = samouraiIndex;
    28.             readys = true;
    29.         }
    30.     }
    31.  
    32.     [PunRPC]
    33.     void UnSubmitSamourai(int samouraiIndex)
    34.     {
    35.  
    36.         if (menuScript.sealedsamourai[samouraiIndex] && playerSamouraiSelected == samouraiIndex)
    37.         {
    38.             menuScript.sealedsamourai[samouraiIndex] = false;
    39.             playerSamouraiSelected = -1;
    40.             readys = false;
    41.         }
    42.     }
    I'm still struggling on how giving a proper number at the connection (it's messed up a little bit, i try to apply the "player.id" but it get reset somehow..)

    Code (CSharp):
    1.    void OnJoinedRoom()
    2.     {
    3.         Debug.Log("OnjoinedRoom " + (PhotonNetwork.player.ID - 1));
    4.         GameObject player = PhotonNetwork.Instantiate("Prefabs/PlayerMultiMenu", Vector3.zero, Quaternion.identity, 0);
    5.         player.GetComponent<PlayerMultiMenu_Script>().myNumber = (PhotonNetwork.player.ID - 1);
    6.          
    7.     }
     
  3. Tifoo

    Tifoo

    Joined:
    Jul 31, 2015
    Posts:
    4
    Finally suceeded after too much hours! (using both RPCs and Serialize).
    Cost of learning :).
     
  4. Mad_Mark

    Mad_Mark

    Joined:
    Oct 30, 2014
    Posts:
    270
    @Tifoo I need to do the same thing, and have no idea how to get it working. Can you share your solution?
     
  5. JulioProfe

    JulioProfe

    Joined:
    May 17, 2018
    Posts:
    1
    @Tifoo hi, I wanna made a game with character selector and i have some problems with Photon, could u give me guides or step by step no get that result?
     
  6. abdelhak_1984

    abdelhak_1984

    Joined:
    Jan 18, 2017
    Posts:
    1
    i have problems all the 2d characters aqualse in the room why???