- Home /
How to get player id's from owners name? Pun network
so what i have is an object in the scene that sets the name of the player (the player sets their name say "Bob") and i get a string and int from a code the player enters in the chat, eg "/speed, 20"
and then i need to set an object that the player controls to the owners name, how do i do this?
what i have so far : commands script-
using UnityEngine;
using System.Collections;
public class Commands : Photon.MonoBehaviour {
private float i = 0.0F;
private string name;
GameObject player;
RigidbodyFPSController controllerScript;
// Use this for initialization
public void Command(string com){
Debug.Log (com);
string[] splitstring = com.Split (',');
if (splitstring.Length > 0){
Debug.Log (splitstring [1]);
}
i = float.Parse(splitstring [1]);
if (splitstring[0] == "/speed" && i > 0 || i < 1000 ) {
Debug.Log(name + "" + splitstring[0] + " " + i);
//controllerScript = player.GetComponent<RigidbodyFPSController>();
//controllerScript.speed = i;
}
}
public void Player(string PlayerUserName){
name = PlayerUserName;
//player = PhotonView.Find (PlayerUserName);
}
}
Chat code-
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(PhotonView))]
public class InRoomChat : Photon.MonoBehaviour
{
public Rect GuiRect = new Rect(0,0, 250,300);
public bool IsVisible = true;
public bool AlignBottom = false;
public List<string> messages = new List<string>();
private string inputLine = "";
private Vector2 scrollPos = Vector2.zero;
public Commands com;
public static readonly string ChatRPC = "Chat";
public void Start()
{
if (this.AlignBottom)
{
this.GuiRect.y = Screen.height - this.GuiRect.height;
}
}
public void OnGUI()
{
com = GetComponent<Commands>();
if (!this.IsVisible || PhotonNetwork.connectionStateDetailed != PeerState.Joined)
{
return;
}
if (Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return))
{
if (!string.IsNullOrEmpty(this.inputLine))
{
if (inputLine[0] == '/'){
com.Command(inputLine);
this.inputLine = "";
GUI.FocusControl("");
return;
}
else {
this.photonView.RPC("Chat", PhotonTargets.All, this.inputLine);
this.inputLine = "";
GUI.FocusControl("");
return; // printing the now modified list would result in an error. to avoid this, we just skip this single frame
}}
else
{
GUI.FocusControl("ChatInput");
}
}
GUI.SetNextControlName("");
GUILayout.BeginArea(this.GuiRect);
scrollPos = GUILayout.BeginScrollView(scrollPos);
GUILayout.FlexibleSpace();
for (int i = messages.Count - 1; i >= 0; i--)
{
GUILayout.Label(messages[i]);
}
GUILayout.EndScrollView();
GUILayout.BeginHorizontal();
GUI.SetNextControlName("ChatInput");
inputLine = GUILayout.TextField(inputLine);
if (GUILayout.Button("Send", GUILayout.ExpandWidth(false)))
{
this.photonView.RPC("Chat", PhotonTargets.All, this.inputLine);
this.inputLine = "";
GUI.FocusControl("");
}
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
[RPC]
public void Chat(string newLine, PhotonMessageInfo mi)
{
string senderName = "anonymous";
if (mi != null && mi.sender != null)
{
if (!string.IsNullOrEmpty(mi.sender.name))
{
senderName = mi.sender.name;
com.Player(mi.sender.name);
}
else
{
senderName = "player " + mi.sender.ID;
}
}
this.messages.Add(senderName +": " + newLine);
}
public void AddLine(string newLine)
{
this.messages.Add(newLine);
}
}
thanks in advance
Please format your code in the future, i've gone ahead and gone it for you, but please see the screens below, if unformatted... well it's a mess and you WILL get less responses from users.
Unformatted:
Formatted:
i tried to add it into the code thing, but it wouldn't format it, the other script done it, was weird
@killa247, i find it easier to paste the code in, then highlight it in the textarea then click on the 101010 button, this seems to be more accurate then click 101010, pasting it in the popup and hitting ok
Answer by AlucardJay · Sep 11, 2014 at 05:38 AM
PhotonNetwork.playerName will return the name of the local player.
PhotonPlayer.name will also return the name of the local player.
PhotonNetwork.player.name will also return the name of the local player.
PhotonNetwork.player.ID will return the ID of the local player.
PhotonNetwork.player.isLocal will return if the player is the local player.
Debug.Log ( PhotonNetwork.playerName + " : " + PhotonNetwork.player.name + " : " + PhotonNetwork.player.ID + " : " + PhotonNetwork.player.isLocal );
PhotonNetwork.player returns the class PhotonPlayer
http://doc-api.exitgames.com/en/realtime/current/pun/doc/class_photon_player.html
but how do i set the object that the player is using to the one in game? i want to change the speed variable of the player he controls
if ( photonView.is$$anonymous$$ine )
DoStuff();
or send a RPC.
Normally when I NetworkInstantiate an object, I store a reference to that object in my PlayerScript, so things like this are not a problem :
GameObject playerObject = (GameObject)PhotonNetwork.Instantiate( "PlayerPrefab", startPos, startRot, 0 );
ObjectController objectController = playerObject.GetComponent< ObjectController >();
objectController.photonView.RPC( "SetName", PhotonTargets.AllBuffered, PhotonNetwork.playerName );
is it possible to skype you? it could make this allot easier, and i would understand allot faster.
i dont have that right now when players are Instantiated
add me "Juane.gray" if you can please ^_^
whoops i was being a tad retarded disregard that previous message, i just did the reference thing, way more obvious, thanks. :)
Your answer

Follow this Question
Related Questions
List of string is duplicated [C#] 1 Answer
PlayerName in Chat Window 2 Answers
Network.player giving me 0 0 Answers
Photon MMO chat 0 Answers
My Voice Chat Script Don't work 1 Answer