Search Unity

Help Wanted Help IN RPC Methods PhotoPUN2

Discussion in 'Multiplayer' started by caiowgcw, Nov 28, 2020.

  1. caiowgcw

    caiowgcw

    Joined:
    Jun 5, 2019
    Posts:
    11
    I am doing a UI, that when a players enter o room, he chooses a slot by clicking on button and spawning on it, but when i click on the button it only disable for localplayer, i want that it disable for all players in room, i know that i doing wrongs methods with RPC, but i need a advice or a help to go ahead, heres my code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5. using TMPro;
    6. using System.IO;
    7. using Hashtable = ExitGames.Client.Photon.Hashtable;
    8. using Photon.Realtime;
    9.  
    10. public class SpawnManager : MonoBehaviourPunCallbacks
    11. {
    12.     public static SpawnManager Instance;
    13.     private PhotonView PV;
    14.     private bool[] isSlotOccuped;
    15.    
    16.  
    17.     public Transform[] slots;
    18.     public GameObject[] slotsButtons;
    19.     public int slotsActual;
    20.     public void Awake()
    21.     {
    22.         if(SpawnManager.Instance == null)
    23.         {
    24.             SpawnManager.Instance = this;
    25.         }
    26.     }
    27.     public void Start()
    28.     {
    29.         isSlotOccuped = new bool[PhotonNetwork.PlayerList.Length];
    30.         ChangeSizeSlots();
    31.     }
    32.     public void Update()
    33.     {
    34.        
    35.     }
    36.     public void ChangeSizeSlots()
    37.     {
    38.  
    39.         slotsActual = PhotonNetwork.PlayerList.Length;
    40.  
    41.         for (int i = 0; i < slotsActual; i++)
    42.         {
    43.             slotsButtons[i].SetActive(true);
    44.             isSlotOccuped[i] = false;
    45.  
    46.         }
    47.  
    48.     }
    49.  
    50.     void TakeSlot(int slotIndex)
    51.     {
    52.         PV = GetComponent<PhotonView>();
    53.         PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "Player"), SpawnManager.Instance.slots[slotIndex].position, Quaternion.identity);
    54.         Debug.Log(slotIndex);
    55.         slotsButtons[slotIndex].SetActive(false);
    56.         isSlotOccuped[slotIndex] = true;
    57.         PV.RPC("VerifySlot", RpcTarget.AllBuffered, slotsButtons[slotIndex]);
    58.  
    59.     }
    60.     public override void OnPlayerPropertiesUpdate(Player targetPlayer, Hashtable changedProps)
    61.     {
    62.  
    63.     }
    64.  
    65.     [PunRPC]
    66.     void VerifySlot(GameObject slotIndex)
    67.     {
    68.         slotIndex.SetActive(false);
    69.     }
    70.  
    71.  
    72. }
    73.  
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,710
    This approach sounds simple but is tricky in the details: What if 2 players choose the same slot at the same time?

    You can use custom room properties for this and CAS. When you create the room, the creating player should set properties for the slots. Maybe "s1" to "s4 or so. Set the values to 0, meaning: Not taken by a player.
    Each player can then attempt to set one of those slot properties from 0 to their ActorNumber.

    Read:
    https://doc.photonengine.com/en-us/pun/v2/gameplay/synchronization-and-state#custom_properties

    With the condition, the effect is that the property only changes if the slot is not yet taken.

    You can disable your UI based on the properties, which are synced.
     
    caiowgcw likes this.
  3. caiowgcw

    caiowgcw

    Joined:
    Jun 5, 2019
    Posts:
    11
    Thank you for the advice, i got it and worked
     
    tobiass likes this.
unityunity