0
\$\begingroup\$

I am making a multiplayer game. I am sending score via the server and client but I want to be sure that can I call an RPC function of a script from any other script.

For example, say Table_Manager as a script which contains a get_score function and I want to call it from a game_stat script like Table_Manager.networkview.rpc("Table_Manager", rpcmode.all, 25); The get_score method would look something like:

[RPC]
public void get_score(int new_score)
{
   // ...
}

How can I accomplish that?

\$\endgroup\$

3 Answers 3

2
\$\begingroup\$

Yes you can :

Table_Manager TM = your_game_object.GetComponent<Table_Manager>();
NetworkView nv = TM.GetComponent<NetworkView>();

nv.RPC("get_score",RPCMode.All,25);
\$\endgroup\$
3
  • \$\begingroup\$ But i didn't understand how are you getting the networkview component from a script ? \$\endgroup\$
    – user43609
    Commented Apr 7, 2014 at 5:28
  • 3
    \$\begingroup\$ That's what GetComponent<NetworkView>() is for. \$\endgroup\$
    – jhocking
    Commented Apr 7, 2014 at 15:19
  • \$\begingroup\$ As jhocking said \$\endgroup\$
    – JoRouss
    Commented Apr 7, 2014 at 17:22
1
\$\begingroup\$

You can add the following attribute at the beginning of your script class:

[RequireComponent (typeof (NetworkView))]

This will ensure that whenever your script is added to a GameObject, a NetworkView component will also be added to that GameObject. Or you can just manually add the NetworkView component yourself. If you're only using it for RPC, you can set the Observed to none and change the State Synchronization to Off.

Once you're sure a NetworkView is attached to the same GameObject as your script, you can either use the built in shortcut of this.networkview (which will automatically retrieve the NetworkView attached to the same GameObject), or you can use this.GetComponent<NetworkView>() to get the NetworkView component attached to the current GameObject.

Either way, you'll be retrieving the same NetworkView, and utilizing it for its RPC functionality.

Your desired strategy of calling the method: Table_Manager.networkview.rpc("Table_Manager",RPCMode.All,25); will not work. In this case you'd be treating Table_Manager like a static class. In which case, you wouldn't have access to instance variables like networkview. You can however make some kind of GameManager object in your scene. Attach your Table_Manager script to it, then create a static function like the following:

static void GetScore() {
    GameObject.Find("GameManger").GetComponent<Table_Manager>().
         networkview.RPC("Table_Manager",RPCMode.All,25);
}

I'm not really sure what GetScore is supposed to do, or why it's trying to call a Table_Manager method on all the clients, but the above is how you'd write that.

\$\endgroup\$
0
\$\begingroup\$

Be aware that a RPC function has to be unique within a scene. I wrote an blogpost explaining this in detail: http://parttimeindie.com/2015/05/08/understanding-and-using-rpcs-in-unity/

To Summarize the blogpost:

A RPC Method Signature has to be Unique inside a Scene, you can not have multiple instances of one script or Two Scripts with a Method with the same Name or a "random" instance will get called.

The Solution i use passes the networkviewid which is observing the script as well as the other input parameters. The receiver searches all networkviews for the one with the networkviewId passed and then looks inside the networkview.observed and calls the method there. This way you can have multiple instances of one script or methods with the same signature in different scripts. Hope this is somewhat clear, please take a look at the blogpost if you want to have it in more detail.

P.s.: Sry that i did not wrote more in my first post i did not know the rules, shame on me ;)

Regards Niki

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .