Text parser. clearing box and entering text when return is pressed

Started by snerzel, Thu 16/03/2017 21:34:28

Previous topic - Next topic

snerzel

OK I'm working with the text parser in ags and I can't figure out how to get text that the player submits to both trigger the appropriate event and clear the text from the text parser when enter/return is pressed. Also under what function should the text parser code be put?

The tutorial I've be following has neglected to explain this.   

dayowlron

Not sure of your question, however you would place the code in the on_key_press function
You would test for the Enter key with:
Code: ags
if (keycode == eKeyReturn) { 
    //process code goes here
    processCommand(txtParser.Text); //example
  }

To clear the text box you would just set its text property to "".
Code: ags

txtParser.Text = "";
Pro is the opposite of Con                       Kids of today are so much different
This fact can clearly be seen,                  Don't you know?
If progress means to move forward         Just ask them where they are from
Then what does congress mean?             And they tell you where you can go.  --Nipsey Russell

snerzel

Been experimenting with what you said. Still nothing happens. i.e. text entered into the textparser does nothing when enter/return is pressed or I get an error in the global script. What is the process code?

Gilbert

Actually the Enter key is treated a little differently in a text box.
Pressing the Enter key means "activating" what you just typed in the box.
So, in the editor, select the text box on the GUI and then choose the Events tab (the lightning icon) on its property pane. The text box should have only one event listed, OnActivate. Click the "..." on the space on the right of this event and a new function called something like TextBox1_OnActivate() will be created in the Global Script.
You may then put whatever responses you want to have inside this function whenever the player presses Enter while the text box is active.

snerzel

Thanks Gilbert! That is now working as it should, but I still can't figure out how to get the text to disappear after you press enter. so the player doesn't have to keep deleting the last thing they typed.

Slasher

Just declare the textbox String as null when pressing Return Key

Something like:
Code: ags
if (keycode == eKeyReturn) { 
   tbInput.Text == "";
//etc etc 
  }


Code: ags

tbInput.Text == "" // returns String as empty (tbInput is the name of the Text box}



Gilbert

@slasher: The OP already mentioned the "keycode" method didn't work. It's been a long time since I touched this, but AFAIK one indeed should use the OnActivate method.

For the text to disappear, as mentioned, set the Text property of the text box to empty. Check the script name of the text box (the default is something like "TextBox1" if you didn't change it), and put in the script:
Code: ags
TextBox1.Text = "";


However, since you destroy the content of the text box when you blank it, if you need to do something according to its contents (such as parsing the String in a text parser) you need to do so first before emptying it. Something like:
Code: ags

function TextBox1_OnActivate(GUIControl *control)
{
  //Do whatever actions you want here according to the contents of the text box
  //...
  
  TextBox1.Text=""; //blank the text box
}

Alternately, you may copy the content of the text box to another String first, and then blank the text box, so you may take action according to the contents of the new String instead, but hey, one step at a time! :wink:

Slasher

Gilbert

by adding to the on_key_press function in the Global:

Code: ags
function on_key_press(eKeyCode keycode) 
{
    if (keycode == eKeyReturn){
  // check Textbox text and run events 
    Label9.Text=""; // else if text is wrong
    return;
}
}


This removes label text so why should it not work for Textbox text?

Of course we normally do this with an added button on the gui to do the check...



Snarky

Because, as the manual explains:

QuoteIf a text box is on a currently displayed GUI, all standard keypresses (ie. letter keys, return and backspace) are diverted to the textbox instead of being passed to the on_key_press function.

Slasher


snerzel

Thanks everyone! (especially Gilbert). Had to mess around my script and naming order but I got all working now! THX  :grin:

SMF spam blocked by CleanTalk