- Home /
In another script there was a line blocking any RPC immediately after the client connects to the game. This way the necessary RPC couldn't pass.
Help with Network Script. Game with multiple Maps.
Hey guys.
So I try making a script which makes it so that when a new player connects to a server he automatically loads the map currently played on the server.
Here is my script:
private var Map : String;
private var lastLevelPrefix = 0;
function Awake ()
{
// Network level loading is done in a separate channel.
DontDestroyOnLoad(this);
networkView.group = 1;
}
function OnConnectedToServer() {
var viewID : NetworkViewID= networkView.viewID;
networkView.RPC("CallServerSync", RPCMode.Server, viewID);
}
//Here was the part which makes it so that the server loads the selected map. I deleted it for this question
@RPC
function LoadLevel (level : String, levelPrefix : int)
{
lastLevelPrefix = levelPrefix;
// There is no reason to send any more data over the network on the default channel,
// because we are about to load the level, thus all those objects will get deleted anyway
Network.SetSendingEnabled(0, false);
// We need to stop receiving because first the level must be loaded first.
// Once the level is loaded, rpc's and other state update attached to objects in the level are allowed to fire
Network.isMessageQueueRunning = false;
// All network views loaded from a level will get a prefix into their NetworkViewID.
// This will prevent old updates from clients leaking into a newly created scene.
Network.SetLevelPrefix(levelPrefix);
Application.LoadLevel(level);
yield;
yield;
// Allow receiving data again
Network.isMessageQueueRunning = true;
// Now the level has been loaded and we can start sending out data to clients
Network.SetSendingEnabled(0, true);
}
@RPC
function CallServerSync(viewID : NetworkViewID){
networkView.RPC("GetServerInfo", viewID.owner,Map);
}
@RPC
function GetServerInfo(ServerMap:String){
Debug.Log("Server got called!");
Map=ServerMap;
Network.RemoveRPCsInGroup(0);
Network.RemoveRPCsInGroup(1);
networkView.RPC( "LoadLevel", RPCMode.AllBuffered, Map, lastLevelPrefix + 1);
}
@script RequireComponent(NetworkView)
The script is attached to the Main Camera which handles the menu.
The part of the server works. CallServer Sync gets called but GetServerInfo doesn't. I don't get any errors on the client. The Client just get stuck on the "Starting the game"-Screen.
I really hope someone can help me.
Edited:
I changed the CallServerSync function like Mike said but that didn't solve my problem. Nothing really changed. GetServerInfo still doesn't get called on the client.
Update:
I currently went back to another method where I pass the information through the comment because this way doesn't seem to work like I wanted it to. If some of you still have an idea of how I could get this method working I would be happy to hear it.
Answer by whydoidoit · Jun 24, 2012 at 01:58 PM
Your call Server Sync function looks a bit strange - I think it should look like this:
@RPC
function CallServerSync(viewID : NetworkViewID){
networkView.RPC("GetServerInfo", RPCMode.All,Map);
}
Also - you are aware that networkView.RPC("CallServerSync", RPCMode.Server, viewID);
will not happen if you are running that code on the server.
Actually on further reflection you are doing that ServerSync call every time a client connects but it is talking to all clients - then your GetServerInfo call is also talking to all clients.
You should:
issue a call on the server when the client connects passing the networkViewId of the client
issue a GetServerInfo call on the single specific client passing the map reference
just load the level in GetServerInfo not issue another RPC.
In my script I tried to do the exact same thing. When you connect to the server ( so OnConnectedToServer) I call ServerSync and pass the networkView.viewID:
var viewID : NetworkViewID= networkView.viewID;
networkView.RPC("CallServerSync", RPC$$anonymous$$ode.Server, viewID);
Then the server calls GetServerInfo only at the networkview attached to the viewID passed to the CallServerSync function:
var NV: NetworkView = NetworkView.Find(viewID); // Finds the NetworkView with the viewID passed to the function
NV.RPC("GetServerInfo", RPC$$anonymous$$ode.All,$$anonymous$$ap); //Calls only at the certain Networkview
If I screwed something up because I don't quite understood what you want to say then please could you explain it some further.
You need to address the call to the connecting player:
networkView.RPC("GetServerInfo", viewID.owner, $$anonymous$$ap);
This send it to just that player - the RPC$$anonymous$$ode.All is sending it to everyone...
So I changed it and edited in my question but that didn't solved the problem that the function doesn't get called.
So you are getting the CallServerSync, that object exists on all players and is a scene object with a fixed id?
Answer by ExTheSea · Jul 15, 2012 at 10:20 PM
So I'm gonna reactivate this question because I tried some stuff as a result of the master server going down.
Now I tried a more simple version:
function OnConnectedToServer()
{
yield;
yield;
var ClientID = networkView.viewID;
transform.networkView.RPC("PlayerConnect", RPCMode.Server, ClientID);
}
@RPC
function PlayerConnect(Client : NetworkViewID){
if(Map1) //is true if Map 1 is played
networkView.RPC("LoadLevel1", Client.owner);
if(Map2) //is true if Map 2 is played
networkView.RPC("LoadLevel2", Client.owner);
}
@RPC
function LoadLevel1()
{
Application.LoadLevel(1);
}
@RPC
function LoadLevel2()
{
Application.LoadLevel(2);
}
function OnNetworkLoadedLevel(){
LevelLoaded=true;
}
This is a part of a script attached to the MainCamera which has a NetworkView. Also in the Awake function I put in DontDestroyOnLoad(this).
That doesn't really work unfortunately. As a server I get error messages that the RPC wasn't called because PlayerConnect doesn't exist in a random object in the scene. The object stays the same each time I try but it's always a different one when I change Map/Scene.
The Client can't load the map as a result of that.
I hope someone find the problem in my code. I really do as I get this kind of error everytime I try to pass the map through RPC's.
Every push in the right direction is much appreciated.
Now I get "RPC failed because the script couldn't be loaded. The function is "PlayerConnect"" all of the sudden. Don't know why. I don't have a missing script or an empty one attached to the $$anonymous$$ainCamera.
Follow this Question
Related Questions
Can not make RPC calls during Start() 1 Answer
Does the position change need state synchronization? 2 Answers
RPC Player Name and show it above head 0 Answers
AllocateViewID over RPC but the NetworkView doesn't exist 2 Answers
View ID AllocatedID: # not found during lookup. Strange behaviour may occur 1 Answer