Reputation: 15
I'm trying to send data from client to client but it doesn't seem to work. I'm using Unitys MasterServer. It works fine when i'm sending data from server to client and client to server.
I'm sending data with RPC. Is it possible to send data from client to client or do I have to rewrite my code so I send an RPC call from client 1 to server, and then a new RPC call from server to client 2?
Here I call the RPC function:
networkView.RPC("UpdateBombIncoming", playerPurple);
Here's my RPC:
[RPC]
void UpdateBombIncoming(){
Debug.Log ("Bomb is incoming");
bombIncoming = true;
}
Here's my Network code:
public class NetworkManager : MonoBehaviour {
public int maxNumberOfPlayers = 3;
public int port = 25002;
public int playerID;
public NetworkPlayer playerBlue; // Server
public NetworkPlayer playerPurple;
public NetworkPlayer playerGreen;
public int playerColor; // 1: blue, 2: purple, 3: green
public Font myFont;
public int playerCount;
private SceneManager sceneManager;
private bool showPlayersOnGUI;
string registeredGameName = "Bomba_Alpha";
bool isRefreshing = false;
float RefreshRequestLength = 3.0f;
HostData[] hostData;
void Awake(){
}
void Start(){
sceneManager = GetComponent<SceneManager>();
}
void Update(){
Debug.Log("Blue Player connected from " + playerBlue.ipAddress + ":" + playerBlue.port);
Debug.Log("Purple Player connected from " + playerPurple.ipAddress + ":" + playerPurple.port);
Debug.Log("Green Player connected from " + playerGreen.ipAddress + ":" + playerGreen.port);
Debug.Log("Players connected to server " + playerCount);
// When player is connected to server, show player on GUI
if(Network.isServer){
// Show players on GUI
showPlayersOnGUI = true;
}
else if(Network.isClient){
// Show players on GUI
showPlayersOnGUI = true;
}
}
private void StartServer(){
Network.InitializeServer (maxNumberOfPlayers, port, false);
MasterServer.RegisterHost(registeredGameName, "Bomba Alpha", "Test of TMB_server");
}
void OnServerInitialized(){
Debug.Log ("Server has been initialized!");
Debug.Log("Player " + playerCount + " connected from " + Network.player.ipAddress + ":" + Network.player.port);
// Set server as playerBlue
playerBlue = Network.player;
}
void OnMasterServerEvent(MasterServerEvent masterServerEvent){
if(masterServerEvent == MasterServerEvent.RegistrationSucceeded){
Debug.Log("Registration Successful!");
}
}
public IEnumerator RefreshHostList(){
Debug.Log ("Refreshing...");
MasterServer.RequestHostList (registeredGameName); // Get servers from MasterServer with 'our' name
float timeStarted = Time.time;
float timeEnd = Time.time + RefreshRequestLength;
// Contínuelly update hostData
while (Time.time < timeEnd) {
hostData = MasterServer.PollHostList(); // pull down the requested server
yield return new WaitForEndOfFrame();
}
// Check if there is no servers
if (hostData == null || hostData.Length == 0) {
Debug.Log ("No active servers have been found");
}
else{
Debug.Log(hostData.Length + " server(s) found");
}
}
void OnPlayerConnected(NetworkPlayer player) {
playerCount++;
if(Network.player == player){
Debug.Log ("Hey!");
}
if (playerCount == 2){
playerPurple = player;
Debug.Log ("I'm purple!");
}
// Set 2nd client as playerGreen
else if (playerCount == 3){
playerGreen = player;
}
if(Network.isServer){
Debug.Log ("Colors are updating!");
networkView.RPC("UpdateColors", RPCMode.Others, playerBlue, playerPurple, playerGreen, playerCount);
}
Debug.Log("Player " + playerCount + " connected from " + player.ipAddress + ":" + player.port);
}
void OnPlayerDisconnected(){
playerCount--;
}
// On GUI is for testing and will not exist in the final game
public void OnGUI(){
if (sceneManager.currentScene == 3){
// Start server
if(GUI.Button(new Rect(150f,100f,300f,150f), "Start new server")){
StartServer();
}
// Refresh server list
if(GUI.Button(new Rect(150f,250f,300f,150f), "Refresh server list")){
StartCoroutine("RefreshHostList");
}
if (hostData != null) {
for (int i = 0; i < hostData.Length; i++){
if(GUI.Button(new Rect(150f ,400f + (110f * i),300f,150f), hostData[i].gameName))
{
Network.Connect(hostData[i]);
}
}
}
// Show list of players connected to the server
if (showPlayersOnGUI) {
GUIStyle myStyle = new GUIStyle();
myStyle.font = myFont;
GUI.Label(new Rect(20, 700, 200, 20), ("Other players connected:"), myStyle);
if (Network.player == playerBlue){
GUI.Label(new Rect(20, 600, 200, 20), ("You are BLUE player"), myStyle);
if(playerCount == 1){
GUI.Label(new Rect(20, 750, 200, 20), ("- none -"), myStyle);
}
if(playerCount >= 2){
GUI.Label(new Rect(20, 750, 200, 20), ("Purple player"), myStyle);
}
if(playerCount == 3){
GUI.Label(new Rect(20, 800, 200, 20), ("Green Player"), myStyle);
}
}
else if (Network.player == playerPurple){
GUI.Label(new Rect(20, 600, 200, 20), ("You are PURPLE player"), myStyle);
if(playerCount >= 2){
GUI.Label(new Rect(20, 750, 200, 20), ("Blue player"), myStyle);
}
if(playerCount == 3){
GUI.Label(new Rect(20, 800, 200, 20), ("Green Player"), myStyle);
}
}
else if (Network.player == playerGreen){
GUI.Label(new Rect(20, 600, 200, 20), ("You are GREEN player"), myStyle);
if(playerCount == 3){
GUI.Label(new Rect(20, 750, 200, 20), ("Blue player"), myStyle);
GUI.Label(new Rect(20, 800, 200, 20), ("Purple Player"), myStyle);
}
}
}
}
}
void UpdatePlayersOnNetwork(){
if (Network.player == playerBlue) {
Debug.Log("I'm player BLUE");
networkView.RPC("UpdateBluePlayer", RPCMode.Others, Network.player);
}
if (Network.player == playerPurple) {
Debug.Log("I'm player PURPLE");
networkView.RPC("UpdatePurplePlayer", RPCMode.Others, Network.player);
}
if (Network.player == playerGreen) {
Debug.Log("I'm player GREEN");
networkView.RPC("UpdateGreenPlayer", RPCMode.Others, Network.player);
}
}
// -------- RPC sends data via network --------
// Update player-color connections
[RPC]
void UpdateColors(NetworkPlayer blue, NetworkPlayer purple, NetworkPlayer green, int players){
Debug.Log ("Colors are updated");
playerBlue = blue;
playerPurple = purple;
playerGreen = green;
playerCount = players;
//Debug.Log ("Colors have been updated!");
}
}
Thanks
Moeskjaer
Upvotes: 0
Views: 2057
Reputation: 686
Short of implementing your own RPC system in C#, you'll need to rewrite your code to "bounce" RPCs off of the server.
In Unity's networking model, the clients are all connected to the server but not to each other. Any state changes the clients cause is sent to other clients through the server.
Upvotes: 1