Search Unity

  1. Full schedule for #UniteBerlin is now available! Featuring talks on our roadmap, hands-on labs and much more! Check it out!
    Dismiss Notice
  2. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  3. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. 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 'Multiplayer Networking' 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:
    256
    @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?