- Home /
RPCMode.Server works in one spot but not another
Hello, so my problem which I just can't seem to solve is that I have the line
networkView.RPC("SOMERANDOMFUNCTION",RPCMode.Server);
in two places in a script but it only runs in one place. That is what really gets me. I have two scripts one is server.cs and the other is client.cs. Server.cs runs only on the server and client.cs runs on every machine (including server). I have two RPC calls in client.cs:
networkView.RPC("sendLevelHasLoaded",RPCMode.Server);
networkView.RPC("requestJoin",RPCMode.Server);
The first one runs, the second one will not. I have no idea why.
Below are the snippets of my code
client.cs
void OnLevelWasLoaded(int level) {
if (!enabled) {return;}//only run if the script is enabled
Network.isMessageQueueRunning = true;
Network.SetSendingEnabled(0,true);
if (Network.isServer) {
cont_server.setPlayerHasLoaded(networkView.owner);
} else {
networkView.RPC("sendLevelHasLoaded",RPCMode.Server);
}
}
int lastLevelPrefix = 0;
[RPC]
void loadMap(string inMap, int levelPrefix) {
lastLevelPrefix = levelPrefix;
Network.SetSendingEnabled(0,false);
Network.isMessageQueueRunning = false;
Network.SetLevelPrefix(levelPrefix);
Application.LoadLevel(inMap);
}
The above works fine the below does not.
void sendRequestJoin() {
if (Network.isServer) {
cont_server.allowPlayer(networkView.owner);
} else {
networkView.RPC("requestJoin",RPCMode.Server);
}
}
[RPC]
public void requestAccept() {
Debug.Log ("From Server - Accepted");
}
[RPC]
public void requestDenied() {
}
void leaveServer() {
networkView.RPC("leaveServer",RPCMode.Server);
}
// Use this for initialization
void Start () {
sendRequestJoin();
}
Server.cs
int levelPrefix = 0;
void sendLoadMap(string inMap) {
Network.RemoveRPCsInGroup(0);
Network.RemoveRPCsInGroup(1);
levelPrefix++;
networkView.RPC("loadMap",RPCMode.AllBuffered,inMap,levelPrefix);
}
public void setPlayerHasLoaded(NetworkPlayer inId) {
Debug.Log ("howdy"+inId);//THIS WILL PRINT WITH PROPPER IDs
//everyoneHasLevelLoaded = true;
for (int i = 0; i < players.Count; i++) {
if (players[i].owner == inId) {
players[i].hasLoadedLevel = true;
} else if (!players[i].hasLoadedLevel) {
everyoneHasLevelLoaded = false;
}
}
}
[RPC]
void sendLevelHasLoaded(NetworkMessageInfo info) {//the client will send this once they have loaded their level
setPlayerHasLoaded(info.sender);
}
Again the above will run but below will not.
public void allowPlayer(NetworkPlayer inId) {
if (networkView.owner == inId) {
cont_client.requestAccept();
} else {
networkView.RPC("requestAccept",inId);//accept them
}
//add them to the player list
players.Add(new player(inId));
}
[RPC]
void requestJoin(NetworkMessageInfo info) {
Debug.Log ("CHERRO");//THIS WILL NEVER PRINT
if (true) {//allow everyone for right now
allowPlayer(info.sender);
}
}
I observed a thing while working on my project once that the flow of intializing objects while starting the game takes some time, so I prefer not to make any network calls in the Start() and Awake() function. because you can't tell if the object is ready to receive or not. So make your call in other functions.
void Start () {
sendRequestJoin();
}
try avoiding this and adding it to Update or something else like OnPlayerConected or OnConnectedToServer().
You sir are a gentleman and a scholar! That was the problem, thank you so much.
Your answer
Follow this Question
Related Questions
Network how to send data/activate function 1 Answer
ClientRpc not called on Client 1 Answer
Totally lost with UNET - Client to Server Networking 3 Answers
Networking: RPC To Specific Player? 1 Answer
RPC not being called 1 Answer