My player name validation popup - Is there a better way?

Hi,

I’ve created a validation popup for my games player name screen, the basic functionality is:

  1. player enters name
  2. player hits go
  3. player name sent to php validation script
  4. php validation script sends result back
  5. game starts or gui texture with message appears advising user to re-enter their name.

What I have written is really clunky I could do with someone having a quick look through it to make some suggestions as to how I could improve it if that’s ok?

http://paste-it.net/public/a30f0bd/

var goBtnGUISkin:GUIStyle;
var inputFieldBgGUISkin:GUIStyle;
var inputFieldGUISkin:GUIStyle; 
var enterNameLabelGUISkin:GUIStyle;
private var playerNameValid:String;
private var playerNameValidated:boolean = false;
private var playerNameValidatedResult:Object;
private var displayInvalidUsernameBg:boolean;

function OnGUI() {

	//GUI.depth = 100;
	
	// Display 'Enter name'
	GUI.Label(Rect (Screen.width / 2 - 100,250,200,30),"Enter name:",enterNameLabelGUISkin);
	
	// Display input background
	GUI.Box(Rect (Screen.width / 2 - 145,285,290,67)," ",inputFieldBgGUISkin);

	// Record player name
	GameObject.Find("PlayerName").GetComponent("PlayerName").playerName = GUI.TextField (Rect (Screen.width / 2 - 115,Screen.height / 2 + 80,230,30),GameObject.Find("PlayerName").GetComponent("PlayerName").playerName,inputFieldGUISkin);
	
	// Set playerName var
	var playerName:String = GameObject.Find("PlayerName").GetComponent("PlayerName").playerName;
	
	// If go button pressed, load game.
	if(GUI.Button(Rect (Screen.width / 2 -63,Screen.height / 2 + 125,126,62)," ",goBtnGUISkin)) {
		Debug.Log("Button clicked");
		
		ValidateUsername(playerName);
	
	}
	
	// Display invalid username button
	if(displayInvalidUsernameBg) {
		
		if(GUI.Button(Rect (Screen.width / 2 - 172,Screen.height / 2 - 85,345,170)," ",invalidUsernameBgGUIStyle)) {
		
			displayInvalidUsernameBg = false;
		
		}
		
	}
}
var invalidUsernameBgGUIStyle:GUIStyle;
function Update() {
	
	if(playerNameValidated) {
			
		if(playerNameValidatedResult == "true") {

			Application.LoadLevel("Game");
		
		}
		else {
		
			playerNameValidated = false;
			Debug.Log("Player name invalid");
			
			// Display input background
			displayInvalidUsernameBg = true;
			
		}
		
	}
	
}
function ValidateUsername(playerName:String) {

	Debug.Log("Validating username...");
	
	// Create www url
	var URL:String = "http://*.php?username="+playerName;
	
	// Validate username using php username validator
	var validUsernameResult:WWW = new WWW(URL);
	
	// Wait for result
	yield validUsernameResult;
	
	Debug.Log(validUsernameResult.data);
	
	// Define player name validated result
	playerNameValidatedResult = validUsernameResult.data;
	
	// Set player validated flag
	playerNameValidated = true;
	
	Debug.Log("Username "+playerNameValidatedResult+" validated.");
	
}

Thanks,

eb_dev[/code]

The code doesn’t look too bad, really. The main speed improvement I could imagine is storing the results of the GameObject.Find calls:-

var playerNameScript: PlayerName;

function Start() {
    playerNameScript = GameObject.Find("PlayerName").GetComponent("PlayerName");
    ...
}

function OnGUI() {
    ...
    playerNameScript.playerName = GUI.TextField (...

}

GameObject.Find is relatively slow, so you will save quite a bit of time by calling it once and reusing the stored result.

Ok cool, cheers for looking over it and the note about optimisation. I’m new to coding in unity and keen to find out the best way of doing things, glad this part at least is ok!

Would you mind if I repurposed this code to use in my own projects? Thx!

Not at all, go right ahead, If you come up with any improvements be sure to let me know!