Hi boys. I think that my problem is with RPC calls! I’m not able to assign the correct name to each player!
I’m trying now for weeks
Can you tell me solution’s/tutorial/script?
Thanks.
I post a screenshot
Hi boys. I think that my problem is with RPC calls! I’m not able to assign the correct name to each player!
I’m trying now for weeks
Can you tell me solution’s/tutorial/script?
Thanks.
I post a screenshot
Perhaps I need create something like “Player list”…but I don’t how to do help please
There is no tutorial on it and you don’t need a list.
It can be achieved through RPC.
ok! I have a server and a client! Before click “connect” (in server or client both) I must enter my playerrname in a gui.textfiel so I’ve memorized my player name into a variable into my principal Script. When I click connect another thing happens: A Cube (the player) is istantiate.
Now I’ve write a script that create a GUI.Label above the cube in way show his name! In local all work good! But I can’t see remote player’s name! How to pass remote Player name at his cube in my game istance? Can you help me?
where are you sending the RPC ?
To who are you sending it ?
Is the script attached to the object that also has the networkView ?
Can you post your code ?
I’m using the script “chat” of M2H’s Networking Tutorial:
//#pragma strict
var lettere:boolean=false;
public var usingChat : boolean = false; //Can be used to determine if we need to stop player movement since we're chatting
var skin : GUISkin; //Skin
var showChat : boolean= false; //Show/Hide the chat
public static var SP : Chat;
//Private vars used by the script
private var inputField : String= "";
private var scrollPosition : Vector2;
private var width : int= 500;
private var height : int= 180;
var playerName : String;
private var lastUnfocusTime : float =0;
private var window : Rect;
//Server-only playerlist
private var playerList = new ArrayList();
class PlayerNode {
var playerName : String;
var networkPlayer : NetworkPlayer;
}
private var chatEntries = new ArrayList();
class ChatEntry
{
var name : String= "";
var text : String= "";
}
function Awake(){
window = Rect(Screen.width/2-width/2, Screen.height-height+5, width, height);
SP = this;
//We get the name from the masterserver example, if you entered your name there ;).
playerName = PlayerPrefs.GetString("playerName", "");
}
//Client function
function OnConnectedToServer() {
ShowChatWindow();
networkView.RPC ("TellServerOurName", RPCMode.Server, playerName);
}
//Server function
function OnServerInitialized() {
ShowChatWindow();
var newEntry : PlayerNode = new PlayerNode();
newEntry.playerName=playerName;
newEntry.networkPlayer=Network.player;
playerList.Add(newEntry);
addGameChatMessage(playerName+" è entrato nella chat!");
}
//A handy wrapper function to get the PlayerNode by networkplayer
function GetPlayerNode(networkPlayer : NetworkPlayer){
for(var entry in playerList){
if((entry as PlayerNode).networkPlayer==networkPlayer){
return entry;
}
}
}
//Server function
function OnPlayerDisconnected(player: NetworkPlayer) {
addGameChatMessage("Un giocatore si è disconesso");
//Remove player from the server list
playerList.Remove( GetPlayerNode(player) );
}
function OnDisconnectedFromServer(){
CloseChatWindow();
}
@RPC
//Sent by newly connected clients, recieved by server
function TellServerOurName(name : String, info : NetworkMessageInfo){
var newEntry : PlayerNode = new PlayerNode();
newEntry.playerName=name;
newEntry.networkPlayer=info.sender;
playerList.Add(newEntry);
GameObject.Find("nomeTesta").GetComponent("GUIText").text = name;///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
addGameChatMessage(name+" è entrato nella chat");
var nome : vasco = GameObject.Find("Biglia(Clone)").GetComponent("vasco");
nome.pino=name;
}
function CloseChatWindow ()
{
showChat = false;
inputField = "";
//chatEntries = new ArrayList(); ///////////evita di cancellare i vecchi messaggi
}
function ShowChatWindow ()
{
showChat = true;
inputField = "";
//chatEntries = new ArrayList(); ///////////evita di cancellare i vecchi messaggi
}
function OnGUI ()
{
var con2 : MiglioramentoDiUnServerDiBaseV3 = GameObject.Find("Base").GetComponent("MiglioramentoDiUnServerDiBaseV3");
var con3 : MiglioramentoDiUnClientDiBaseV3 = GameObject.Find("Base").GetComponent("MiglioramentoDiUnClientDiBaseV3");
if(con2.connesso||con3.connesso){
if(showChat){
if (GUI.Button (Rect (660, 550, 120, 30), "Nascondi Chat")) {
//CloseChatWindow ();
showChat=false;
}
}
}
if(con2.connesso||con3.connesso){
if(!showChat){
if (GUI.Button (Rect (660, 550, 120, 30), "Mostra Chat")) {
ShowChatWindow ();
//showChat=true;
}
}
}
var con : MiglioramentoDiUnServerDiBaseV3 = GameObject.Find("Base").GetComponent("MiglioramentoDiUnServerDiBaseV3");
var con1 : MiglioramentoDiUnClientDiBaseV3 = GameObject.Find("Base").GetComponent("MiglioramentoDiUnClientDiBaseV3");
if(con.connesso==false){
if(con1.connesso==false){
GUI.Label (Rect (25, 60, 150, 30), "Inserisci il tuo nome");
playerName= GUI.TextField (Rect (25, 85, 190, 30), playerName);
}
}
if (playerName==""){
lettere=false;
}
else
lettere=true;
if(!showChat){
return;
}
GUI.skin = skin;
if (Event.current.type == EventType.keyDown Event.current.character == "\n" inputField.Length <= 0)
{
if(lastUnfocusTime+0.25<Time.time){
usingChat=true;
GUI.FocusWindow(5);
GUI.FocusControl("Chat input field");
}
}
window = GUI.Window (5, window, GlobalChatWindow, "");
}
function GlobalChatWindow (id : int) {
GUILayout.BeginVertical();
GUILayout.Space(10);
GUILayout.EndVertical();
// Begin a scroll view. All rects are calculated automatically -
// it will use up any available screen space and make sure contents flow correctly.
// This is kept small with the last two parameters to force scrollbars to appear.
scrollPosition = GUILayout.BeginScrollView (scrollPosition);
for (var entry in chatEntries)
{
GUILayout.BeginHorizontal();
if((entry as ChatEntry).name==""){//Game message
GUILayout.Label ((entry as ChatEntry).text);
}else{
GUILayout.Label ((entry as ChatEntry).name+": "+(entry as ChatEntry).text);
}
GUILayout.EndHorizontal();
GUILayout.Space(1); /////////////////////////////Originale era 30! cambie le distanze tra le righe
}
// End the scrollview we began above.
GUILayout.EndScrollView ();
if (Event.current.type == EventType.keyDown Event.current.character == "\n" inputField.Length > 0)
{
HitEnter(inputField);
}
GUI.SetNextControlName("Chat input field");
inputField = GUILayout.TextField(inputField);
if(Input.GetKeyDown("mouse 0")){
if(usingChat){
usingChat=false;
GUI.UnfocusWindow ();//Deselect chat
lastUnfocusTime=Time.time;
}
}
}
function HitEnter(msg : String){
msg = msg.Replace("\n", "");
networkView.RPC("ApplyGlobalChatText", RPCMode.All, playerName, msg);
inputField = ""; //Clear line
GUI.UnfocusWindow ();//Deselect chat
lastUnfocusTime=Time.time;
usingChat=false;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@RPC
function ApplyGlobalChatText (name : String, msg : String)
{
var entry = new ChatEntry();
entry.name = name;
entry.text = msg;
chatEntries.Add(entry);
//Remove old entries
if (chatEntries.Count > 14){
chatEntries.RemoveAt(0);
}
scrollPosition.y = 1000000;
}
//Add game messages etc
function addGameChatMessage(str : String){
ApplyGlobalChatText("", str);
if(Network.connections.length>0){
networkView.RPC("ApplyGlobalChatText", RPCMode.Others, "", str);
}
}
and this is the script of the playerPrefab
public var skin:GUISkin;
var[COLOR="red"] playerName[/COLOR]:String="";
function OnGUI(){
GUI.skin = skin;
var nome : Chat = GameObject.Find("Base").GetComponent("[COLOR="red"]playerName[/COLOR]");
var content:GUIContent=new GUIContent(pino);
var textSize:Vector2=skin.GetStyle("Box").CalcSize(content);
var bubblePos:Vector3=transform.position + new Vector3(-0.5, 1.5, 0);
var screenPos:Vector3=Camera.main.WorldToScreenPoint(bubblePos);
if (screenPos.x >= 0 screenPos.x <= Screen.width screenPos.y >= 0 screenPos.y <= Screen.height screenPos.z >= 0 ) {
var pos:Vector2=GUIUtility.ScreenToGUIPoint(new Vector2(screenPos.x, Screen.height - screenPos.y));
GUI.Box(new Rect(pos.x, pos.y, textSize.x + 5, textSize.y + 20), content);
//}
}
}
My playerPrefab has the NetworkView.
How to send the last player’s name to the playerprefab script?
Ok, i’m not going to start debugging your complete script…
Please post the RPC relative info only
Are you trying authoritative or not ?
no is not authoritative .
function OnConnectedToServer() {
ShowChatWindow();
networkView.RPC ("TellServerOurName", RPCMode.Server, playerName);
}
@RPC
//Sent by newly connected clients, recieved by server
function TellServerOurName(name : String, info : NetworkMessageInfo){
var newEntry : PlayerNode = new PlayerNode();
newEntry.playerName=name;
newEntry.networkPlayer=info.sender;
playerList.Add(newEntry);
GameObject.Find("nomeTesta").GetComponent("GUIText").text = name;///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
addGameChatMessage(name+" è entrato nella chat");
var nome : vasco = GameObject.Find("Biglia(Clone)").GetComponent("vasco");
nome.pino=name;
}
I think I must use this part of “chat” script to do it!
well yes you are trying authoritative since the server spawns the objects and your RPC sends to the server.
Is that what you want or not ?
yes I want it
Ok then you need to make sure you send the RPC from the controller script the player owns.
You can make it authoritative and send to the server and then let the server update all clients through an RPC or send to all clients directly.
you is right but I’m trying so many things and I can not. if you can I would take a practical example! you are the only one who is helping me for months, partly because my English disgusts
In your player controller script there is an RPC function that gets called to enable the script.
Find that one and add this to the enable part :
networkView.RPC ("UpdateClientLabels", RPCMode.AllBuffered, "MYUSERNAMEHERE");
in your labels script thats attached to the same gameobject where your controller is add this function :
@RPC
function UpdateClientLabels (label:String) {
labelText = label; // labelText is your text for the label
}
Thats it.
Oh yes!! Thanks!!! It work!!