Search Unity

RPC from Server to Client

Discussion in 'Multiplayer' started by jimma, Oct 24, 2014.

  1. jimma

    jimma

    Joined:
    Apr 3, 2013
    Posts:
    28
    Hello,

    I am currently working on a game inside Unity where I would use IPAD's as controllers to play the game run from a PC. I intend to host the server on the PC and all the IPAD's connected in the game would act as clients.

    The IPAD's would be used to detect touch and drag for the game. Everything needs to run in sync of course.

    I currently have two different scenes in my project, one for the server where the main game would be run and the other for the clients ie the controllers.

    Now I am able to pass information from the clients to server like touch information and stuff.

    For example this is the code I run on the client side

    Code (CSharp):
    1. void OnGUI()
    2.   {
    3.   if (GUI.Button(new Rect(10, 150, 200, 50), " START GAME "))
    4.   {
    5.   networkView.RPC("StartGame", RPCMode.Server, true);
    6.   }
    7.   }
    //this acts a signature for both client and server


    Code (CSharp):
    1. [RPC]
    2.   public void StartGame(bool startGame)
    3.   {
    4.    //ON CLIENT SIDE
    5.   }
    And on the server end, I just use the same signature

    Code (CSharp):
    1. [RPC]
    2.   public void StartGame(bool start)
    3.   {
    4.   Debug.Log(" ON SERVER SIDE ");
    5.   startGame = start;
    6.   }
    And this works fine..

    However what if I want to send message from a Server back to the Client? Say once my game starts running, I want all my clients to know that the game has started. The above technique of similar signature does not work in this case. How would I do it? I tried using the same technique as above but it doesn't work

    so once my game starts

    Code (CSharp):
    1. if (startGame && initializeOnce)
    2. {
    3. networkView.RPC("GAMESTARTED", RPCMode.All, true);
    4. }
    // the call inside server
    Code (CSharp):
    1. [RPC]
    2.   public void GAMESTARTED(bool started)
    3.   {
    4.   Debug.Log(" INSIDE SERVER ");
    5.   }
    6.  
    I then use the same signature to call on the Client side script which is in a different scene as I mentioned previously but this doesn't work

    Code (CSharp):
    1. [RPC]
    2.   public void GAMESTARTED(bool started)
    3.   {
    4.   Debug.Log(" INSIDE CLIENT ");
    5.   }

    How do I make sure that I can pass message back and forth client and server correctly?