x


Textmesh Username for networkplayers. help.

Hello im looking for a solution, so far i have managed to use a textmesh component that is attached to and object, which is a child of the instantiated prefab, therefore each user has this textmesh object. I have edited code based on chat scripts to make arrays of all users. This script works for two connections, both can see eachothers names above thier avatars head but when the third connects serverside remote 1 and 2 swap usernames and on clients remote 1 & 2 have the same username, lookin for advanced advice on how i can make this work. Thanks.

Namespace..

public var UserName: GameObject;
public var Cam: GameObject;
public var player : NetworkPlayer;
public var username : String;
public var awakeName: String;
public var remoteUsers : int;
public var remoteInt : int = 1;
public var userList = new Array();
public var userName: String;

class UserNode{
var userName : String;
var networkPlayer : NetworkPlayer;
} //

body..

function Awake () {
userName = ConnectGui.playerNameInput; // this is a PlayerPrefs.GetString in ConnectGui script.
awakeName = userName;

if(networkView.isMine){
    gameObject.tag = "LocalName";
    gameObject.GetComponentInChildren(TextMesh).text = awakeName;
}

    if(!networkView.isMine){
    gameObject.tag = "RemoteName";
}

    RemoteNameInst (); //renames this object from username to unsername1 ...2 ...3 etc.

    if(Network.isClient || Network.isServer){
    networkView.RPC ("TellOurUser", RPCMode.OthersBuffered, awakeName);
}

networkView.RPC("MeshName", RPCMode.OthersBuffered, awakeName);}

    function Update () {

remoteUsers = userList.length;  
UserName = GameObject.FindWithTag("LocalName");
UserName.transform.localPosition = Vector3(0, 2, 0);
Cam = GameObject.FindWithTag("MainCamera");
var direction : Vector3 = Cam.transform.position - UserName.transform.position;
transform.LookAt(UserName.transform.position - direction);

    }

Other functions ...

    function OnPlayerDisconnected(player: NetworkPlayer) {
        userList.Remove(GetUserNode(player));
    }
    function OnNetworkInstantiate (info : NetworkMessageInfo){
        remoteInt +=1;
        RemoteNameInst ();
    }
    @RPC
    //Sent by newly connected clients, recieved by server
    function TellOurUser(name : String, info : NetworkMessageInfo){
        var newEntry : UserNode = new UserNode();
        newEntry.userName=name;
        newEntry.networkPlayer=info.sender;
        userList.Add(newEntry);
    }

    @RPC
    function MeshName (name : String){

        for(var entry : UserNode in userList){
           if(entry.userName==name){
              LocalUserName (name); // function bellow changes remotes username textmesh.name
           }
        }
    }

    function GetUserNode(networkPlayer : NetworkPlayer){
         for(var entry : UserNode in  userList){
             if(entry.networkPlayer==networkPlayer){
                return entry;
             }
         }
         Debug.LogError("GetUserNode: Requested a playernode of non-existing player!");
         return null;
    }


    function LocalUserName (name : String){

       var remotelist = gameObject.FindGameObjectsWithTag ("RemoteName");

       if(!gameObject.Find("Username2")){
         remotelist[0].GetComponent(TextMesh).text = name;
       }    
       if(gameObject.Find("Username1") && gameObject.Find("Username2")){
         remotelist[1].GetComponent(TextMesh).text = name;
       }
       if(gameObject.Find("Username1") && gameObject.Find("Username2")&& gameObject.Find("Username3")){
         remotelist[2].GetComponent(TextMesh).text = name;
       }

       return;
     }


    function RemoteNameInst (){
        var rgo = gameObject.FindGameObjectsWithTag ("RemoteName");

        if(!networkView.isMine){
          if(remoteInt == 1){
             rgo[0].name = "Username" + 1;
          }
          if(remoteInt == 2){
             rgo[0].name = "Username" + 1;
             rgo[1].name = "Username" + 2;
          }
          if(remoteInt == 3){
             rgo[0].name = "Username" + 1;
             rgo[1].name = "Username" + 2;
             rgo[2].name = "Username" + 3;
          }
        }
    }
more ▼

asked Apr 18 '10 at 10:35 PM

Sinncere gravatar image

Sinncere
15 3 3 7

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

nevermind, ive managed to create a workin script for this problem. Any improvements are welcome.

few edits ...

RPCMode's ...

@RPC
function TellOurUser(name : String, info : NetworkMessageInfo){
var newEntry : UserNode = new UserNode();
newEntry.userName=name;
newEntry.networkPlayer=info.sender;
userList.Add(newEntry);
PlayerPrefs.SetString(name, name); // <---- creating keys for remote usernames.
}

@RPC
function MeshName (name : String){
for(var entry : UserNode in userList){
    if(entry.userName==name){                               
        var userPrefs = PlayerPrefs.GetString(name, "");  
        LocalUserName (userPrefs); // <-- if remote name == the username in our array, then userPrefs = string key name from playerPrefs.        
    }
}
}

related functions ...

 function LocalUserName (name : String){
var remotelist = gameObject.FindGameObjectsWithTag ("RemoteName");
if(gameObject.Find("Username1")){
    var un1 : GameObject = gameObject.Find("Username1");
    if(un1.GetComponent(TextMesh).text.Length ==0){
        remotelist[0].GetComponent(TextMesh).text = name;
    }
}
if(gameObject.Find("Username2")){
    var un2 : GameObject = gameObject.Find("Username2");
    if(un2.GetComponent(TextMesh).text.Length ==0){
        remotelist[1].GetComponent(TextMesh).text = name;
    }
}
if(gameObject.Find("Username3")){
    var un3 : GameObject = gameObject.Find("Username3");
    if(un3.GetComponent(TextMesh).text.Length ==0){
        remotelist[2].GetComponent(TextMesh).text = name;
    }
}

return;
}


function RemoteNameInst (){
var rgo = gameObject.FindGameObjectsWithTag ("RemoteName");
if(!networkView.isMine){
    if(remoteInt == 1){
        rgo[0].name = "Username" + 1;
    }
    if(remoteInt == 2){
        rgo[0].name = "Username" + 1;
        rgo[1].name = "Username" + 2;
    }
    if(remoteInt == 3){
        rgo[0].name = "Username" + 1;
        rgo[1].name = "Username" + 2;
        rgo[2].name = "Username" + 3;
    }

}
 }
more ▼

answered Apr 19 '10 at 01:28 PM

Sinncere gravatar image

Sinncere
15 3 3 7

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x801
x697
x352
x143
x100

asked: Apr 18 '10 at 10:35 PM

Seen: 1311 times

Last Updated: Apr 18 '10 at 10:35 PM