6
\$\begingroup\$

In my game inside Unity, I have two scenes setup. One for the main server ( acts like authoritative server ) and the other scene for my client. The game starts once my client connects to the server.

Now I am able to send RPC function calls from my scripts in my client scene to the server but I am not able to do it the other way around.

** Further edit to my code after comments ** *********** This is my client side script attached to GameObject CursorDetection inside Scene2 *****

 using UnityEngine;
    using System.Collections;


    // ON THE CLIENT SIDE
    public class CursorDetectionScript : MonoBehaviour {

        private bool hasGameStarted = false;
        void Update()
        {
           if (Input.GetMouseButtonDown(0))
           {
              networkView.RPC("mouseDown", RPCMode.Server,true);
           }
        }

        [RPC]
        public void mouseDown(bool isMouseDown)
        {

        }

    }

**** On the server side script inside scene1 *******

private void OnPlayerConnected(NetworkPlayer player)
{
    Debug.Log(" PLAYER CONNECTED " +player);
    networkView.RPC("AddNetworkPlayer", RPCMode.AllBuffered, player);
}

[RPC]
public void AddNetworkPlayer(NetworkPlayer player)
{
    playersList.Add(player);
    Debug.Log(" FIRST PLAYER IS " + playersList[0]);
}

 if(startGame)
 {
    networkView.RPC("SendMessageToClient1", RPCMode.AllBuffered, playersList[0]);
 }

[RPC]
public void mouseDown(bool isMouseDown, NetworkMessageInfo sender)
{
    isMouseDown = true;
    Debug.Log("CAN PLACE PANELS NOW" + " VIEW IS " + sender.sender.guid);
}

**** back on the client side script inside scene2 I called the SendMessageToClient1 function call *******

[RPC]
public void SendMessageToClient1()
{
    Debug.Log(" INSIDE CLIENT 1 ");
}
\$\endgroup\$
  • \$\begingroup\$ What do you mean by it "doesn't work"? \$\endgroup\$ – Ben Oct 24 '14 at 22:54
  • \$\begingroup\$ Sorry for the confusion with my initial question. So I am trying to make an RPC function call from my server script to client script. Both are in different scenes. The RPC call inside the server script with log message "FROM SERVER TO CLIENT " gets called and works fine. However the very last log message "INSIDE THE CLIENT" doesn't get called on the client side script. I have a boolean variable on my client script which I would want to set as true inside this call. \$\endgroup\$ – ckzilla Oct 24 '14 at 22:58
  • \$\begingroup\$ Which gameobject is this script attached to? And do all clients including the server have this gameobject instantiated? \$\endgroup\$ – Ben Oct 24 '14 at 23:01
  • \$\begingroup\$ I have edited the code from my first post. Basically they are 2 different scripts from 2 different scenes. One scene runs the server and the other scene runs my client. I have not instantiated anything. Could you please explain me what do you mean by that? Basically in short, once my game starts on the server side, I want to pass a boolean variable from my server to the client that is connected to the server through an RPC call telling the client that the game has started which is what I am not able to achieve. \$\endgroup\$ – ckzilla Oct 25 '14 at 0:03
  • \$\begingroup\$ You can only call RPC methods on scripts that are attached to the same game object. If you have a player object, for example, and it has a networkview component, then you can call an RPC that is on a script in player, from another script on player. However, you could not call an RPC method that is on a script that is part of an enemy object from a script on the player object. \$\endgroup\$ – Ben Oct 25 '14 at 9:12
0
\$\begingroup\$

When communicating between client and server you're using RPC calls. These RPCs are possible with the NetworkView script attached to an object. For simplicity, it's easiest to attach your communication scripts (the ones making invoking the RPCs) to the same object that the NetworkView script is attached to. This needs to be done on both Client and Server.

The easiest way to accomplish this is to create a communication relay object. This object has both the communication scripts and NetworkView script attached to it.

Your communication scripts will contain the RPC signatures for both the server and the client, in the same file.

So for example, your communication script might have a MouseDown RPC:

public void MouseDown(bool state) {
    if (Network.isServer) {
       //If we're the server, just call the method directly
       SetMouseDown(Network.player, state);
    } else {
       //Otherwise we're a client, make an RPC call on the server
       SendMouseDown(state);
    }
}

private void SendMouseDown(bool state) {
    networkView.RPC("SetMouseDown", Network.player, state);
}

//This method will only be called on the server.
[RPC]
public void SetMouseDown(NetworkPlayer clientID, bool state) {
     //dictionary for client mouse down states
     mouseDownState[clientID] = state;
     ConfirmMouseDown(clientID, state);
}

private void ConfirmMouseDown(NetworkPlayer player, bool state) {
     if (Network.isServer) {
        //If we're the server, print a message locally and call an RPC on the client
        // to activate the else clause of this method
        PrintMessage("Player: " + player + " set his mouse down to " + state);
        networkView.RPC("PrintMessage", player, "Server received my mouse down state change to " + state);
    }
    // this should be called on a client, so there is no 'else'
}

[RPC]
private void PrintMessage(string message) {
    Debug.WriteLine(message);
}

Now a call to MouseDown will work on either the client or the server. This is pretty useful in the case of the server being a player as well. They share RPCs and the server and client can communicate easily.

\$\endgroup\$
  • \$\begingroup\$ Note: That code is untested written from memory, so it might not being copy/paste-able. \$\endgroup\$ – MichaelHouse Oct 25 '14 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.