[SOLVED] The Method for Number Inputs & Limited Characters on Label

Started by Priabudiman, Fri 23/10/2015 07:59:22

Previous topic - Next topic

Priabudiman

Hello everyone.

I have managed to create a simple text box gui with input button and exit button.

However, the problem im having is, I want to limit the text box Input to numbers only, no alphabet and other characters. Also, i need to limit that number input, eg. Max 8 numbers.

Ive browsed the forum but to no available samples for this one, please help me with this if you can.

Thank you in advance.

My Code :

Code: ags
function Button1_OnClick(GUIControl *control, MouseButton button)
{
gChestLock.Visible = false;
}

function btnEnter_OnClick(GUIControl *control, MouseButton button)
{
  if (tbInput.Text == "BOB")
      player.Say("Yes");
      //next command
  else
  {
    gChestLock.Visible = false;
    player.Say("Hell No");
     //next command
  }
}

Khris

The only event a textbox has, afaik, is OnActivate, which is triggered by pressing return.

So you would have to create this manually by using on_key_press and a Label. While the GUI is visible, on_key_press will react to 0-9 and backspace being typed, and change the Label.Text accordingly.
Code: ags
  if (gNumber.Visible) {
    Label *l = lblNumber;
    String lt = l.Text;
    if (keycode >= eKey0 && keycode <= eKey9 && lt.Length < 8) l.Text = String.Format("%s%c", lt, keycode);
    if (keycode == ekeyBackspace && lt.Length > 0) l.Text = lt.Truncate(lt.Length - 1);
  }

Priabudiman

Hmm.. very well, ill try to understand the code. So basically i just have to create a blank GUI with label, and number buttons right? before putting the codes.

Monsieur OUXX

#3
Quote from: Priabudiman on Fri 23/10/2015 14:12:41
So basically i just have to create a blank GUI with label, and number buttons right?

You don't even have to create "number" buttons.
1. Create a Gui
2. Add a Label called "lblNumber"
3. In your global script, go to on_key_press and add Khris' script.

There you go. Now if you press any key between 0 and 9 (these keys wear built-in names eKey0 and eKey9), the digit appears in the Label. If you press eKeyBackspace, it deletes the last character in the Label. Therefore the Label now acts as a digits-only textbox.
 

Priabudiman

Thank you Khris, OUXX,

i did create a GUI with label as instructed.

placed the code in on_key_press in global script, like this :

Code: ags
//----------------------------------------------------------------------------------------------------
// on_key_press
//----------------------------------------------------------------------------------------------------
function on_key_press(eKeyCode keycode) 
{
  if (IsGamePaused()) keycode = 0;
  
  // "System Keys"
  if (keycode == eKeyCtrlQ) QuitGame(1); // Ctrl-Q
  if (keycode == eKeyF9) RestartGame(); // F9
  if (keycode == eKeyF12) SaveScreenShot("scrnshot.pcx");  // F12
  
  // Debugger Keys
  if (keycode == eKeyCtrlS) Debug(0,0); // Ctrl-S, give all inventory
  if (keycode == eKeyCtrlV) Debug(1,0); // Ctrl-V, version
  if (keycode == eKeyCtrlA) Debug(2,0); // Ctrl-A, show walkable areas
  if (keycode == eKeyCtrlX) Debug(3,0); // Ctrl-X, teleport to room
  
  //GUI Set On_key_Press
   if (gNumber.Visible) 
    {
    Label *l = lblNumber;
    String lt = l.Text;
    if (keycode >= eKey0 && keycode <= eKey9 && lt.Length < 8) l.Text = String.Format("%s%c", lt, keycode);
    if (keycode == ekeyBackspace && lt.Length > 0) l.Text = lt.Truncate(lt.Length - 1);
    }
}


but the compiler keep getting me error :

Code: ags
GlobalScript.asc(47): Error (line 47): undefined symbol 'ekeyBackspace'

Crimson Wizard

Quote from: Priabudiman on Fri 23/10/2015 17:33:47
but the compiler keep getting me error :

Code: ags
GlobalScript.asc(47): Error (line 47): undefined symbol 'ekeyBackspace'


Well, it is eKeyBackspace; all key constants start with capital letter in "Key".

Priabudiman

#6
Quote from: Crimson Wizard on Fri 23/10/2015 17:38:20
Well, it is eKeyBackspace; all key constants start with capital letter in "Key".

Now I'm sorry for not noticing that very small difference. I should have! thank you CW!

Crimson Wizard

#7
Hmmm.... no, it should not matter which numeric button you use. If you are able to type numbers in the text editor, AGS should work too.
Did you remove the TextBox from your GUI? It catches the input when on screen.


E: Wait, what is this?
Code: ags

if (IsGamePaused()) keycode = 0;

If your GUI makes game pause, it will break your typing too.

Try substituting with:
Code: ags

if (IsGamePaused() && !gNumber.Visible) keycode = 0;

Priabudiman

CW That one is already on the default on key press global script. I didn't touch anything up there, just slip the code under all the default codes.

And the problem is because the text box is still there... *doh*

now i just have to make a button to confirm the label text to make the puzzle working..

Thank you.. thank you everyone!

SMF spam blocked by CleanTalk