- Home /
other players and the server does not receive networkView.RPC
Hello, I have a problem, I made a server and a client with login and password, so far everything works, but for some reason, when I change the scene, after instantiating the player, if I call networkview.RPC, the server and other clients do not receive the RPC, and also returns no error, the instantiated object has NetworkView, following the example code of my player (cube):
using UnityEngine;
using System.Collections;
public class Characters : MonoBehaviour {
public int id;
public string name;
public int level;
public int sex;
public int classNumber;
public string account;
public Vector3 pos;
public Quaternion rot;
public int map;
public TextMesh text;
public bool viewName = true;
public bool isMine = true;
void Start () {
transform.position = pos;
transform.rotation = rot;
if(!viewName)
text.text = "";
else
text.text = name;
}
void Update () {
pos = transform.position;
rot = transform.rotation;
isMine = networkView.isMine;
if(isMine)
{
networkView.RPC("receiveData",RPCMode.All,transform.position,transform.rotation,name,level,classNumber,sex,networkView.viewID);
}
}
[RPC]
void receiveData(Vector3 pos,Quaternion rot,string name,int level,int classNumber,int sex,NetworkViewID view)
{
if(!isMine)
{
transform.position = pos;
transform.rotation = rot;
this.name = name;
this.level = level;
this.classNumber = classNumber;
this.sex = sex;
viewName = true;
}
}
void OnApplicationQuit()
{
networkView.RPC("PlayerLeave",RPCMode.Others,account);
networkView.RPC("SavePlayer",RPCMode.Server,id,level,classNumber,pos,map);
}
[RPC]
void PlayerLeave(string login)
{
}
[RPC]
void SavePlayer(int id, int level,int classNumber, Vector3 pos, int map)
{
}
}
Thanks.
Please remove the massive amounts of excess witespace from your code. Are you getting any errors or warnings on any of the clients/server?
Answer by akauper · Apr 15, 2014 at 08:21 AM
I believe you have some networking logic errors here.
All players have a gameobject with this script in their scene. Within Update all players set the "isMine" variable equal networkView.isMine. Then all players send an RPC to ALL other players if the networkview is theirs, which, for each of them, it is.
Basically everyone is sending the "receiveData" RPC every frame and everyone is receiving everyone else's "receiveData" RPC every frame.
I think what you are experiencing is a lockup. Where players positions and other values are so constantly being set to the same value that no movement or change is possible.
think I'm doing something wrong related to viewID explain me something, networkView.RPC only sends to the objects with the same viewID? because the tests I did, it only works with objects with the same viewID
I understand now, but it seems the problem is that the object is instantiated on the client with the viewId that is different from the server
Think of it this way:
Client1, Client2, and Server all have a gameobject in their scene. This gameobject has a NetworkViewID attached to it.
When using Network.Instantiate() or allocating a ViewID the server sets the internal information on the networkview of each of those players (including the server). It sets the 'owner' variable to whoever called Network.Instantiate() or Network.AllocateViewID(). It then sets the NetworkViewID to the same value on all gameobjects.
You come along and are trying to, lets say, update the value of a variable (a boolean variable well call exampleVar). Client 1 executes (within a method): exampleVar = true; networkView.RPC("Example$$anonymous$$ethod", RPC$$anonymous$$ode.OthersBuffered, exampleVar);
Unity then looks for an [RPC] tagged method named "Example$$anonymous$$ethod" on any script attached to a GameObject with the SA$$anonymous$$E NetworkViewID as the sending client's gameobject.
Networking is about SYNCING two or more games togethor. NetworkViewID acts as a way for each game to 'know' that their gameobject is supposed to be the same as the other gameobjects with the same NetworkViewID