How to do an action if the last Parser sentence was succesfully recognised?

Started by bx83, Sun 09/05/2021 11:08:28

Previous topic - Next topic

bx83

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: ags
if (Parser.LastRunCommandMatched) aSound.Play(....


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

Code: ags
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;



Crimson Wizard

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

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

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

    return false;
}


And then -
Code: ags

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

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.

bx83

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.

Crimson Wizard

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.


Crimson Wizard

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.

SMF spam blocked by CleanTalk