Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Change Value .

Discussion in 'Multiplayer' started by mimat18, Jul 18, 2019.

  1. mimat18

    mimat18

    Joined:
    Feb 16, 2019
    Posts:
    12
    1- Let say I have a variable X .
    I want every player enter the room Compare his "X" Value with my X Value .
    2- can I change his X Value

    Maybe a small example .
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,753
    Yes you can.
    Please show us what you tried.

    Also, please do not duplicate posts. Thx.
     
  3. mimat18

    mimat18

    Joined:
    Feb 16, 2019
    Posts:
    12
    Simple thing
    I want every player enter the room
    1- get from him the value of X
    2- change him the value of X
     
  4. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    It depends to some degree on which networking solution you're using.

    In Photon PUN, you'd probably want use a CustomPlayerProperty and in UNET a Syncvar (but I wouldn't recommend using UNET anyway, as it's deprecated.) Other networking solutions may have different approaches.

    You didn't do what Antypodish asked, you just repeated your first question in a slightly different word order. Without giving us more to go on you're unlikely to get anything more specific in the way of advice.
     
    Antypodish likes this.
  5. Livindaki

    Livindaki

    Joined:
    Jun 13, 2017
    Posts:
    49
    i am using PUN2 .

    in a case that i have create a room and entered the room .. and i have a Variable X for each player .. i want to change for every player enter room the value of X .
    how i cant do that ?
     
  6. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    As I said in my previous reply, a CustomPlayerProperty sounds like it may be what you need.

    If you don't know what that is you should probably work through a few tutorials from the Exit Games website. It's impossible to give you any further advice from the scant information you've provided.
     
  7. mimat18

    mimat18

    Joined:
    Feb 16, 2019
    Posts:
    12
    Ok thank you I'll try it .
     
  8. mimat18

    mimat18

    Joined:
    Feb 16, 2019
    Posts:
    12
    I am using Pun2
    Let say that there is twi variables int X and boll passing . A player was joined the room I want to compare if the value if his X= the value of my X . If yes pass a true to his boolian variable
    That's it .
     
  9. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    Implement IInRoomCallbacks and then do something like

    Code (CSharp):
    1.     public void OnPlayerEnteredRoom(Player newPlayer)
    2.     {
    3.         if (((int)newPlayer.CustomProperties["key"]) == ((int)PhotonNetwork.LocalPlayer.CustomProperties["key"]))
    4.         {
    5.             // Do your thing here...
    6.             // The easiest thing is probably to call an RPC on the newPlayer object that sets the value of his bool.
    7.         }
    8.     }
    This tutorial has an example of how to set up a custom property https://sharpcoderblog.com/blog/photon-network-beginners-guide
     
    Last edited: Jul 19, 2019
  10. Livindaki

    Livindaki

    Joined:
    Jun 13, 2017
    Posts:
    49
    i do it but now i have a problem calling the RPC .. sorry i am new on PUN .how can i call the newPlayer to create a Button for example.
     
  11. Livindaki

    Livindaki

    Joined:
    Jun 13, 2017
    Posts:
    49
    Can you Please ?
     
  12. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    You say you have a problem calling the RPC, but you don't say what the problem is, and more importantly you haven't posted your code.

    If you post the code that you've tried that doesn't work, then we can have a look at that and help you correct it.
     
    Last edited: Jul 22, 2019
    Antypodish likes this.
  13. Livindaki

    Livindaki

    Joined:
    Jun 13, 2017
    Posts:
    49
    OK .. Sorry
    my mission is to create a room remotly for a client that i spacify the name and the size and the password .. and store them on the [Key] .. i create a temperolaly room and the client automacly enter the room .. checking his confirm code if its ok then i can pass for him the keys (Variables) and add to the listvew an item of the room .. he can then enter the room by choosing that item ..
    a script was like that but all the time i had errors and some times no errors but didnt add any item to his listview ("i want to add the item to him not to me . also if it add for him and me its ok)
    one of the mostly errors was : PhotonView with ID 1001 has no method .....
    ----------------------------------------------------------------------------------------------
    My Script:


    Code (CSharp):
    1. using System.Collections.Generic;
    2. using ExitGames.Client.Photon;
    3. using Photon.Pun;
    4. using Photon.Realtime;
    5. using UnityEngine;
    6. using UnityEngine.UI;
    7.  
    8. public class Add_Room : MonoBehaviourPunCallbacks
    9. {
    10.  
    11.     public GameObject Prefab;
    12.     public Transform trans;
    13.  
    14.     public InputField Room_Name;
    15.     public InputField Room_Size;
    16.     public InputField Room_Password;
    17.     public InputField Room_Coast;
    18.     public InputField Room_Confirm_Code;
    19.     public InputField Room_Master_Confirm_Code;
    20.     Text[] txt;
    21.     public GameObject Start_Screen;
    22.     public GameObject Room_Screen;
    23.     public GameObject Room_Setup;
    24.  
    25.     bool Create_Room_Is_OK = false;
    26.    // Load_Rooms LD = new Load_Rooms();
    27.     public static GameObject parents;
    28.     public GameObject source;
    29.      PhotonView pv;
    30.     public Player My_New_Player;
    31.     Hashtable setPlayerProperties;
    32.     //public override void OnEnable()
    33.     //{
    34.     //    Debug.Log("Scene Enabled");
    35.     //    if(!PhotonNetwork.InLobby)
    36.     //       PhotonNetwork.JoinLobby(TypedLobby.Default);
    37.     //}
    38.     private void Start()
    39.     {      
    40.         //if (Load_Rooms.Player_Name != "tamim")
    41.         if (!PhotonNetwork.InRoom)
    42.             PhotonNetwork.JoinLobby(TypedLobby.Default);
    43.  
    44.     }
    45.  
    46.     //public override void OnRoomListUpdate(List<RoomInfo> roomList)
    47.     //{
    48.     //    Debug.Log("room Name: " + ++xxx);
    49.     //    Debug.Log("room List: " + roomList.Count);
    50.     //    if (Load_Rooms.ClientConfirmCode != "" && Load_Rooms.Player_Name != "tamim")
    51.     //        PhotonNetwork.JoinRoom("RG5", null);
    52.     //}
    53.     public override void OnJoinedLobby()
    54.     {
    55.         if (Load_Rooms.ClientConfirmCode != "" && Load_Rooms.Player_Name != "tamim")
    56.         {
    57.             PhotonNetwork.JoinRoom("RG5", null);
    58.         }
    59.             //Set our Player's property
    60.             setPlayerProperties = new Hashtable();
    61.             setPlayerProperties.Add("PlayerCC", (string)Load_Rooms.ClientConfirmCode);
    62.             setPlayerProperties.Add("PlayerRN", (string)Load_Rooms.ClientRoomName);
    63.             setPlayerProperties.Add("PlayerRS", (int)Load_Rooms.ClientRoomSize);
    64.             setPlayerProperties.Add("PlayerRP", (string)Load_Rooms.ClintRoomPassword);
    65.         PhotonNetwork.LocalPlayer.SetCustomProperties(setPlayerProperties);
    66.      
    67.         ////Will print "100"
    68.         //print((string)PhotonNetwork.LocalPlayer.CustomProperties["PlayerHP"]);
    69.         //Debug.Log("0-State is: " + PhotonNetwork.NetworkClientState.ToString());
    70.  
    71.  
    72.     }
    73.     public void Adding_Room()
    74.     {
    75.         if (PhotonNetwork.InRoom) return;
    76.             if (Room_Name.text!="" && Room_Size.text!="" && Room_Password.text!="" && Room_Confirm_Code.text!="" && Room_Coast.text!="" && Room_Master_Confirm_Code.text=="")
    77.         {          
    78.             if (check_if_Room_name_Not_Present(Room_Name.text) == false)
    79.             {
    80.                 GameObject go = (GameObject)Instantiate(Prefab);
    81.                 go.transform.SetParent(trans, false);
    82.                 txt = go.GetComponentsInChildren<Text>();
    83.                 txt[0].text = Room_Name.text;
    84.                 txt[1].text = "0/" + Room_Size.text;
    85.  
    86.                 Debug.Log(txt[0].text);
    87.                 Debug.Log(txt[1].text);
    88.  
    89.                 //RoomInfo rmf;
    90.                 _RoomsList rm = new _RoomsList();
    91.                 rm.RoomName = Room_Name.text;
    92.  
    93.                 rm.RoomSize = int.Parse(Room_Size.text);
    94.                 rm.RoomPassword = Room_Password.text;
    95.                 rm.Room_Coast_Per_one = int.Parse(Room_Coast.text);
    96.                 rm.RoomConfirmCode = Room_Confirm_Code.text;
    97.                 //rm.Room_Is_Active = true;
    98.                 //rm.Room_Is_Private = false;
    99.                 //RoomOptions ro = new RoomOptions() { IsVisible = true, IsOpen = true, MaxPlayers = (byte)int.Parse(Room_Size.text) };
    100.                 //PhotonNetwork.CreateRoom(Room_Name.text, ro, TypedLobby.Default, null);
    101.                 Debug.Log("had creat a room with name: " + Room_Name.text);
    102.                 First_Connection.MC.Rooms.Add(rm);
    103.                 // First_Connection.MC.rooms
    104.                 First_Connection.Save();
    105.             }
    106.         }
    107.       if (Room_Name.text != "" && Room_Size.text != "" && Room_Password.text != "" && Room_Confirm_Code.text != "" && Room_Coast.text != "" && Room_Master_Confirm_Code.text != "")
    108.         {
    109.          
    110.             Load_Rooms.ClientRoomName = Room_Name.text;
    111.             Load_Rooms.ClientRoomSize = int.Parse(Room_Size.text);
    112.             Load_Rooms.ClintRoomPassword = Room_Password.text;
    113.             Load_Rooms.ClientRoomCoast = int.Parse(Room_Coast.text);
    114.             Load_Rooms.ClientConfirmCode = Room_Confirm_Code.text;
    115.             Load_Rooms.MasterConfirmCode = Room_Master_Confirm_Code.text;
    116.  
    117.             setPlayerProperties.Clear();
    118.             setPlayerProperties.Add("PlayerCC", (string)Load_Rooms.ClientConfirmCode);
    119.             setPlayerProperties.Add("PlayerRN", (string)Load_Rooms.ClientRoomName);
    120.             setPlayerProperties.Add("PlayerRS", (int)Load_Rooms.ClientRoomSize);
    121.             setPlayerProperties.Add("PlayerRP", (string)Load_Rooms.ClintRoomPassword);
    122.             PhotonNetwork.LocalPlayer.SetCustomProperties(setPlayerProperties);
    123.             //if (PhotonNetwork.InLobby)
    124.             {              
    125.                 Go_To_Rooms_Screen();
    126.                 RoomOptions ro = new RoomOptions() { IsVisible = true, IsOpen = true,MaxPlayers = 4 };
    127.                 PhotonNetwork.JoinOrCreateRoom("RG5",ro,TypedLobby.Default,null);  
    128.                          
    129.             }
    130.         }
    131.     }
    132.  
    133.     public override void OnJoinedRoom()
    134.     {      
    135.         Debug.Log("My Confirm Code is: " + (string)PhotonNetwork.LocalPlayer.CustomProperties["PlayerCC"]);
    136.         Debug.Log("4-State is: " + PhotonNetwork.NetworkClientState.ToString());
    137.         parents = PhotonNetwork.Instantiate("My_View", new Vector3(0, 0.5f, 0), Quaternion.identity, 0);
    138.         parents.transform.SetParent(source.transform);
    139.         pv = parents.GetComponent<PhotonView>();
    140.         pv.RPC("Create_Room", RpcTarget.AllBuffered,(string) PhotonNetwork.LocalPlayer.CustomProperties["PlayerRN"],(int) PhotonNetwork.LocalPlayer.CustomProperties["PlayerRS"],(string)PhotonNetwork.LocalPlayer.CustomProperties["PlayerRP"]);
    141.     }
    142.     [PunRPC]
    143.    public void Create_Room(string rn, int rs, string rp)
    144.     {
    145.         Debug.Log("Creating List Object Room For: " + My_New_Player.NickName);
    146.         GameObject go = (GameObject)Instantiate(Prefab);
    147.         go.transform.SetParent(trans, false);
    148.         txt = go.GetComponentsInChildren<Text>();
    149.         txt[0].text = rn;
    150.         txt[1].text = "0/" + rs.ToString();
    151.  
    152.     }
    153.     public override void OnPlayerEnteredRoom (Player newPlayer)
    154.     {
    155.      
    156.         print((string)PhotonNetwork.LocalPlayer.CustomProperties["PlayerCC"]);
    157.         print((string)newPlayer.CustomProperties["PlayerCC"]);
    158.         if(PhotonNetwork.PlayerList.Length==1) PhotonNetwork.SetMasterClient(newPlayer);                  
    159.  
    160.         if (((string)newPlayer.CustomProperties["PlayerCC"]) == ((string)PhotonNetwork.LocalPlayer.CustomProperties["PlayerCC"])&& PhotonNetwork.PlayerList.Length>1)
    161.         {          
    162.             Debug.Log("Player Entered The Room: " + newPlayer.NickName);
    163.             Create_Room_Is_OK = true;
    164.             //My_New_Player = newPlayer;
    165.              // PhotonNetwork.LeaveRoom();
    166.             // The easiest thing is probably to call an RPC on the newPlayer object that sets the value of his bool.
    167.         }
    168.     }
    169.  
    170.     public override void OnLeftRoom()
    171.     {
    172.         if(Create_Room_Is_OK==true)
    173.         {
    174.             if (check_if_Room_name_Not_Present(Room_Name.text) == false)
    175.             {
    176.                 GameObject go = (GameObject)Instantiate(Prefab);
    177.                 go.transform.SetParent(trans, false);
    178.                 txt = go.GetComponentsInChildren<Text>();
    179.                 txt[0].text = Load_Rooms.ClientRoomName;
    180.                 txt[1].text = "0/" + Load_Rooms.ClientRoomSize.ToString();
    181.  
    182.                 Debug.Log(txt[0].text);
    183.                 Debug.Log(txt[1].text);
    184.  
    185.                 //RoomInfo rmf;
    186.                 _RoomsList rm = new _RoomsList();
    187.                 rm.RoomName = Load_Rooms.ClientRoomName;
    188.  
    189.                 rm.RoomSize = int.Parse(Load_Rooms.ClientRoomSize.ToString());
    190.                 rm.RoomPassword = Load_Rooms.ClintRoomPassword;
    191.                 rm.Room_Coast_Per_one = (Load_Rooms.ClientRoomCoast);
    192.                 rm.RoomConfirmCode = Load_Rooms.ClientConfirmCode;
    193.                 //rm.Room_Is_Active = true;
    194.                 //rm.Room_Is_Private = false;
    195.                 //RoomOptions ro = new RoomOptions() { IsVisible = true, IsOpen = true, MaxPlayers = (byte)int.Parse(Room_Size.text) };
    196.                 //PhotonNetwork.CreateRoom(Room_Name.text, ro, TypedLobby.Default, null);
    197.                 Debug.Log("had creat a room with name: " + Room_Name.text);
    198.                 First_Connection.MC.Rooms.Add(rm);
    199.                 // First_Connection.MC.rooms
    200.                 First_Connection.Save();
    201.             }
    202.         }
    203.         Go_To_Rooms_Screen();
    204.     }
    205.     bool check_if_Room_name_Not_Present(string name)
    206.     {
    207.         if (First_Connection.MC.Rooms == null) return false;
    208.         for (int i = 0; i < First_Connection.MC.Rooms.Count; i++)
    209.         {
    210.             if (First_Connection.MC.Rooms[I].RoomName == name)
    211.             {
    212.                 First_Connection.MC.Rooms[I].RoomName = Room_Name.text;
    213.                 First_Connection.MC.Rooms[I].RoomSize = int.Parse(Room_Size.text);
    214.                 First_Connection.MC.Rooms[I].RoomPassword = Room_Password.text;
    215.                 First_Connection.MC.Rooms[I].RoomConfirmCode = Room_Confirm_Code.text;
    216.                 First_Connection.MC.Rooms[I].Room_Coast_Per_one = int.Parse(Room_Coast.text);
    217.  
    218.                 //First_Connection.MC.Rooms[I].Room_Is_Active = true;
    219.                 //First_Connection.MC.Rooms[I].Room_Is_Private = false;
    220.                 First_Connection.Save();
    221.                 Go_To_Rooms_Screen();
    222.                 return true;
    223.             }
    224.         }
    225.         return false;
    226.     }
    227.  
    228.     public void Go_To_Rooms_Screen()
    229.     {
    230.         Start_Screen.SetActive(false);
    231.         Room_Screen.SetActive(true);
    232.         Room_Setup.SetActive(false);
    233.     }
    234. }
    [/I][/I][/I][/I][/I][/I][/I][/I]
     
    Last edited: Jul 22, 2019
  14. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,753
    @Livindaki
    To make code easier to read, you can use Code Tags (you can edit post).
    Using code tags properly
    This will allow us easier to follow your script and also adds lines numbers.
     
    Munchy2007 likes this.