- Home /
Networking how to return data? OR how to send only to 1 client
how to return data like function does
bool somefunction (){
return true;
}
as I've found out
[RPC]
bool somefunction(){
return true;
}
returns to it's own program and not network
OR how to send data to only 1 client:
my FULL CODE below that sends to all clients instead of only 1.
Server Script:
using UnityEngine;
using System.Collections;
using System.IO; // for writing and reading binary files
public class Server : MonoBehaviour {
public GameObject Planet;
public string IP = "127.0.0.1";
public int Port = 25001;
private SaveLoadFile SaveLF;
void OnGUI (){
if (Network.peerType == NetworkPeerType.Disconnected){
if (GUILayout.Button("Start Server")){
Network.InitializeServer(10, Port);
}
}
else {
GUILayout.Label("Server");
GUILayout.Label("Connections: " + Network.connections.Length);
if (GUILayout.Button("Logout")){
Network.Disconnect(5);
}
}
}
[RPC]
bool Register (string UserName, string Password){
if (SaveLF.SaveUserPassword(UserName, Password, false) ){
Debug.Log("Account created");
networkView.RPC("ReturnTF", RPCMode.Others, "Register", true );
return true;
}
else {
Debug.Log("user Exists");
networkView.RPC("ReturnTF", RPCMode.Others, "Register", false );
return false;
}
}
[RPC]
bool LogIn (string UserName, string Password){
if (SaveLF.VerifyUserPassword(UserName, Password)){
Debug.Log("correct Password");
networkView.RPC("ReturnTF", RPCMode.Others, "Log In", true );
return true;
}
else{
Debug.Log("wrong username or password");
networkView.RPC("ReturnTF", RPCMode.Others, "Log In", false );
return false;
}
}
[RPC]
void ChangeColor (int i){
if (i == 0){
Planet.renderer.material.color = Color.green;
}
else if (i == 1){
Planet.renderer.material.color = Color.red;
}
else if (i == 2){
Planet.renderer.material.color = Color.blue;
}
}
}
Client Script:
using UnityEngine;
using System.Collections;
public class Client : MonoBehaviour {
public GameObject Planet;
public string IP = "127.0.0.1";
public int Port = 25001;
private string UserName = "Username";//||Email";
private string UserPass = "";
string type = "";
bool works = false;
void Start (){
Network.Connect(IP, Port);
}
void OnGUI (){
if (Network.peerType == NetworkPeerType.Disconnected){
if (GUILayout.Button("Reconnect")){
Network.Connect(IP, Port);
}
}
// enter username
GUILayout.BeginHorizontal();
GUILayout.Label("User Name");
UserName = GUILayout.TextField(UserName, GUILayout.MinWidth(50));
GUILayout.EndHorizontal();
// enter password
GUILayout.BeginHorizontal();
GUILayout.Label("Password");
UserPass = GUILayout.PasswordField(UserPass,'*', GUILayout.MinWidth(30));
GUILayout.Label("" + UserPass.Length);
GUILayout.EndHorizontal();
// login || register
if (! (Network.peerType == NetworkPeerType.Disconnected)) {
GUILayout.BeginHorizontal();
if (GUILayout.Button("Login")){
networkView.RPC("LogIn", RPCMode.Server, UserName, UserPass);
}
if (GUILayout.Button("Register")){
networkView.RPC("Register", RPCMode.Server, UserName, UserPass);
}
GUILayout.EndHorizontal();
GUILayout.Label(type + works);
}
// else {
// if (Network.peerType == NetworkPeerType.Client){
// GUILayout.Label("Client");
// if (GUILayout.Button("Color green")){
// networkView.RPC("ChangeColor", RPCMode.All,0);
// }
// if (GUILayout.Button("Color Red")){
// networkView.RPC("ChangeColor", RPCMode.All,1);
// }
// if (GUILayout.Button("Color Blue")){
// networkView.RPC("ChangeColor", RPCMode.All,2);
// }
// if (GUILayout.Button("Logout")){
// Network.Disconnect(5);
// }
// }
// }
}
// [RPC]
// void Register (string UserName, string Password){
// }
// [RPC]
// void LogIn (string UserName, string Password){
// }
[RPC]
void ReturnTF (string tip, bool TF){
works = TF;
type = tip;
}
}
AND if someone knows how to even give data only from another client to other specific client and not to all others too.
it would become pretty laggy if I'd send the data to all and than block the data from those that must not get it.
thanks in advance
Answer by whydoidoit · Aug 20, 2013 at 09:01 PM
You can't return data but you can get information about who called the method and then RPC that player only.
[RPC]
void PerformLikeAFunction(string parameter, NetworkMessageInfo info)
{
networkView.RPC("PerformReturn", info.sender, parameter.ToUpper());
}
[RPC]
void PerformReturn(string returnValue)
{
}
In the same way, as long as you know the player you can send a message just to that player using that rather than RPCMode.Server etc.
General reference: http://docs.unity3d.com/Documentation/Components/net-RPCDetails.html
worked just a bit more info, ...
sender doesn't need to give in info within function as it's made automatically
my new code:
Client
// client sends message
networkView.RPC("Register", RPC$$anonymous$$ode.Server, UserName, UserPass);
// client recives message
[RPC]
void ReturnTF (string tip, bool TF){
works = TF;
type = tip;
}
Server recives and messages back to other client function
[RPC]
bool Register (string UserName, string Password, Network$$anonymous$$essageInfo Info){
if (SaveLF.SaveUserPassword(UserName, Password, false) ){
Debug.Log("Account created");
networkView.RPC("ReturnTF", Info.sender, "Register", true );
return true;
}
else {
Debug.Log("user Exists");
networkView.RPC("ReturnTF", Info.sender, "Register", false );
return false;
}
}
I still needed a bit of time to understood your code, ... I'm pretty new at this but now that I understand it workes like charm, ...
now I even know how to make from client from client using Network$$anonymous$$essageInfo
Thanks a bunch @whydoidoit you answered my both Q
Your answer
Follow this Question
Related Questions
Network how to send data/activate function 1 Answer
RPCMode.Server works in one spot but not another 0 Answers
ClientRpc not called on Client 1 Answer
Totally lost with UNET - Client to Server Networking 3 Answers
Multiple Cars not working 1 Answer