- Home /
RPC Broadcast using Unet
Hi, i need help with my multiplayer game using Unet, i have a script which creates a chat gui, when i press enter the text gets send to the server using [Command] but how do i send the text to all player instances ? . When i use syncvar or rpc only the original player instance will get the text.
sincerly frubi
Answer by DiegoSLTS · Aug 17, 2016 at 10:15 PM
Can you share your current code of that command and some info on your setup? Your project can be anything.
With that little info I can only tell you that you need a ClientRPC method that your server should call after executing the command. Something like:
[Client]
public void SendMessage(string msg) {
CmdSendMessage(msg);
}
[Command]
void CmdSendMessage(string msg){
chat.AddMessage(msg);
RpcAddMessage(msg);
}
[ClientRpc]
void RpcAddMessage(string msg) {
chat.AddMessage(msg);
}
Also, I noted you said "the player that sent the text"... are you talking about the player (you'll have multiple players for each client, one being the local player) or the client? If you're expecting the SyncVar off all players being updated or the Rpc method of all players being called you're missunderstanding some concepts. The ClientRpc runs on a certain player (the "same" player) in all the clients. Also, make sure you're using ClientRpc and not the new TargetRpc, which actually runs only on one player of the target client.
Sure, the concept of the network is: the network manager (Unet) manages all connections (host, client etc.) loads the scene and spawns the player prefab. In this player prefab i added the the networkChat.cs, a other script of the player is checking if the instance is the local player and enables/disables all components. When i use simple RPC only the sender receives his text.
NetworkChat.cs = https://drive.google.com/file/d/0B1IuACgDZHdYX3ZUbnhEQ3NGbGc/view?usp=sharing
"only the sender receives his text".
Seems to me that the actual data is not being transferred across the network.
Are you testing this across multiple devices? Use the editor as the Server and launch a Standalone for the client.
Your setup is confusing, each player has it's own chat?
As I said before, when you execute an RPC method in the server for one player, all clients execute that method for that player ONLY. If on each client you're deactivating scripts on players that are not the local player, then there's only one client that will do anything since it's the only cilent with the player with the script with the RPC method enabled. Just to be clear, the RPC method doesn't run for the local player, if you have two players called Player1 and Player2 and you run an RPC method on the server on a script of Player1, all clients will be notified to execute that same method on their copy of Player1, even if it's not that client's local player.
Disabling all components on players that are not the local players is a bad idea, the players must be enabled so the networking makes sense, it's the only way you can synchronize all player objects between all clients.
The basic code to send the text to all clients is what I wrote above, you just execute an RPC for some players and all copies of that player will run that code, the problem here seems to be that you have a "chat" for each player ins$$anonymous$$d of one chat that all players can update, so you must disable the players to show only one chat.
Create one object that handles the chat, make all players update the same chat, and add some reference to the local player. That way you have only one thing that draws the chat and handles input, and when you press Return you can do:
localPlayer.CmdSendChat(inputField);
ins$$anonymous$$d of:
CmdSendChat(inputField);
One more detail, the [Client] attribute is for methods that you call yourself. For OnGUI, Update and other $$anonymous$$onoBehaviour methods you must use [ClientCallback].
"As I said before, when you execute an RPC method in the server for one player, all clients execute that method for that player ONLY"
I know that, i thought there would be a easy way to execute the rpc on all players.
I will try as you said, thank you, at the moment i dont have much time (and motivation) so it will take a little time untill i answer again
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Network Chat not sending messages to other clients 1 Answer
Sending RPC to add playerName 4 Answers