Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* core.js
- Description : This script will hold the core elements for a dedicated server.
- Date : 21/01/2014 */
- /* static function StartServer(value, value, value, value, value)
- This function will initialize a dedicated server and register it on the master server with the parameters given,
- this function is static so parameters can be introduced from other scripts and it will also print to the server
- log-file. */
- static function StartServer(gameName : String, hostName: String, hostDescription : String, slots : int, port : int)
- {
- // Add unity's built in security layer.
- Network.InitializeSecurity();
- // Start the server. (max players, port, ipaddress)
- Network.InitializeServer(slots, port, !Network.HavePublicAddress);
- // Register the server with the master server. (Game Name, Server Name, Server Description)
- MasterServer.RegisterHost(gameName, hostName, hostDescription);
- // Set the server to be dedicated.
- MasterServer.dedicatedServer = true;
- // Write/print to the logfile,screen and console.
- file.WriteToFile("Initalizing Server.", "Server");
- Error.message = "Initalizing Server.";
- Debug.Log("Initalizing Server.");
- }
- /* function OnServerInitialized()
- This function will print to the server log-file, to the screen and the console when the server has started. */
- function OnServerInitialized()
- {
- file.WriteToFile("Server Initialized", "Server");
- Error.message = "Server Initialized";
- Debug.Log("Server Initialized");
- }
- /* function OnMasterServerEvent(Value)
- This function will check for master server events such as, server registered or registration failed and it
- will print to the screen and to the server log-file. */
- function OnMasterServerEvent(mse : MasterServerEvent)
- {
- // checks for events.
- if(mse == MasterServerEvent.RegistrationSucceeded)
- {
- file.WriteToFile("Server is Registered", "Server");
- Error.message = "Server is Registered";
- }
- if(mse == MasterServerEvent.RegistrationFailedNoServer)
- {
- file.WriteToFile("Server is not running", "Server");
- Error.message = "Server is not running";
- }
- }
- /* function OnDisconnectedFromServer(Value)
- This function is responsible for checking the server and players disconnection. */
- function OnDisconnectedFromServer(nw : NetworkDisconnection)
- {
- // if the server has disconnected then print to the console and server logfile.
- if(Network.isServer)
- {
- Debug.Log("Server Shutdown.");
- file.WriteToFile("Server Shutdown.", "Server");
- }
- // Client
- if(Network.isClient)
- {
- // If the player has disconnected, print to the console and server logfile.
- if(nw == NetworkDisconnection.Disconnected)
- {
- Debug.Log(login.username + " Disconnected.");
- file.WriteToFile(login.username + "Disconnected.", "Server");
- }
- // if the player has lost connection, print to the console and server logfile.
- if(nw == NetworkDisconnection.LostConnection)
- {
- Debug.Log(login.username + " has lost connection.");
- file.WriteToFile(login.username + " has lost connection.", "Server");
- }
- }
- Application.LoadLevel(Application.loadedLevel);
- }
- /* function OnPlayerConnected(player: NetworkPlayer)
- When the player is connected to the server, print to the console and server log-file the users username,
- ip and port. */
- function OnPlayerConnected(player : NetworkPlayer)
- {
- Debug.Log("Player " + player + " connected from " + player.ipAddress + ":" + player.port);
- file.WriteToFile("Player " + player + " connected from " + player.ipAddress + ":" + player.port, "Server");
- }
- function OnPlayerDisconnected(player : NetworkPlayer)
- {
- Network.RemoveRPCs(player);
- Network.DestroyPlayerObjects(player);
- Debug.Log("Player " + player + " disconnected");
- file.WriteToFile("Player " + player + " disconnected", "Server");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement