Sender

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
 
m (Text replace - "</javascript>" to "</syntaxhighlight>")
 
(2 intermediate revisions by 2 users not shown)
Line 2: Line 2:
 
A sender sends data via a [[Server]].  This script was implemented so that the code could be modified in one place.  It sends the root object name, the name of the object it's attached to, and whatever is passed in as the ''data'' variable. A script could simply access the Server directly and bypass the Sender.
 
A sender sends data via a [[Server]].  This script was implemented so that the code could be modified in one place.  It sends the root object name, the name of the object it's attached to, and whatever is passed in as the ''data'' variable. A script could simply access the Server directly and bypass the Sender.
 
==Code==
 
==Code==
<javascript>
+
<syntaxhighlight lang="javascript">
 
private var server : Server = null;
 
private var server : Server = null;
private var timer = 0.0;
 
  
 
function Start(){
 
function Start(){
Line 21: Line 20:
 
}
 
}
 
}
 
}
</javascript>
+
</syntaxhighlight>

Latest revision as of 20:52, 10 January 2012

[edit] Sender Script

A sender sends data via a Server. This script was implemented so that the code could be modified in one place. It sends the root object name, the name of the object it's attached to, and whatever is passed in as the data variable. A script could simply access the Server directly and bypass the Sender.

[edit] Code

private var server : Server = null;
 
function Start(){
	var controller = GameObject.FindWithTag("GameController");
	if(controller!=null)server = controller.GetComponent("Server");
}
 
function Send(data){
	if(server==null) {
		var controller = GameObject.FindWithTag("GameController");
		if(controller!=null)server = controller.GetComponent("Server");
	}
	if(server!=null && server.Connected()){
		var sendString = gameObject.transform.root.name + "," + gameObject.name + "," + data + "\n";
		server.PutMessage(sendString);
	}
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox