Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: bx83 on Sun 09/05/2021 11:08:28

Title: How to do an action if the last Parser sentence was succesfully recognised?
Post by: bx83 on Sun 09/05/2021 11:08:28
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
if (Parser.LastRunCommandMatched) aSound.Play(....

Is anything like the above possible? Otherwise it'll become a case of repeating code:

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;


Title: Re: How to do an action if the last Parser sentence was succesfully recognised?
Post by: Crimson Wizard on Sun 09/05/2021 13:00:23
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 (ags) Select

bool TestAll() {
    if (Parser.Said("eat apple")) {
        ....
        return true;
    }

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

    return false;
}


And then -
Code (ags) Select

if (TestAll()) aSound.Play(...);




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 (ags) Select

if (Parser.Said("eat apple")) {
}
else if (Parser.Said("steal apple")) {
}


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.
Title: Re: How to do an action if the last Parser sentence was succesfully recognised?
Post by: bx83 on Sun 09/05/2021 13:04:51
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.
Title: Re: How to do an action if the last Parser sentence was succesfully recognised?
Post by: Crimson Wizard on Sun 09/05/2021 13:23:47
Quote from: bx83 on Sun 09/05/2021 13:04:51
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 (https://github.com/adventuregamestudio/ags/issues).
Title: Re: How to do an action if the last Parser sentence was succesfully recognised?
Post by: bx83 on Sun 09/05/2021 13:42:27
https://github.com/adventuregamestudio/ags/issues/1272
Title: Re: How to do an action if the last Parser sentence was succesfully recognised?
Post by: Crimson Wizard on Sun 09/05/2021 13:58:25
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.