- Home /
Strings through a network?
hi I am making a RTS style game where you build armies/bases and attack your friends/enemies army/base(I was making this on DellanX, but the password for the login was mest up, so I created a new account). I am having trouble with sending my variables to the network. In this case I am sending each players name, faction name, color, and alliance to the network. I need all players to be able to see eachothers name/army/faction, but I cant seem to figure it out. I know very little about RPC, and I dont have enough knowledge to use it to solve this problem.
EDIT:
in case you didn't see the comment below
I attempted to figure out RPC, but am having problems. I am Using arrays to store players, but when I connect to my server my variables arent changed. I am using code like:
public ArrayList PlayerName = ArrayList public string MyName = ""; //Script for GUI here
void OnServerInitialized(){
MyName = "BLUE";//For identification
networkView.RPC("SetServerInfo",RPCMode.All);
}
void OnConnectedToServer(){
MyName = "RED";//For identification
networkView.RPC("SetServerInfo",RPCMode.All);
}
[RPC]
void SetServerInfo(){
PlayerName.Add(MyName);
Debug.Log(PlayerName[0])//Show What the first name stored is
}
When I run my program as server I get in my log:
BLUE
When I run my program as client I get:
RED
What am I doing wrong? and what do I need to do to fix it???
RPC can send strings, no trouble. What's the issue? It can also send byte arrays, which is as useful as it is undocumented...
I attempted to figure out RPC, but am having problems. I am Using arrays to store players, but when I connect to my server my variables arent changed. I am using code like:
public ArrayList PlayerName = ArrayList public string $$anonymous$$yName = ""; //Script for GUI here
void OnServerInitialized(){
$$anonymous$$yName = "BLUE";//For identification
networkView.RPC("SetServerInfo",RPC$$anonymous$$ode.All);
}
void OnConnectedToServer(){
$$anonymous$$yName = "RED";//For identification
networkView.RPC("SetServerInfo",RPC$$anonymous$$ode.All);
}
[RPC]
void SetServerInfo(){
PlayerName.Add($$anonymous$$yName); Debug.Log(PlayerName[0])//Show What the first name stored is
}
When I run my program as server I get in my log:
BLUE
When I run my program as client I get:
RED
What am I doing wrong? and what do I need to do to fix it???
Answer by Dellan_X · Apr 20, 2012 at 04:49 PM
I figured the problem out it turns out the function itself is over the network, so in order for the variables to be stored right I need to send them over the network also, I ended up with this:
void OnServerInitialized(){
PlayerNames.Add(PlayerName);
Debug.Log("SERVER");
Debug.Log(PlayerNames[0]);
}
void OnConnectedToServer(){
networkView.RPC("SetServerInfo",RPCMode.All,PlayerName);
}
void SetServerInfo(string PN){
PlayerNames.Add(PlayerName);
Debug.Log("CLIENT");
Debug.Log(PlayerNames[1]);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613070534im_/https://answers.unity.com/themes/thub/images/avi.jpg)