- Home /
RPC buffered calls
Hi! I'm making a small project in order to understand how to work with networking code in Unity. Here's what I'm trying to accomplish: I want to create an executable that displays two buttons (one to start a new server and the other one to join an existing one as a client). The server also has the capability of instantiating a scene using an RPC buffered call and the client should be able to replicate the server's output. Notice that the client should be able to connect to the server anytime (even after the scene instantiation took place).
I did some tests and, although the instantiation is working, there's something wrong with the RPC call. Here's the scenario test: when a client joins a server that has already instantiated the scene, the client doesn't execute the buffered RPC call. If I close the server's window, I get a message on the client (something along the lines of "The server has disconnected from the client") and the RPC call is made (and the scene is appropriately instantiated).
This is my connection script, which is attached to a game object that has a NetworkView component observing it. I simplified it as much as possible, do you mind helping me a little bit in order to determine where's my error? Thanks in advance!
using UnityEngine; using System.Collections;
public class Connection : MonoBehaviour { public Transform Scene; // The prefab that holds the scene we would like to instantiate.
// Use this for initialization
void Start () {}
// Update is called once per frame
void Update () {}
void OnGUI()
{
GUILayout.BeginArea(new Rect(10,10,200,250));
if(GUILayout.Button("CREATE SERVER"))
{
NetworkConnectionError err = Network.InitializeServer(32,25000,false);
if(err==NetworkConnectionError.NoError)
{
Debug.Log("Server initialized!");
}
}
if(GUILayout.Button("JOIN SERVER"))
{
NetworkConnectionError err = Network.Connect("127.0.0.1", 25000);
if(err==NetworkConnectionError.NoError)
{
Debug.Log("Successfully connected to server!");
}
}
GUILayout.EndArea();
if(Network.isServer)
{
GUILayout.BeginArea(new Rect(10,270,200,250));
if(GUILayout.Button("INSTANTIATE"))
{
Debug.Log("Instantiating scene...");
NetworkViewID SceneViewID = Network.AllocateViewID();
networkView.RPC("InstantiateScene", RPCMode.AllBuffered, SceneViewID);
}
GUILayout.EndArea();
}
}
[RPC]
void InstantiateScene(NetworkViewID SceneViewID)
{
Transform SceneTransform = Instantiate(Scene, Scene.position, Scene.rotation) as Transform;
if(SceneTransform==null)
{
Debug.Log("SceneTransform is null!");
}
NetworkView nView;
nView = SceneTransform.GetComponent<NetworkView>();
nView.viewID = SceneViewID;
Debug.Log("Executing RPC function InstantiateScene");
}
}
From what I understand, you seem to be doing things correctly. Not sure what's wrong. I am having an issue with Buffered RPCs as well.
The issue I'm having is that the buffered RPCs are sent to all currently connected clients. When the next client connects, that client receives them also. However, only the first subsequent client to connect gets them. After that, none do.
Are we having similar issues?