Parsing and Limiting Text Fields

I want to create a text field that will only allow the user to input values greater than 0 and less than or equal to 36. I then want to be able to output the inputed value to another place in the game. How could I do this?

This is what I’ve got so far…

var widthFeet = 25;
var heightFeet = 10;
var depthFeet = 30;

var tempWidth = "25";

function OnGUI () {
	GUI.TextField (Rect (10, 10, 200, 20), tempWidth, 2);
	parseFloat("tempWidth");
	if (tempWidth >0  tempWidth <= 36) {
		widthFeet = tempWidth;
	}
		//if (widthFeet > 0  widthFeet <= 36) {
			
		//}
}

Just for reference in case somebody searches this out, this script was corrected by doing it like this:

var widthFeet = "25";
//var heightFeet = "10";
//var depthFeet = "30";


function OnGUI () {
	var tempWidth = GUI.TextField (Rect (10, 10, 200, 20), widthFeet, 2);
	if (tempWidth != ""  Input.GetButton("Fire1")) {
		var tempWidthInt : int = parseInt (tempWidth);
		if (tempWidthInt >= 0  tempWidthInt <= 36) {
			widthFeet = tempWidth;
		}
	}
	else {
		widthFeet = tempWidth;
	}

}

Note, however, that using this particular script will generate errors when the user types in a character that is not a numeral. Any help with limiting the text box to only allow numeral character would be appreciated.

A couple of ways…1) Don’t accept anything other than numerical input in the first place:

private var nullChar : char;

function OnGUI () {
	if (Event.current.character < "0"[0] || Event.current.character > "9"[0]) {
		Event.current.character = nullChar;
	}
}

(Not sure how to set a char to null properly in Javascript, but that works.)

Or 2) Accept any input, but discard results that don’t parse:

try {
	var tempWidthInt : int = parseInt (tempWidth);
}
catch (err) {
	tempWidthInt = 0;
}

Although in that case, you could get fancy and attempt to extract only numerical values and parse that in the catch block.

–Eric

Ha… I’m lost. I am very definitely one of those artists who is trying to learn scripting, but can’t because it requires a completely different thought process.

So if I’m going by your first example, what do I do with nullChar after that and where should I put this in my script? would nullChar be widthFeet in my script?

nullChar is just a null character. I would prefer not to use it, and simply do “Event.current.character = null” like I would with other types of variables, but that doesn’t seem to be possible because I get an error message about not being able to convert null to char. Might be a bug or something I’m missing?

var widthFeet = "25"; 
//var heightFeet = "10"; 
//var depthFeet = "30"; 
private var nullChar : char;

function OnGUI () {
   if (Event.current.character < "0"[0] || Event.current.character > "9"[0]) { 
      Event.current.character = nullChar; 
   }

   var tempWidth = GUI.TextField (Rect (10, 10, 200, 20), widthFeet, 2); 
   if (tempWidth != ""  Input.GetButton("Fire1")) { 
      var tempWidthInt : int = parseInt (tempWidth); 
      if (tempWidthInt >= 0  tempWidthInt <= 36) { 
         widthFeet = tempWidth; 
      } 
   } 
   else { 
      widthFeet = tempWidth; 
   } 

}

Note that I didn’t test this. :wink: (I mean, this code…I’ve used the technique.) But to explain what it’s supposed to be doing, the “if (Event.current.character” part looks to see what the current character of the OnGUI event is. If the char is outside the range of 0…9, then it is set to null. This effectively blocks any non-numerical input. The “private var nullChar : char;” simply declares the nullChar variable and doesn’t set it to any value. Hence, it is null. I believe this is known as a hack. :wink: As I said, I’d rather just use null but that doesn’t work here.

–Eric

what is the zero in brackets for? The “[0]”. What is that doing?

That’s to specify a char, which is by definition a single character. You can think of a string as an array of chars, so to specify one of them, you can use the brackets. “Hello”[3] would give you “l” as a char. “Hello”[4] would give you “o”, and “Hello”[0] would give you “H”. You might think that just using “9” would be enough to specify of char of 9, however anything enclosed in quotes is a string, even if it’s just one character. So explicitly saying “9”[0] gives you a char of 9.

–Eric

ohhhhh! That’s awesome! I love this community so much. I am definitely a newcomer to coding, but this is the only community I can think of that doesn’t keep reminding me of that fact.

Thank you Eric!

Or you can simply do ‘0’ and ‘9’ :stuck_out_tongue:

That would be nice, but no, you can’t. :wink:

–Eric

You could do something like:

import System;

//...
if (Event.current.type == EventType.keyDown) {
	var c = Event.current.character;
	if (!Char.IsLetterOrDigit(c)  !Char.IsWhiteSpace(c)  !Char.IsControl(c))
		Event.current.Use ();
}

There is also Char.IsDigit, etc.

Cheers,
-Jon

Just tried the following code and it works:

if( Event.current.isKey  (Event.current.character < '0' || Event.current.character > '9') )
	Debug.Log("Not a digit: " + Event.current.character);
if( Event.current.isKey  (Event.current.character >= '0' || Event.current.character <= '9') )
	Debug.Log("A digit: " + Event.current.character);

The only problem that I found with this code is that if I don’t check if isKey and if no key is pressed, it still enters both ifs! not sure why that happens…

And with the isKey check on the if, if I click the arrow keys, for example, it also enters both ifs! Now that’s just crazy :stuck_out_tongue:

There seems to be a bug here. Has anyone had any problems with trying to detect key strokes this way?

We’re doing Javascript in this topic. :slight_smile: As for the other stuff, this works here:

if (Event.current.isKey) {
	if (Event.current.character < "0"[0] || Event.current.character > "9"[0]) 
		Debug.Log("Not a digit: " + Event.current.character);
	else
		Debug.Log("A digit: " + Event.current.character);
}

–Eric

Hehe, sorry! :wink: