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.
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. (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. As I said, I’d rather just use null but that doesn’t work here.
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.
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.
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 ();
}
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
There seems to be a bug here. Has anyone had any problems with trying to detect key strokes this way?