Hello, I have one problem and one that I have a script to connect to the server. And when I connect and everything is fine. But I also script to change the map after 20 minutes. Until after 20 minutes, and loads the second map. And now when someone joins, it is again in the first map. I want to let them join the map, in which the founder of the site. Anyone ideas?
You can use a buffered RPC call to inform new players of changes to the game state :
http://unity3d.com/support/documentation/ScriptReference/NetworkView.RPC.html?from=RPC
Please give me a code or video tutorial.
Help
The documentation has a complete and fully explained example in the 3 supported languages.
I know but there anywhere I can’t find a solution to MY problem.
eh… I gave you a solution being the RPC call, you only have to understand what it does, implement it and your done.
Yes, you gave me solution being RPC call, but how to use it?
Network script (+chat)
DontDestroyOnLoad(this);
var mySkin : GUISkin;
private var gameNameType = "UnityChat"; // leave this alone. then we will see all other builds of this example
var myGameName : String = "thylaxene's Chat";
var gameDesc : String = "Very Basic Chat App";
var userName : String;
private var timeoutHostList = 0.0;
private var lastHostListRequest = -1000.0;
private var hostListRefreshTimeout = 10.0;
public var serverPort = 25002;
private var natCapable : ConnectionTesterStatus;
private var filterNATHosts = false;
private var doneProbingPublicIP = false;
private var hideTest = true;
private var windowRect = Rect (10,10,400,100);
private var windowRectT = Rect (200,400,500,200);
var scrollViewVector : Vector2;
private var outputText : String = "";
private var yourInputText : String = "";
// Enable this if not running a client on the server machine
//MasterServer.dedicatedServer = true;
function OnFailedToConnectToMasterServer(info: NetworkConnectionError) {
Debug.Log(info);
Application.LoadLevel("MainMenu");
Debug.Log(userName + " Disconnected from Master server");
networkView.RPC ("SendMessage", RPCMode.All, (userName + " Disconnected from Master server"));
}
function OnFailedToConnect(info: NetworkConnectionError) {
Debug.Log(info);
Application.LoadLevel("MainMenu");
Debug.Log(userName + " Failed connect");
}
function OnDisconnectedFromServer (info: NetworkDisconnection) {
Debug.Log(info);
Application.LoadLevel("MainMenu");
Debug.Log(userName + " Disconnected from server");
networkView.RPC ("SendMessage", RPCMode.All, (userName + " Disconnected from server"));
}
function OnServerInitialized () {
networkView.RPC ("SendMessage", RPCMode.AllBuffered, (userName + " has connected"));
Application.LoadLevel("Level2");
}
function OnConnectedToServer () {
networkView.RPC ("SendMessage", RPCMode.All, (userName + " has connected"));
Application.LoadLevel("Level2");
}
function OnGUI () {
GUI.skin = mySkin;
windowRect = GUILayout.Window (0, windowRect, MakeWindow, "Options");
windowRectT = GUILayout.Window (1, windowRectT, MakeTranscriptWindow, "Chat");
}
function Awake () {
// Start connection test
natCapable = Network.TestConnection();
// What kind of IP does this machine have? TestConnection also indicates this in the
// test results
if (Network.HavePublicAddress())
Debug.Log("This machine has a public IP address");
else
Debug.Log("This machine has a private IP address");
}
function MakeTranscriptWindow () {
GUILayout.Label ("Chat Log:");
scrollViewVector = GUILayout.BeginScrollView (scrollViewVector);
GUILayout.TextArea (outputText);
GUILayout.EndScrollView();
GUILayout.Label("Write Message: ");
yourInputText = GUILayout.TextField (yourInputText);
if (GUILayout.Button ("Send")) {
yourInputText = userName + ": " + yourInputText;
networkView.RPC ("SendMessage", RPCMode.All, yourInputText);
yourInputText = "";
}
}
// this is where the magic is. when this function is called via networkView.RPC
// everyone's chat log is updated
@RPC
function SendMessage (text : String) {
outputText += (text + "\n");
}
function MakeWindow (id : int) {
GUILayout.BeginHorizontal ();
GUILayout.Label ("User Name:");
userName = GUILayout.TextField (userName);
GUILayout.EndHorizontal ();
hideTest = GUILayout.Toggle(hideTest, "Hide test info");
if (!hideTest) {
// Start/Poll the connection test, report the results in a label and react to the results accordingly
natCapable = Network.TestConnection();
if (natCapable == -2)
GUILayout.Label("Problem determining NAT capabilities");
else if (natCapable == -1)
GUILayout.Label("Undetermined NAT capabilities");
else if (natCapable == 0) {
GUILayout.Label("Cannot do NAT punchthrough, filtering NAT enabled hosts for client connections, impossible to run a server.");
filterNATHosts = true;
Network.useNat = false;
}
else if (natCapable == 1) {
if (doneProbingPublicIP)
GUILayout.Label("Non-connectable public IP address (port "+ serverPort +" blocked), NAT punchthrough can circumvent the firewall.");
else
GUILayout.Label("NAT punchthrough capable. Enabling NAT punchthrough functionality.");
// NAT functionality is enabled in case a server is started, clients enable this based on if the host requires it
Network.useNat = true;
} else if (natCapable == 2) {
GUILayout.Label("Directly connectable public IP address.");
Network.useNat = false;
} else if (natCapable == 3) {
GUILayout.Label("Non-connectble public IP address (port " + serverPort +" blocked), running a server is impossible.");
Network.useNat = false;
if (!doneProbingPublicIP) {
natCapable = Network.TestConnectionNAT();
doneProbingPublicIP = true;
}
}
else if (natCapable == 4) {
GUILayout.Label("Public IP address but server not initialized, it must be started to check server accessibility.");
Network.useNat = false;
}
if (GUILayout.Button ("Retest connection")) {
Debug.Log("Redoing connection test");
doneProbingPublicIP = false;
natCapable = Network.TestConnection(true);
}
}
if (Network.peerType == NetworkPeerType.Disconnected) {
GUILayout.BeginHorizontal();
GUILayout.Space(10);
// Start a new server
if (GUILayout.Button ("Start Server")) {
Network.InitializeServer(32, serverPort);
MasterServer.RegisterHost(gameNameType, myGameName, gameDesc);
}
// Refresh hosts
if (GUILayout.Button ("Refresh available Servers") || Time.realtimeSinceStartup > lastHostListRequest + hostListRefreshTimeout) {
MasterServer.RequestHostList (gameNameType);
lastHostListRequest = Time.realtimeSinceStartup;
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.Space(5);
var data : HostData[] = MasterServer.PollHostList();
for (var element in data) {
GUILayout.BeginHorizontal();
// Do not display NAT enabled games if we cannot do NAT punchthrough
if ( !(filterNATHosts element.useNat) ) {
var name = element.gameName + " " + element.connectedPlayers + " / " + element.playerLimit;
GUILayout.Label(name);
GUILayout.Space(5);
var hostInfo;
hostInfo = "[";
for (var host in element.ip) {
hostInfo = hostInfo + host + ":" + element.port + " ";
}
hostInfo = hostInfo + "]";
//GUILayout.Label("[" + element.ip + ":" + element.port + "]");
GUILayout.Label(hostInfo);
GUILayout.Space(5);
GUILayout.Label(element.comment);
GUILayout.Space(5);
GUILayout.FlexibleSpace();
if (GUILayout.Button("Connect")) {
// Enable NAT functionality based on what the hosts if configured to do
Network.useNat = element.useNat;
if (Network.useNat)
print("Using Nat punchthrough to connect");
else
print("Connecting directly to host");
Network.Connect(element.ip, element.port);
}
}
GUILayout.EndHorizontal();
}
} else {
if (GUILayout.Button ("Disconnect")) {
Debug.Log(userName + " Disconnected from server");
networkView.RPC ("SendMessage", RPCMode.All, (userName + " Disconnected"));
Network.Disconnect();
MasterServer.UnregisterHost();
}
GUILayout.FlexibleSpace();
}
}
function OnNetworkInstantiate (info : NetworkMessageInfo) {
if (networkView.isMine)
Debug.Log("New object instanted by me");
else
Debug.Log("New object instantiated by " + info.sender);
}
You have about 20 or more RPC calls in your script already, if you understand how they work you can easily copy one and edit it to suit your needs.
but i don’t understand
From the example in the documentation :
This is the command to send the RPC:
networkView.RPC("SpawnBox", RPCMode.AllBuffered, viewID, transform.position);
So basicly this command sends the last 2 parameters to all connected players and all players that will connect at a later stage. It sends the command to the “SpawnBox” function and the receiving end will execute whatever is in the command.
@RPC
function SpawnBox (viewID : NetworkViewID, location : Vector3) {
// code to be executed here
}
Grow you cant make a game without basic skills that needs for such project. You cant hope that unity community will do everything for you. You should thank appels that he show you the door now its your job to get threw it not his.
Understand what you want to create how you want to create it, and than you will be able to do such stuff by your self without problem.