0
\$\begingroup\$

I got help from someone on making a turn-based button but so far I am not able to prevent the button from getting the next player when a certain bool is activated in the room.

Initially, the SetActivePlayer was called without the if-condition, I later adjusted the script to prevent the button to move to the next player when a certain condition is met.

I did call some debug.logs but I get different results since it is able to go to the next player!

What I had in mind is Getting the current player's ID(Actor Number) where when the bool is true then SetActive the current player. (My idea lies here hope I get some help!)

private void Awake()
{
    button.onClick.AddListener(TurnButton):
    button.gameObject.SetActive(false);

    Player_Name.text = "Connecting ...";

    // Store the current room
    room = PhotonNetwork.CurrentRoom;

    if (PhotonNetwork.IsMasterClient)
    {
        // As master go active since usually this means you are the first player in this room

        // Get your own ID
        var myId = PhotonNetwork.LocalPlayer.ActorNumber;
        // and se it to active
        SetActivePlayer(myId);
    }
    else
    {
        // Otherwise fetch the active player from the room properties
        OnRoomPropertiesUpdate(room.CustomProperties);
    }
}
public override void OnRoomPropertiesUpdate(Hashtable propertiesThatChanged)
{
    // Maybe another property was changed but not the one we are interested in
    if(!propertiesThatChanged.TryGetValue(ACTIVE_PLAYER_KEY, out var newActiveID)) return;

    // if we got a value but it's not an int something is wrong in general
    if(!(newActiveID is int newActvieIDValue))
    {
        Debug.LogError("For some reason \"ACTIVE_PLAYER_KEY\" is not an int!?");
        return;
    }

    // otherwise apply the ID
    ApplyActivePlayer(newActvieIDValue);
}

//on click
public void TurnButton()
{
    // this gets the next player after you sorted by the actor number (=> order they joined the room)
    // wraps around at the end
    var nextPlayer = PhotonNetwork.LocalPlayer.GetNext(); //should only happen when isshot is false

    // Get the id
    var nextPlayerID = nextPlayer.ActorNumber;

    var currentPlayer = PhotonNetwork.LocalPlayer.ActorNumber;

    if(room.CustomProperties.ContainsKey(IS_SHOT_KEY) && (bool)room.CustomProperties[IS_SHOT_KEY])
    {
        //when the bool is set to true then
        //Don't move to the next player rather break the cycle and stay in the current player
        //return the current player
    }
    else
    {
        SetActivePlayer(nextPlayerID);
    }

    LuckyBastard.SetActive(true);
    Debug.Log(PhotonNetwork.CurrentRoom.CustomProperties.ToStringFull());
}

private void SetActivePlayer(int id)
{
    var hash = new ExitGames.Client.Photon.Hashtable();
    hash[ACTIVE_PLAYER_KEY] = id;
    room.SetCustomProperties(hash);
}

// this applies all local changes according to the active player
private void ApplyActivePlayer(int id)
{
    // get the according player
    var activePlayer = Player.Get(id);

    // Am I this player?
    var iAmActive = PhotonNetwork.LocalPlayer.ActorNumber == id;

    // Set the button active/inactive accordingly
    button.gameObject.SetActive(iAmActive);

    // Set the text accordingly
    Player_Name.text = string.Format(iAmActive ? ACTIVE_ME_FORMAT : ACTIVE_OTHER_FORMAT, activePlayer.NickName):
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.