Trying to send command for non-local player (592919)

So the goal here is to have the ability for clients to chat with eachother, but unfortunately only the host can chat and the clients can see the hosts messages but cannot send their own. Here is the basic setup:

void SendMessage(string message){
if(!string.IsNullOrEmpty(message.Trim())){
CmdChatMessage(message);
}
}

[Command]//Remote Procedure Call
public void CmdChatMessage(string message){
chatHistory.Add(message);
RpcSyncChatMessages(message);
}

[ClientRpc]
public void RpcSyncChatMessages(string message){
chatHistory.Add (message);
}

Your problem is that you can’t send a Client RPC from a player object. How I would do it is to have an empty game object with the chat script and then have another script on each player that handles sending a command to the chat script on the empty game object.

The problem I have with that, is that I can’t link the empty object to the prefabs of the players, they simply get erased when i hit save.

You only can send commands from player object which you own and the way you should do it is that clients should send a command to server but then server after receiving the command should gather all messages for all players in a chat object spawned by server and not owned by any client.
then this chat object send the message to all clients.
so each player’s object send it’s command
there is a chat object owned by server and no clients which server gathers all messages in
then this chat object sends the clientRPC of the chat to all chat objects in clients.

Well that is an issue considering 1 player needs to host the game and another needs to connect to it. The chat script is being run on an empty object and only works for the player who hosted the game. Unless you mean something else because I can’t understand what you typed very well.

well do this
create an object with network identity on server and call it chat (doesn’t matter if it is a host i.e. a client which is server as well)
add a script to it which has the following code
void GatherChatMessage(string msg,string client)
{
chatHistory.Add(message,client);
RpcUpdateChatOnClient(message,client);
}

[ClientRPC]
void RpcUpdateChatOnClient(string message,string client)
{
//show the message
}

then for sending message from client to server, your player object should send a command with message and client name as you sent it in your own code but on player object’s server script you put
[Command]
void CmdChatMessage(string message,string client)
{
//now find the chat object on server and call relevant method
chatObject.GatherChatMessage(message,client);
}

hope it clarifies a bit.
}

also you can use Messages instead of RPCs and commands for chat so define a class ChatMessage : MessageBase {public string name,message}
and then send it from client with NetworkClient and from server by NetworkServer.SendXXX methods

Then you register for messages of the type ChatMessage in NetworkMessage and ClientScene classes and use the callback to receive messages.

Ok so I did the first option you gave me, but for some reason the message never reached the ClientRpc method. What could be stopping the message from going from this method: (This is on the ChatManager Object on the server)

public void AddChatMessage(string message){
RpcSyncChatMessage(message);
}

to this method:

[ClientRpc]
public void RpcSyncChatMessage(string message){
chatHistory.Add(message);
}

Also the messages should be getting displayed by this same object as well using this:

public void OnGUI(){
GUILayout.Space (250);
for (int i = chatHistory.Count - 1; i >= 0; i–) {
GUILayout.Label (chatHistory*);*
}
}

any errors?
is the caht manager available on clients? does it have a network identity and is it spawned using NetworkServer.Spawn?

you should use SyncListString, so all you need is a command to server and the list is already synced :slight_smile:

Ashkan, i’m not too sure how to show you all these things in how it is setup, but if your willing to connect on skype I can share my screen for a more in-depth look at the problem I am having.

Lordphil, I am trying it your way but it doesn’t like how I am doing it and I am inexperienced with SyncVar and SyncList so what might I be doing wrong with this?:

[SyncList] //This is the line the error message points to
public List chatHistory = new List();

Error:
error CS0404: ‘<’ unexpected: attributes cannot be generic

FoxCastz, its used like so : public SyncListString chatHistory = new SyncListString();

Yea I tried it like that, but it crashes my game ): I am running out of options D:
It gave a really weird glitch which looked like gibberish