I am testing this over the network already and not just locally. The issue at hand is that the user who logs in will only receive the “has logged in” message. This also goes for any further chat messages sent.
I have NetworkManager.cs attached to an object of its own. OnConnectedToServer() and OnServerInitialized() runs spawnPlayer():
void spawnPlayer()
{
player = Network.Instantiate (playerPrefab, new Vector3 ((float)Random.Range (255, 265), (float)251.5, (float)Random.Range (285, 295)), Quaternion.identity, 0) as GameObject;
player.networkView.RPC("setPlayerName", RPCMode.All, username);
if(Network.isServer)
player.GetComponent<Chat>().playerList.Add (player.name);
else {
player.networkView.RPC ("addPlayerToList", RPCMode.All, player.name);
}
}
In here, the player is instantiated and stored as a GameObject. It also calls setPlayerName which simply sets their name to Player_USERNAMEHERE and also updates the Text Mesh above their head for all other players to see. This, surprisingly enough, works. All other players can see the names properly.
setPlayerName is in the Player.cs script. This script is attached to the playerPrefab that is in NetworkManager.cs:
[RPC] void setPlayerName(string username) {
name = "Player_" + username;
savedUsername = name.Substring (7);
TextMesh playerName = GetComponent<TextMesh>();
playerName.text = savedUsername;
if(networkView.isMine) {
networkView.RPC("setPlayerName", RPCMode.OthersBuffered, savedUsername);
networkView.RPC ("print", RPCMode.AllBuffered, "" + savedUsername + " has logged on.");
}
}
It calls print as well so it will display “Player has logged in.” This is where I have been having trouble.
print() is in Chat.cs which is also attached to the playerPrefab:
[RPC] void print(string string_)
{
log.Add (string_);
if(log.Count > maxLogMessages) {
log.RemoveAt(0);
}
}
It will add it to the list, log, and OnGUI() will print it out to the label which creates the chat box. It will display it on the user’s screen but not other user’s screens. Pretty much as if it was never sent.
Pertaining the issue where no players can converse with each other, here is how I use RPC to also use print() While I don’t think It’s important to be read, it’s there if it may help to solve my issue:
List<string> charList = new List<string>();
foreach(char c in stringToEdit) {
charList.Add (c.ToString());
}
int limit = 1;
string brokenString = "";
bool brokenStringLooped = false;
foreach(string s in charList) {
if(!brokenStringLooped && limit == (25 - (GetComponent<TextMesh>().text + ": ").Length)){
brokenString = brokenString + s;
networkView.RPC ("print", RPCMode.AllBuffered, GetComponent<TextMesh>().text + ": " + brokenString);
brokenStringLooped = true;
brokenString = "";
limit = 1;
}
else{
if(limit == 25){
brokenString = brokenString + s;
networkView.RPC ("print", RPCMode.AllBuffered, brokenString);
brokenString = "";
limit = 1;
}
else{
brokenString = brokenString + s;
limit++;
}
}
}
if(brokenString!=""){
if(!brokenStringLooped)
networkView.RPC ("print", RPCMode.AllBuffered, GetComponent<TextMesh>().text + ": " + brokenString);
else
networkView.RPC ("print", RPCMode.AllBuffered, brokenString);
brokenString = "";
The majority of what is written there is simply what I wrote to break the string in chunks so it fits the label and the ScrollView.
Going back to spawnPlayer(), addToPlayerList() does not add the player’s name to the list at all. This is also an issue. addToPlayerList() is also in Chat.cs which is attached to the playerPrefab:
[RPC] void addPlayerToList(string playerName)
{
if(Network.isServer) {
playerList.Add(playerName);
}
}
Successes:
-Users can see each other move
-Users control their own characters
-Each player’s overhead name (Text Mesh) is their own username.
Issues:
-print() does not send text over the network. It only displays on the senders screen.
-playerList is not being updated.
Questions about RPC:
-What is the difference between buffered and the rest of the RPCModes? I’ve been alternating between them to get a clear understanding. I’ve done this about 10 times and still can’t see what’s the difference.
-When should RPC be used?