Sending a username to an instance to be displayed

Change 7/11/08

I’m starting to get the hang off networking, but I’m having diffculty sending information to instances of the player when instantiated.

I have a username that is submitted by the player before connection. This is stored as myUsername in a script called gui which handles the gui and conenctions.

When a user connects an avatar is created, but I want the username to be displayed above the avatar in a Second Life / WoW style. Currently the GUI part for this is working, but I am having a lot of trouble sending the username by RPC to each user whenever a new player enters.

Basically I need to do this.

When a new avatar instantiates it requests the usernames of the other players and also sends its own name out.

I think the issue I have been having is knwoing which instance has sent which name and applying it to the correct one.

OK, just built a web player version, used my Mac’s IP instead of 127.0.0.1, ran inside of Unity on Mac and opened the webplayer on my PC and it connected.

Can a server not also be a client?

You can not be a server and a client at the same time unfortunately.

OK, I’ve got the network connect working, but without a simple networking tutorial that works (Could be my network…) I’m finding it a little hard to work out how to get people moving around.

I can use Network.Instantiate to clone a prefab in the world.

But I’m not sure how to go about creating new objects from prefabs to indicate other players inworld, which instantiate where that player is when they connect to the server, then move as other players move.

Thanks to AngryAnt on IRC I know I need to use OnSerializeNewtorkView, which I think needs to be applied to the object I am cloning (that indicates other players, for this I’m just going to use a textured cube for now).

Anything along the line of… use this class/function, here, here, here would help.

So far on my little script I’ve gotten…(And I know I haven’t dealt with disconnections yet…)

var textFieldString  = "App Started";
var scrollViewVector: Vector2 = Vector2.zero;
var playerCount: int = 0;
var typingArea = "";
var playerPrefab : Transform;

function OnGUI () {
	
	scrollViewVector = GUI.BeginScrollView(Rect(25,25,220,200), scrollViewVector,Rect(0,0,180,2000));
	textFieldString = GUI.TextArea(Rect(0,0,200,2000 ),textFieldString);
	GUI.EndScrollView();
	typingArea = GUI.TextArea(Rect(25,225,200,100),typingArea);
	if(GUI.Button(Rect (225,225,100,100),"Send")){
		SendText();	
	}	
	if(GUI.Button(Rect (425,0,150,50),"Launch Server")){
		LaunchServer();	
	}
	if(GUI.Button(Rect (425,50,150,50),"Connect")){
		ConnectToServer();	
	}	
}

function LaunchServer(){
	textFieldString  = textFieldString + "\nCreating Server";
	Network.incomingPassword = "";
	Network.InitializeServer(32,25000);	
}

function OnServerInitialized(){
	Debug.Log("Server initialized and ready");
	textFieldString  = textFieldString + "\nServer Created";
}

function ConnectToServer(){
	textFieldString  = textFieldString + "\nConnecting to Server";
	Network.Connect("147.197.166.113",25000,"");
}

function OnConnectedToServer(){
	textFieldString  = textFieldString + "\nConnected To Server";
	Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
}

function OnPlayerConnected(player: NetworkPlayer){
	textFieldString  = textFieldString + "\nPlayer" + playerCount++ + "connected from " + player.ipAddress + ":" + player.port;
}

function SendText(){
	//@RPC
	textFieldString  = textFieldString + "\nSending Text";
	networkView.RPC ("PrintText", RPCMode.All, typingArea);
	typingArea 	="";
}

@RPC
function PrintText (text : String, info : NetworkMessageInfo)
{
   textFieldString  = textFieldString + "\n" + info.sender + " says:" + text;
}

I may have misunderstood your first post. Just to clarify, it is not possible to be a client and server within the same program; however, it is possible to run a client and server in 2 separate programs on the same computer.

So you can create a server on your computer and connect to 127.0.0.1 from another unity program.

Also, have you checked out the networking example project?
http://unity3d.com/support/resources/example-projects/networking-example

It isn’t a tutorial, but it is pretty good. And you should be able to have Lerpz walking around without any problem on 2 instances of the program running on the same computer (1 client 1 server).

Just remember, if you build as a web player, ensure that run in background is turned on or you will not be able to connect to the server.

Yeah, I’ve tried the networking example, but it doesn’t want to run, although I am working form home today and the network is different from work.

I can connect two machines together though so its more the process and flow of instanting avatars and moving them around.

EDIT: Hmmm… the example seems to be working from home. Something to do with NAT punchthrough.

Will now disassemble the example to see how it works.

Bump. 1st Post and title have been changed to reflect the new question. Don’t want to flood this forum with my questions so am keeping it in one place.

I think it is actually harder to follow the thread when the first post and title get changed to a different issue. I recommend starting a new thread for new issues in the future.

What I do is create a file that contains global variables for the local user:

static var username : String = "Racer";

Then when a network player is instantiated, I use the networkView.isMine property to set the name of all the networked versions of itself.

private var myName : String = "Unknown";

function OnNetworkInstantiate()
{
   if( networkView.isMine ){
      networkView.RPC( "SetName", RPCMode.AllBuffered, Globals.username );
   }
}

@RPC
function SetName( newName : String )
{
   myName = newName;
}

Thanks Proton,

Will set up an new thread from now on.

To be honest that looks like something I’ve tried, but I can’t look until Monday as the Mac is in the office, when Unity3d Windows arrives I won’t have that problem :slight_smile:

I’ll have a look Monday.

It works!!!

I think the problem must have been to do with RPCMode.AllBuffered, I was using RPCMode.All

Must do some more reading.

Thanks Proton!