Author Topic: How to do an action if the last Parser sentence was succesfully recognised?  (Read 144 times)

bx83

  • benjamin.rich@protonmail.com
I have a console; what's entered into it is checked by a short list of words. If the word(s) are recognised (eg. "eat apple"), or in other words a specific command matching search criteria is run, I want the Parser to make a sound. This sound would probably be triggered by something like
Code: [Select]
if (Parser.LastRunCommandMatched) aSound.Play(....
Is anything like the above possible? Otherwise it'll become a case of repeating code:

Code: [Select]
if (Parser.Said("eat apple")) {
....
x=true;
}

if (Parser.Said("steal apple")) {
....
x=true;
}

if (Parser.Said("get,buy drink")) {
....
x=true;
}

if (Parser.Said("steal drink")) {
....
x=true;
}

if (x) aSound.Play(...);
x=false;


You are in a situation when you have a lengthy code with many checks and want to use the final result elsewhere.
In this situation one of the solutions is to move the checks to a separate function and return final value.

Code: Adventure Game Studio
  1. bool TestAll() {
  2.     if (Parser.Said("eat apple")) {
  3.         ....
  4.         return true;
  5.     }
  6.  
  7.     if (Parser.Said("steal apple")) {
  8.          ....
  9.          return true;
  10.     }
  11.  
  12.     return false;
  13. }
  14.  

And then -
Code: Adventure Game Studio
  1. if (TestAll()) aSound.Play(...);
  2.  



Also, I must note that when you do multiple ifs one after another script will keep testing every one of them regardless of the result. If only one of the checks is expected to give positive result then the correct way is to use if/else:
Code: Adventure Game Studio
  1. if (Parser.Said("eat apple")) {
  2. }
  3. else if (Parser.Said("steal apple")) {
  4. }
  5.  

When you do "return" under "if" you don't have to do this, because the function will end when at least one "if" triggers anyway.
« Last Edit: 09 May 2021, 13:02 by Crimson Wizard »

bx83

  • benjamin.rich@protonmail.com
Thank you :)
Any plans to add new functionality to the parser? I can't imagine it's the main method for adventure game interfaces but still.

Any plans to add new functionality to the parser? I can't imagine it's the main method for adventure game interfaces but still.

There are no plans, not many people use it, and I can't recall a single suggestion made in years.
If you have one you can post it in the engine development forum or on our github issue tracker.


Why did you post it as a "bug" though?...

TBH what you suggest is quite possible to code in the script, maybe even make a custom script module. You simply need to write a function that calls Parser.Said and stores returned value somewhere.
« Last Edit: 09 May 2021, 14:20 by Crimson Wizard »