Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: C.T.C.B on Tue 28/05/2013 23:45:37

Title: String.Contains Text Parser
Post by: C.T.C.B on Tue 28/05/2013 23:45:37
[embed=425,349]
How would I use String.Contains with a Text Parser?
For example if the player types in "Hello My name is John", I want it to check if the it contains "Hello" and then Display "Hello"
[/embed]
Title: Re: String.Contains Text Parser
Post by: Phemar on Wed 29/05/2013 00:15:30
You would use String.IndexOf (String needle);

Eg:
Code (ags) Select
String input = "Hello World";

if (input.IndexOf("hello") != -1)
  Display ("Hello!");

else
  Display ("Input does not contain hello.");


Edit: Then again, if you're using a text parser you wouldn't need to use IndexOf, you just use Parser.ParseText.

Eg:
Code (ags) Select
String input = Game.InputBox ("Enter Text:");

Parser.ParseText (input);

if (Parser.Said("hello"))
  Display ("Hello!");

else
  Display ("Input does not contain Hello.");


This is of course assuming "Hello" isn't an ignored word :D
Title: Re: String.Contains Text Parser
Post by: C.T.C.B on Wed 29/05/2013 00:29:01
I guess that wasn't the best example since I want it so If the player types something before "Hello" such as "Also, let me just say hello first".
Title: Re: String.Contains Text Parser
Post by: Khris on Wed 29/05/2013 02:28:16
Since you must feed the text typed by the player into the parser manually, what's keeping you from using String.IndexOf first?

Code (ags) Select
  String input = txtParserInput.Text;

  // first, do some manual parsing
  if (input.indexOf("hello") >= 0) {
    // input contains "hello"
    ...
    return; // exit
  }

  // now, AGS parsing
  Parser.ParseText(input);
  if (Parser.Said("...")) ...
Title: Re: String.Contains Text Parser
Post by: Stupot on Wed 29/05/2013 08:52:12
What if the player says something that contains the word 'hello' but does not necessarily warrant a greeting in return.
For Example:
"Hello, My name is John."
"Hello."
"I love Hello Kitty."
"Hello"
"No, I already said hello to you."
"Hello"
"Grrr, Goodbye you stupid Parser!"
"Goodbye *trolololol*"
Title: Re: String.Contains Text Parser
Post by: selmiak on Wed 29/05/2013 17:42:30
:D