- Home /
Question by
HuskyPanda213 · Sep 09, 2013 at 05:24 PM ·
networkhostmigration
Host migration or so?
I would like to add host migration in my game, but it needs to have it that the level is NOT reset. Also I have read one that every answer I saw referred to. Though posting a chunk of code would be really useful I will provide my network code below.
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class NetworkMenu : MonoBehaviour {
public string CurrentMenu = "";
public string servername = "Server Name";
public int Port = 24444;
public List<MPPlayer> PlayerList = new List<MPPlayer>();
public GameObject Spawnpoint;
public GameObject Spawnedpl;
public bool matchstarted;
public bool Connected;
public string _Playername;
public static NetworkMenu ins;
public GameObject networkScoreManager;
public ScoreManager score;
public bool _start;
public GUIStyle text_style;
public GUIStyle gui_Style;
void Awake(){
ins = this;
NextMenu("Main");
networkScoreManager = GameObject.FindGameObjectWithTag("ScoreManager");
score = networkScoreManager.GetComponent<ScoreManager>();
_Playername = PlayerPrefs.GetString("PlayerName");
}
void Start(){
Set_GUI_Style();
}
private void Set_GUI_Style(){
gui_Style.alignment = TextAnchor.MiddleCenter;
text_style.alignment = TextAnchor.MiddleCenter;
}
void OnGUI(){
if(CurrentMenu == "Main"){
GUIMain();
}
if(CurrentMenu == "Start"){
GUIStart();
}
if(CurrentMenu == "Browser"){
GUIBrowser();
}
Lobby();
}
//GUI
private void GUIMain(){
if(!Network.isClient && !Network.isServer){
if(GUI.Button(new Rect(10,10,200,25),"Browser",gui_Style)){
NextMenu("Browser");
MasterServer.RequestHostList("354545Battle");
}
if(GUI.Button(new Rect(10,40,200,25),"Host",gui_Style)){
NextMenu("Start");
}
_Playername = GUI.TextArea(new Rect(10,70,200,25),_Playername,15,text_style);
if(GUI.Button(new Rect(10,100,200,25),"Save Name",gui_Style)){
PlayerPrefs.SetString("PlayerName", _Playername);
}
if(GUI.Button(new Rect(10,130,200,25),"Quit",gui_Style)){
Application.Quit();
}
}
}
private void GUIStart(){
//Starts The server
if(!Network.isClient && !Network.isServer){
if(GUI.Button(new Rect(10,10,200,25),"Start Server",gui_Style)){
StartServer();
Debug.Log("Started Server with name" + servername);
}
servername = GUI.TextArea(new Rect(10,40,200,25),servername,20,text_style);
if(GUI.Button(new Rect(10,70,200,25),"Back",gui_Style)){
NextMenu("Main");
}
}
}
void Update(){
//Allows for starting
if(Network.isServer){
if(Network.connections.Length >= 0 && !matchstarted){
//Server_SendSettings();
//Spawn();
_start = true;
}
}
}
public void Server_SendSettings(){
networkView.RPC("Player_LoadSettings",RPCMode.All,true);
networkScoreManager.networkView.RPC("FoundAllPlayers",RPCMode.All,true,Network.connections.Length + 1);
Debug.Log("Sending Match Settings");
}
public void Lobby(){
//Starts The match
if(Network.isServer && !matchstarted){
if(_start){
if(GUI.Button(new Rect(10,10,200,25),"Start Match",gui_Style)){
Server_SendSettings();
Network.maxConnections = 0;
}
if(GUI.Button(new Rect(10,40,200,25),"End Match",gui_Style)){
Network.Disconnect();
Application.LoadLevel(0);
}
}
}
}
private void GUIBrowser(){
if(!Network.isClient && !Network.isServer){
if(GUI.Button(new Rect(10,10,200,25),"Back",gui_Style)){
NextMenu("Main");
}
if(GUI.Button(new Rect(10,40,200,25),"Refresh",gui_Style)){
MasterServer.RequestHostList("354545Battle");
}
GUILayout.BeginArea(new Rect(Screen.width / 2, 0,Screen.width / 2,Screen.height),"Server List","box");
GUILayout.Space(20);
foreach(HostData hd in MasterServer.PollHostList()){
GUILayout.BeginHorizontal();
GUILayout.Label(hd.gameName);
if(hd.connectedPlayers < hd.playerLimit){
GUILayout.Label(hd.connectedPlayers + "/" + hd.playerLimit);
}
else{
GUILayout.Label("Match is full or has started");
}
if(GUILayout.Button("Connect")){
Network.Connect(hd);
}
GUILayout.EndHorizontal();
}
GUILayout.EndArea();
}
}
void OnPlayerConnected(NetworkPlayer player){
}
void OnConnectedToServer(){
Debug.Log("Connecting");
networkView.RPC("PlayerConnectedToServer",RPCMode.Others,_Playername);
if(matchstarted){
Network.Disconnect();
Application.LoadLevel(0);
}
Debug.Log("Done");
}
public void PlayerLeave(GameObject player){
Network.Destroy(player);
networkScoreManager.networkView.RPC("PlayerLeft",RPCMode.All,_Playername);
}
public void HostQuit(GameObject host){
networkView.RPC("KickAllPlayers",RPCMode.All);
}
void OnServerInitialized(){
}
public void StartServer(){
Network.InitializeServer(15,Port,false);
// Network.InitializeSecurity();
MasterServer.RegisterHost("354545Battle",servername,"Hosted Game");
}
public void Spawn(){
GameObject PlayerSpawned = Network.Instantiate(Spawnedpl,Spawnpoint.transform.position,Quaternion.identity,1) as GameObject;
PlayerSpawned.GetComponent<NetworkManager>()._name = _Playername;
}
//Next Menu Void
public void NextMenu(string nxt){
CurrentMenu = nxt;
}
void OnDisconnectedFromServer(){
Application.LoadLevel(0);
}
void OnPlayerDisconnected(NetworkPlayer id){
Network.DestroyPlayerObjects(id);
Network.RemoveRPCs(id);
}
//RPCs
[RPC]
void Player_Joined(){
}
[RPC]
void Player_LoadSettings(bool _matchstarted){
matchstarted = _matchstarted;
Debug.Log("Got Settings!");
Spawn();
}
}
public class MPPlayer{ public string PlayerName; public NetworkPlayer PlayerNetwork; }
Comment
Your answer
Follow this Question
Related Questions
Automatic Network Migration Manager 0 Answers
Create server without port 0 Answers
What is the easiest way to set up a LAN-game? 0 Answers
Host Migration ? 1 Answer