Search Unity

Third Party PUN2 - int for player index

Discussion in 'Multiplayer' started by Divis10nStudios, Oct 24, 2021.

  1. Divis10nStudios

    Divis10nStudios

    Joined:
    Dec 26, 2017
    Posts:
    197
    Hi everyone, I want to get the player index from a list of players
    I currently have this

    Code (CSharp):
    1. Player[] players = PhotonNetwork.PlayerList;
    is there anything along the lines of
    Code (CSharp):
    1. int playerIndex = players.GetPlayerIndex;
    or anything along those lines that I can do? or anything built into unity that I can do to return the "playerIndex" as an integer?
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,067
    Each player has an ActorNumber. As long as nobody leaves, these simply go up from 1.
    But, as soon as one player left, there will be gaps in the ActorNumber. So if you index based on the ActorNumber, the index gets tossed up whenever someone leaves.
    PlayerNumbering.cs is included in PUN and should help assign a playerNumer / index, which doesn't change.
    Have a look at it's code. The comments explain it.
     
  3. Divis10nStudios

    Divis10nStudios

    Joined:
    Dec 26, 2017
    Posts:
    197
    Yes, managed to get a system working with actornumbers instead, Thank you. I've run into another issue.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5. using Photon.Realtime;
    6.  
    7. public class PlayerAnimator : MonoBehaviourPunCallbacks
    8. {
    9.     public Animator anim;
    10.     public PhotonView PV;
    11.     private float x;
    12.     private float y;
    13.  
    14.     void Start()
    15.     {
    16.  
    17.     }
    18.  
    19.     void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    20.     {
    21.         if (stream.IsWriting)
    22.         {
    23.             Debug.Log("Writing");
    24.             stream.SendNext(x);
    25.             stream.SendNext(y);
    26.         }
    27.         else if (stream.IsWriting)
    28.         {
    29.             Debug.Log("Receiving");
    30.             x = (float)stream.ReceiveNext();
    31.             y = (float)stream.ReceiveNext();
    32.  
    33.         }
    34.  
    35.         anim.SetFloat("MoveY", y);
    36.         anim.SetFloat("MoveX", x);
    37.     }
    38.  
    39.     void Update()
    40.     {
    41.         if (PV.IsMine)
    42.         {
    43.             x = Input.GetAxis("Horizontal");
    44.             y = Input.GetAxis("Vertical");
    45.         }
    46.  
    47.     }
    48. }
    49.  
    Do you know why this stream isn't writing? The animations are fine in first person but don't seem to sync over the network. Sorry for just posting it here like this, from browsing for answers I noticed that you've helped a lot of people and wanted to give it a shot here : (
     
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,067
    The PUN side of this is working, as soon as you are able to send x and y and receive it. That's all where PUN is involved. Why the results are not what you expect ... I won't be able to help with unless I happen to know (and the answer is short).

    One point to keep in mind: OnPhotonSerializeView is not called as often as Update.
    Also, I don't know if it's enough to feed x,y into the animation 10x per second. It's not going to be precise.
    Did you have a look at the PhotonAnimationView component?
     
  5. Divis10nStudios

    Divis10nStudios

    Joined:
    Dec 26, 2017
    Posts:
    197

    Everything I need to sync has been set up here. It works locally, just not over the network.
    Do I have to initiate the stream writing and receiving somewhere? Because debug.log does not print "Writing" or "receiving"
     
  6. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,067
    The PhotonAnimatorView and PhotonAnimator sound as if they'd do about the same?! Hope these don't clash.

    Yes, any IPunObservable script needs to go into a PhotonView's Observed list.
     
  7. Divis10nStudios

    Divis10nStudios

    Joined:
    Dec 26, 2017
    Posts:
    197
    It's all in the photon view
     
  8. Divis10nStudios

    Divis10nStudios

    Joined:
    Dec 26, 2017
    Posts:
    197
    Nevermind, it just started working now for some reason, I was working on some unrelated code and it fixed itself somehow, thanks for the help :D