I have been thinking about how to implement a control panel ingame, as far as the player using it for tasks ingame.
I have a model of a control panel now and was thinking of using the numpad for entering numbers in it. Only allowing 5-6 numbers, but is it not a good idea to use the numpad? are there more keyboards without a numpad? would this way restrict players without a numpad?
I am using a GUItext to diaply the numbers on the display of the CP, but I am having a hard time being able to enter the numbers with the numpad and being able to add other numbers.
I have this so far…with all the numbers but it isn’t working out.
/
/
if(Input.GetKeyDown(KeyCode.Keypad1))
{
CPURead.guiText.text = "1";
}
/
/
Would it be better to put colliders on the buttons and us onmouseclick or something to enter the numbers??
It worked for me. Did you hit numLock lol?
Yeah it works, but it will just make the guitext that number. I can’t figure out at all how to make it work so all numbers are inputted til there is 5-6 digits…
You need something outside of a method(not a local variable to the method i mean) that maintains the text to display, for length checking and such.
There are a lot of ways of doing this, this code should be straight forward and thats on purpose.
private string m_Digits = string.empty;
private int m_MaxDigits = 6;
// Snip your method
if(Input.GetKeyDown(KeyCode.Keypad1))
{
if (m_Digits.Length < m_MaxDigits)
{
m_Digits = m_Digits + "1";
CPURead.guiText.text = m_Digits;
}
else
{
// i don't know, a beep sound?
}
}
So would I have to repeat this for all the numpad keys?
Thats one way, you could also place the “possible” change in a temp var, always add to it in the if statement for the keys. at the bottom of the method check for the length, if it’s a go, update the gui text, if not, junk it.
Again there are a lot of ways to do it, it really should be your choice, also think about what the code is doing, i’m sure if you put your mind to it, you can optimize it.
I’m getting an error on the empty in this-
/
private string m_Digits = string.empty;
/
I got it, I had to make .Empty-
Now I need to get the backspace working with the keypad period key…
Thanks Landern you helped a lot. I understand the code now.
Why isn’t this code working-
/
/
if(Input.GetKeyDown(KeyCode.KeypadPeriod))
{
if (m_Digits.Length <= m_MaxDigits)
{
CPURead.guiText.text = CPURead.guiText.text.Substring(0, CPURead.guiText.text.Length - 1);
}
}/
/
It works, but then I can not type any more numbers on the guitext???
With this error-
/
/
ArgumentOutOfRangeException: Cannot be negative.
Parameter name: length
System.String.Substring (Int32 startIndex, Int32 length)
/
/
Also, I changed the max numbers which is 4, then hit the period to backspace it does the backspace but if I type another digit the one that was backspaced reappears???
Anyone know how to solve this??