How to I get a random player

iamE
iamE
I want to get some random players in a room at the start of a new round, but I don't know about to pick random players. Can someone help, please?

Comments

  • One of forum search result for 'random player': http://forum.exitgames.com/viewtopic.php?f=17&t=5365&p=20399&hilit=random+player#p20399
    Is it what you are looking for?
  • Please do the Marco Polo Tutorial. It uses a very simple random matchmaking workflow.
    http://doc.exitgames.com/en/pun/current ... marco-polo
  • vadim wrote:
    One of forum search result for 'random player': http://forum.exitgames.com/viewtopic.php?f=17&t=5365&p=20399&hilit=random+player#p20399
    Is it what you are looking for?
    That's a earlier post of mine. I didn't know what you mean't. I don't see how you could select a player using integers.
  • Then asking for clarification in old post is better option.

    Use [] to take array element with given index:
    int i = random(PhotonNetwork.playerList.Length)
    PhotonPlayer randomPlayer = PhotonNetwork.playerList

    Where 'random' is function of your choice returning random integer from [0, PhotonNetwork.playerList.Length) range
  • vadim wrote:
    Then asking for clarification in old post is better option.

    Use [] to take array element with given index:
    int i = random(PhotonNetwork.playerList.Length)
    PhotonPlayer randomPlayer = PhotonNetwork.playerList

    Where 'random' is function of your choice returning random integer from [0, PhotonNetwork.playerList.Length) range
    I'm getting an error that says that random doesn't exist in the current context. All I'm trying to do is select a random player and put a tag on him. I don't see why this has to be so complicated.
  • This is a simple example of one way to accomplish the task.

    using System.Collections.Generic;
    public List<string> PlayerList = new List<string>();
    
    We can call PlayerConnected
    void OnPhotonPlayerConnected(){
    				if (!PlayerList.Contains (PhotonNetwork.playerName)) {
    					    PlayerList.Add (PhotonNetwork.playerName);
    				}
    		}
    
    Next We Need A Pick Random Method to Invoke
    void TagRandomPlayer(){
    		int PlayerListRange = PlayerList.Count;
    		System.Random Rand = new System.Random ();
    		int RandomPick = Rand.Next (0, PlayerListRange);
    		string TaggedPlayer = PlayerList [RandomPick];
    		}
    
  • iamE
    iamE
    edited November 2014
    This is a simple example of one way to accomplish the task.

    using System.Collections.Generic;
    public List<string> PlayerList = new List<string>();
    
    We can call PlayerConnected
    void OnPhotonPlayerConnected(){
    				if (!PlayerList.Contains (PhotonNetwork.playerName)) {
    					    PlayerList.Add (PhotonNetwork.playerName);
    				}
    		}
    
    Next We Need A Pick Random Method to Invoke
    void TagRandomPlayer(){
    		int PlayerListRange = PlayerList.Count;
    		System.Random Rand = new System.Random ();
    		int RandomPick = Rand.Next (0, PlayerListRange);
    		string TaggedPlayer = PlayerList [RandomPick];
    		}
    
    How do I apply the tag to the picked player? Do I turn the String TaggedPlayer into a gameObject instead a string so I can use .tag or will that not work? Ty for the help.
  • Make sure you add
    using System.Collections.Generic;
    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    public class ClassName : MonoBehaviour {}
  • Make sure you add
    using System.Collections.Generic;
    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    public class ClassName : MonoBehaviour {}
    Yah I solved that already
  • iamE wrote:
    How do I apply the tag to the picked player? Do I turn the String TaggedPlayer into a gameObject instead a string so I can use .tag or will that not work? Ty for the help.

    This is where we start to leave scope of the original problem.
    string TaggedPlayer, is your Random player you wanted. Now we need to do something with it, What that is only you know.

    you could maybe cast something like this.
    .GetComponent<PhotonView> ().owner & Match that with TaggedPlayer
    At that point, you could invoke the method you want.
    i.e (Mark PlayerObject)
  • iamE wrote:
    How do I apply the tag to the picked player? Do I turn the String TaggedPlayer into a gameObject instead a string so I can use .tag or will that not work? Ty for the help.

    This is where we start to leave scope of the original problem.
    string TaggedPlayer, is your Random player you wanted. Now we need to do something with it, What that is only you know.

    you could maybe cast something like this.
    .GetComponent<PhotonView> ().owner & Match that with TaggedPlayer
    At that point, you could invoke the method you want.
    i.e (Mark PlayerObject)
    System.String does not contain a definition for GetComponent.
  • iamE wrote:
    How do I apply the tag to the picked player? Do I turn the String TaggedPlayer into a gameObject instead a string so I can use .tag or will that not work? Ty for the help.

    This is where we start to leave scope of the original problem.
    string TaggedPlayer, is your Random player you wanted. Now we need to do something with it, What that is only you know.

    you could maybe cast something like this.
    .GetComponent<PhotonView> ().owner & Match that with TaggedPlayer
    At that point, you could invoke the method you want.
    i.e (Mark PlayerObject)
    Can you please help. I can't do anything with it because its a string
  • iamE wrote:
    How do I apply the tag to the picked player? Do I turn the String TaggedPlayer into a gameObject instead a string so I can use .tag or will that not work? Ty for the help.

    This is where we start to leave scope of the original problem.
    string TaggedPlayer, is your Random player you wanted. Now we need to do something with it, What that is only you know.

    you could maybe cast something like this.
    .GetComponent<PhotonView> ().owner & Match that with TaggedPlayer
    At that point, you could invoke the method you want.
    i.e (Mark PlayerObject)
    Can you explain to me how to use GetComponent on TaggedPlayer