Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Matt 5 · Mar 19, 2012 at 05:46 PM · multiplayernetworkstrings

Adding Entered name as Player

Hi everyone I'm using this code from the following web site but what I want to do is take what ever name the players enter be put in the private var chosenPlayerNames as well as the switch statement for chosenPlayerInt.

Could someone help?

/ This file is part of the Unity networking tutorial by M2H (http://www.M2H.nl) The original author of this code is Mike Hergaarden, even though some small parts are copied from the Unity tutorials/manuals. Feel free to use this code for your own projects, drop us a line if you made something exciting! / #pragma strict #pragma implicit #pragma downcast

var serverPort : int = 45671; var gameName : String = "FPS_Lobby";

private var launchingGame : boolean = false; private var showMenu : boolean = false;

private var playerList : Array = new Array(); class PlayerInfo { var username : String; var player : NetworkPlayer; }

private var serverMaxPlayers : int = 16; private var serverTitle : String = "Loading.."; private var serverPasswordProtected : boolean = false; private var serverGameTypeSelected : String = ""; private var serverFriendlyFire : boolean = false;

private var playerName : String = "";

private var mainMenuScript : MainMenu;

function Awake(){ showMenu=false; }

function Start(){ mainMenuScript = MainMenu.SP; }

function EnableLobby(){ playerName = PlayerPrefs.GetString("playerName");

 lastRegTime=Time.time-3600;
 
 launchingGame=false;
 showMenu=true;
 
 var chat : LobbyChat = GetComponent(LobbyChat);        
 chat.ShowChatWindow();

}

function OnGUI () { if(!showMenu){ return; }

 //Back to main menu
 if(GUI.Button(Rect(40,10,150,20), "Back to main menu")){
     leaveLobby();
 }
 
 if(launchingGame){        
     launchingGameGUI();
     
 } else if(!Network.isServer && !Network.isClient){
     //First set player count, server name and password option            
     hostSettings();
     
 } else {
     //Show the lobby        
     showLobby();
 }

}

function leaveLobby(){ //Disconnect fdrom host, or shotduwn host if (Network.isServer || Network.isClient){ if(Network.isServer){ MasterServer.UnregisterHost(); } Network.Disconnect(); yield WaitForSeconds(0.3); }

 var chat : LobbyChat = GetComponent(LobbyChat);
 chat.CloseChatWindow();
     
 mainMenuScript.OpenMenu("multiplayer");
 
 showMenu=false;

}

private var hostSetting_title : String = "No server title"; private var hostSetting_players : int = 16; private var hostSetting_password : String = ""; private var hostgameTypeSelected : String = ""; private var gameTypeInt : int = 0; private var gameTypeStrings : String[] = ["DM", "TDM", "DOM"]; private var hostFF : boolean = false;

function hostSettings(){

 GUI.BeginGroup (Rect (Screen.width/2-175, Screen.height/2-75-50, 350, 150));
 GUI.Box (Rect (0,0,350,150), "Server options");
 
 GUI.Label (Rect (10,20,150,20), "Server title");
 hostSetting_title = GUI.TextField (Rect (175,20,160,20), hostSetting_title);

 GUI.Label (Rect (10, 40, 150, 20), "Game Type");
 gameTypeInt = GUI.Toolbar( Rect(175, 40, 160, 20), gameTypeInt, gameTypeStrings);
 hostgameTypeSelected = gameTypeStrings[gameTypeInt];

 GUI.Label (Rect (10,60,150,20), "Max. players (2-32)");
 hostSetting_players = parseInt(GUI.TextField (Rect (175,60,160,20), hostSetting_players+""));
 
 GUI.Label (Rect (10,80,150,50), "Password\n");
 hostSetting_password = (GUI.TextField (Rect (175,80,160,20), hostSetting_password));
 
 if (hostgameTypeSelected != "DM")
 {
     GUI.Label (Rect (10, 100, 150, 20), "FriendlyFire: ");
     ffInt = GUI.Toolbar ( Rect (175, 100, 160, 20), ffInt, ffNames);
     
     switch (ffInt)
     {
     case 0:
         hostFF = false;
         break;
     case 1:
         hostFF = true;
         break;
     }
 }
 
 if(GUI.Button (Rect (100,115,150,20), "Go to lobby")){
     StartHost(hostSetting_password, parseInt(hostSetting_players), hostSetting_title, hostgameTypeSelected, hostFF);
 }
 GUI.EndGroup();

}

function StartHost(password : String, players : int, serverName : String, serverGameType : String, ff : boolean){ if(players<1){ players=1; } if(players>=32){ players=32; } if(password && password!=""){ serverPasswordProtected = true; Network.incomingPassword = password; }else{ serverPasswordProtected = false; Network.incomingPassword = ""; }

 serverTitle = serverName;
 serverGameTypeSelected = serverGameType;
 serverFriendlyFire = ff;

 Network.InitializeSecurity();
 Network.InitializeServer((players-1), serverPort);    

}

private var chooseTeamInt = 0; private var chooseTeamNames : String[] = ["Red", "Blue"]; private var ffInt = 0; private var ffNames : String[] = ["Off", "On"]; private var chosenPlayerInt = 0; private var chosenPlayerNames : String[] = ["", "", "", "", "", "", "", ""];

function showLobby(){ var players = ""; var currentPlayerCount : int =0; for (var playerInstance : PlayerInfo in playerList) { players=playerInstance.username+"\n"+players; currentPlayerCount++;
}

 GUI.BeginGroup (Rect (Screen.width/2-300, Screen.height/2-200, 600, 500));
 GUI.Box (Rect (0,0,400,200), "Game lobby");
 

 var pProtected="no";
 if(serverPasswordProtected){
     pProtected="yes";
 }
 GUI.Label (Rect (110,20,150,20), "Password protected: ");
 GUI.Label (Rect (250,20,100,100), pProtected);
 
 GUI.Label (Rect (110,40,150,20), "Server title: ");
 GUI.Label (Rect (250,40,100,100), serverTitle);

 GUI.Label (Rect (110, 60, 150, 20), "GameType Selected: ");
 GUI.Label (Rect (250, 60, 100, 100), serverGameTypeSelected);
 
 if (serverGameTypeSelected != "DM")
 {
     GUI.Label (Rect (110, 120, 150, 20), "Choose Team: ");
     chooseTeamInt = GUI.Toolbar ( Rect (250, 120, 100, 20), chooseTeamInt, chooseTeamNames);
     
     switch (chooseTeamInt)
     {
     case 0:
     PlayerPrefs.SetString("playerTeam", "RedTeam");
     break;
     case 1:
     PlayerPrefs.SetString("playerTeam", "BlueTeam");
     break;
     }
 }

 GUI.Label (Rect (110,80,150,20), "Players: ");
 GUI.Label (Rect (250,80,100,100), currentPlayerCount+"/"+serverMaxPlayers);
 
 GUI.Label (Rect (110,100,150,20), "Current players: ");
 GUI.Label (Rect (250,100,100,300), players);

 GUI.Label (Rect (250, 330, 150, 20), "Choose Player");
 chosenPlayerInt = GUI.Toolbar (Rect(0, 350, 600, 30), chosenPlayerInt, chosenPlayerNames);
 
 switch (chosenPlayerInt)
 {
  case 0:
     PlayerPrefs.SetString("ChosenPlayer", "");
  break;
  case 1:
     PlayerPrefs.SetString("ChosenPlayer", "");
  break;
  case 2:
     PlayerPrefs.SetString("ChosenPlayer", "");
  break;
  case 3:
     PlayerPrefs.SetString("ChosenPlayer", "");
  break;
  case 4:
     PlayerPrefs.SetString("ChosenPlayer", "");
  break;
  case 5:
     PlayerPrefs.SetString("ChosenPlayer", "");
  break;
  case 6:
     PlayerPrefs.SetString("ChosenPlayer", "");
  break;
  case 7:
     PlayerPrefs.SetString("ChosenPlayer", "");
  break;
 }

 if(Network.isServer){
     if(GUI.Button (Rect (110,140,100,20), "Start the game")){
         HostLaunchGame();
     }
 }else{
     GUI.Label (Rect (110,140,100,40), "Waiting for server..");
 }
 
 GUI.EndGroup();

}

function OnConnectedToServer(){ //Called on client //Send everyone this clients data playerList = new Array(); playerName = PlayerPrefs.GetString("playerName"); networkView.RPC("addPlayer",RPCMode.AllBuffered, Network.player, playerName);
}

function OnServerInitialized(){ //Called on host //Add hosts own data to the playerlist
playerList = new Array(); networkView.RPC("addPlayer",RPCMode.AllBuffered, Network.player, playerName);

 var pProtected : boolean = false;
 if(Network.incomingPassword && Network.incomingPassword!=""){
     pProtected=true;
 }
 var maxPlayers : int = Network.maxConnections+1;
 
 networkView.RPC("setServerSettings",RPCMode.AllBuffered, pProtected, maxPlayers, hostSetting_title, serverGameTypeSelected, serverFriendlyFire);
 

}

var lastRegTime : float = -60; function Update(){ if(Network.isServer && lastRegTime

@RPC function setServerSettings(password : boolean, maxPlayers : int, newSrverTitle : String, gameType : String, ff : boolean){ serverMaxPlayers = maxPlayers; serverTitle = newSrverTitle; serverPasswordProtected = password; serverGameTypeSelected = gameType; serverFriendlyFire = ff; if (ff == true) PlayerPrefs.SetString("FF", "ON"); else PlayerPrefs.SetString("FF", "OFF"); }

function OnPlayerDisconnected(player: NetworkPlayer) { //Called on host //Remove player information from playerlist networkView.RPC("playerLeft", RPCMode.All, player);

 var chat : LobbyChat = GetComponent(LobbyChat);
 chat.addGameChatMessage("A player left the lobby");

}

@RPC function addPlayer(player : NetworkPlayer, username : String){ Debug.Log("got addplayer"+username);

 var playerInstance : PlayerInfo = new PlayerInfo();
 playerInstance.player = player;
 playerInstance.username = username;        
 playerList.Add(playerInstance);

}

@RPC function playerLeft(player : NetworkPlayer){

 var deletePlayer : PlayerInfo;
 
 for (var playerInstance : PlayerInfo in playerList) {
     if (player == playerInstance.player) {            
         deletePlayer = playerInstance;
     }
 }
 playerList.Remove(deletePlayer);
 Network.RemoveRPCs(player);
 Network.DestroyPlayerObjects(player);

}

function HostLaunchGame(){ if(!Network.isServer){ return; }

 // Don't allow any more players
 Network.maxConnections = -1;
 MasterServer.UnregisterHost();    
 
 networkView.RPC("launchGame",RPCMode.All);

}

@RPC function launchGame(){ Network.isMessageQueueRunning=false; launchingGame=true; }

function launchingGameGUI(){ //Show loading progress, ADD LOADINGSCREEN? if (serverGameTypeSelected == "DM") { GUI.Box(Rect(Screen.width/4+180,Screen.height/2-30,280,50), ""); if(Application.CanStreamedLevelBeLoaded ((Application.loadedLevel+1))){ GUI.Label(Rect(Screen.width/4+200,Screen.height/2-25,285,150), "Loaded, starting the game!"); Application.LoadLevel( (Application.loadedLevel+1) ); }else{ GUI.Label(Rect(Screen.width/4+200,Screen.height/2-25,285,150), "Starting..Loading the game: "+Mathf.Floor(Application.GetStreamProgressForLevel((Application.loadedLevel+1))*100)+" %"); }
} else if (serverGameTypeSelected == "TDM") { GUI.Box(Rect(Screen.width/4+180,Screen.height/2-30,280,50), ""); if(Application.CanStreamedLevelBeLoaded ((Application.loadedLevel+2))){ GUI.Label(Rect(Screen.width/4+200,Screen.height/2-25,285,150), "Loaded, starting the game!"); Application.LoadLevel( (Application.loadedLevel+2) ); }else{ GUI.Label(Rect(Screen.width/4+200,Screen.height/2-25,285,150), "Starting..Loading the game: "+Mathf.Floor(Application.GetStreamProgressForLevel((Application.loadedLevel+2))*100)+" %"); }
} }

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ByteSheep · Mar 19, 2012 at 06:09 PM 1
Share

Don't quite understand what you are trying to do.. Could you try and explain alittle more? thanks

avatar image Matt 5 · Mar 19, 2012 at 06:13 PM 0
Share

The player saves a name in the beginning of the code I want to take that name that the player saves and add it into the player chosen names

avatar image ByteSheep · Mar 19, 2012 at 06:22 PM 0
Share

I'm trying to figure out in which variable the player name is currently stored, it isn't this or?:

playerName = PlayerPrefs.GetString("playerName");

Do you already know which variable the name is stored in? If you don't, but you're sure it is being saved somewhere, easiest thing to do would be to find the variable that holds the name through using Debug.Log();

You could try something like:

Debug.Log("Player name is: "+playerName);

and if it gives you the correct player name the user typed in you'll know you've got the right variable.. Tell me if I'm totally misinterpreting the question ;)

avatar image Matt 5 · Mar 19, 2012 at 06:26 PM 0
Share

I'm sorry I messed up the question I need to add another class that the current code is referenced in.

avatar image Matt 5 · Mar 19, 2012 at 06:26 PM 0
Share

/ This file is part of the Unity networking tutorial by $$anonymous$$2H (http://www.$$anonymous$$2H.nl) The original author of this code is $$anonymous$$ike Hergaarden, even though some small parts are copied from the Unity tutorials/manuals. Feel free to use this code for your own projects, drop us a line if you made something exciting! / #pragma strict #pragma implicit #pragma downcast

static var SP : $$anonymous$$ain$$anonymous$$enu;

private var join$$anonymous$$enuScript : Join$$anonymous$$enu; private var gameLobbyScript : GameLobby; private var multiplayerScript : $$anonymous$$ultiplayer$$anonymous$$enu;

private var requirePlayerName : boolean = false; private var playerNameInput : String = "";

function Awake(){ SP=this;

 playerNameInput = PlayerPrefs.GetString("playerName", "");
 requirePlayerName=true;

 
 join$$anonymous$$enuScript = GetComponent(Join$$anonymous$$enu);
 gameLobbyScript = GetComponent(GameLobby);
 multiplayerScript = GetComponent($$anonymous$$ultiplayer$$anonymous$$enu);    

 Open$$anonymous$$enu("multiplayer");

}

function OnGUI(){ if(requirePlayerName){ myWindowRect = GUILayout.Window (9, Rect(Screen.width/2-150,Screen.height/2-100,300,100), Name$$anonymous$$enu, "Please enter a name:");
} }

function Open$$anonymous$$enu(new$$anonymous$$enu : String){ if(requirePlayerName){ return; }

 if(new$$anonymous$$enu=="multiplayer-quickplay"){                    
     join$$anonymous$$enuScript.Enable$$anonymous$$enu(true);//quickplay=true    
     
 }else if(new$$anonymous$$enu=="multiplayer-host"){ 
     gameLobbyScript.EnableLobby();        
     
 }else if(new$$anonymous$$enu=="multiplayer-join"){ 
     join$$anonymous$$enuScript.Enable$$anonymous$$enu(false);//quickplay:false
     
 }else if(new$$anonymous$$enu=="multiplayer"){ 
     multiplayerScript.Enable$$anonymous$$enu();
     
 }else{            
     Debug.LogError("Wrong menu:"+new$$anonymous$$enu);    
     
 }

}

function Name$$anonymous$$enu(id : int){

 GUILayout.BeginVertical();
 GUILayout.Space(10);
         
 
 GUILayout.BeginHorizontal();
 GUILayout.Space(10);
     GUILayout.Label("Please enter your name");
 GUILayout.Space(10);
 GUILayout.EndHorizontal();
 
 
 GUILayout.BeginHorizontal();
 GUILayout.Space(10);
 playerNameInput = GUILayout.TextField(playerNameInput);
 GUILayout.Space(10);
 GUILayout.EndHorizontal();    
 
 
 
 
  GUILayout.BeginHorizontal();
 GUILayout.Space(10);
     if(playerNameInput.length>=1){
         if(GUILayout.Button("Save")){
             requirePlayerName=false;
             PlayerPrefs.SetString("playerName", playerNameInput);
             Open$$anonymous$$enu("multiplayer");
         }
     }else{
         GUILayout.Label("Enter a name to continue...");
     }
 GUILayout.Space(10);
 GUILayout.EndHorizontal();
 
 
 GUILayout.EndVertical();
 

}

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Players can't connect to my server 0 Answers

Creating a Multiplayer Scoreboard? 2 Answers

Set unity process as server and client? 0 Answers

How to control "portable hotspot" function on Android? 1 Answer

Problem with RPC attribute. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges