Check typed text box

Started by Vincent, Thu 11/07/2024 08:01:37

Previous topic - Next topic

Vincent

Hey guys, I wanted to ask a quick question for those who are more experienced. How can I check what the player typed in a text box and respond accordingly by checking what pronoun or verb was been used? I tried this a few days ago and I kind of managed to make it work, but it looks like it will require a lot of lines of code and I would like to make it simple without writing some nightmare code. How would you start doing something like this? What it would be your first approach?

Khris

Language processing is a massive task. AGS can help you to some degree if you use the built-in parser but other than that you're on your own.

It would help if you listed some examples.
What are typical lines the user is expected to enter, and what do you want to recognize exactly?

Vincent

Thanks Khris.

Quote from: Khris on Thu 11/07/2024 08:23:54What are typical lines the user is expected to enter, and what do you want to recognize exactly?

That is a good question because the player can basically type everything which it sound more a massive task to accomplish. I can check easily if a specific question has been asked for example "how are you" by adding the entire sentence on the Parser dictionary. But the hardest thing is to catch the random stuff 🙄 like "how is the weather today?"

In my mind I have it something like this:
Code: ags
bool how;
int result_how = txt.IndexOf("how");
if (result_how != -1) how = true;

if (how)
{
  if (is) ...
  {
    //etc etc
  }
}

But doing like this in the long term I think it would become a big mess, so I was wondering to ask what it would be your basic approach to make it simple.

Khris

If I had to do this I'd probably come up with a bunch of base phrases like
"can I x y"
"how do you x y"
"are you x y"
etc., then create a RegExp-like parser that a) detects which phrase was used and b) what x and y are.

Also, one of the hardest things to do is to upkeep a conversation; is your bot supposed to be able to do that? Like understand references to things previously mentioned?

Anyway, if somebody put a gun to my head and told me to implement this (I wouldn't otherwise) I'd probably use an existing online API and http requests via the sockets plugin.

Vincent

#4
Quote from: Khris on Thu 11/07/2024 10:23:38Anyway, if somebody put a gun to my head and told me to implement this (I wouldn't otherwise) I'd probably use an existing online API and http requests via the sockets plugin.

This makes me laugh 🤣 cause is exactly what I thought at first but still want it to give it a go in Ags.

Quote from: Khris on Thu 11/07/2024 10:23:38Also, one of the hardest things to do is to upkeep a conversation; is your bot supposed to be able to do that? Like understand references to things previously mentioned?

Kind of yes, like if you type a specific sentence like "i want to eat" maybe it could upkeep the conversation otherwhise it will not. like if you type "how is the weather today" he might answer "I dont know that." Still, for some specific question I can might put the entire sentence on the Parser dictionary to check it easily.

Quote from: Khris on Thu 11/07/2024 10:23:38If I had to do this I'd probably come up with a bunch of base phrases like
"can I x y"
"how do you x y"
"are you x y"
etc., then create a RegExp-like parser that a) detects which phrase was used and b) what x and y are.

Thanks for your thought, this actually seems like a smart approach.

Quote from: Khris on Thu 11/07/2024 10:23:38I'd probably use an existing online API and http requests via the sockets plugin.

ps: @Khris I remember using the sockets plugin only for pushing a button and opening a web page but nothing more than that, how can I do this? Like to reference a word and doing a request?

Khris

You're thinking of the ags_shell plugin; the sockets plugin allows raw communication with a server and was initially used to let an AGS game talk to an IRC server.

Here's the sockets plugin (not sure which version, it's the one I used back then):
https://drive.google.com/file/d/1-MdDxpEEo0J1UAUm96fbIsyunmgZzja8/view?usp=sharing

And here's a link to an HTTP request module I wrote:
https://drive.google.com/file/d/1-GWWv4W2_dE40ydIFnGHp7Tm-btr7xoz/view?usp=sharing

You can use it like this:
Code: ags
  HTTPRequest.Prepare(GET_url);
  int HTTPCode = HTTPRequest.Get();
  Display(HTTPRequest.Reply());

It doesn't support https afaik, so it's kind of useless for external APIs though.

Perpetuall

Like a chatbot in AGS. That's cool, I was thinking about implementing something like this in my game in the future, and I thought I'd do what Khris said, just program the chatbot separately and then link it up with the game.
Anyway, I'm working on a similar project where I have to input massive amounts of text, so keep me up to date on how you're making progress. I'd like to see what you come up with.

Vincent

#7
@Khris first off I want to say that this module you wrote its so amazing as usual beautiful work, thanks for sharing! And yes I just realize that it was the ags_shell plugin I was using long time ago and not the socket one, my mistake. Btw as you say the https will not work unfortunately but still wanted to give it a go by doing something like this:
Code: ags
HTTPRequest.Prepare(String.Format("http://api.dictionaryapi.dev/api/v2/entries/en/%s", txt));
int HTTPCode = HTTPRequest.Get();
ClaytonMsg = String.Format("%s", HTTPRequest.Reply());

I dont know if im doing this correctly thought, I am getting some results which means its connected but it doesnt get the word im typing in.
ps: I had to quickly fix some lines replacing 'log' with 'logg'  but nothing more than that :)

@Perpetuall Cool good luck with it in the meantime.

Khris

I wanted to test this myself but AGS tells me it's unable to load agssock.dll when I open the editor. Not sure what the reason is, IIRC it used to work on and off for me back then.

The error also says "it may depend on another dll that is missing" and I'm several Windows reinstalls down the line, so maybe that's the reason.

Vincent

#9
Quote from: Khris on Thu 11/07/2024 12:45:34I wanted to test this myself but AGS tells me it's unable to load agssock.dll when I open the editor. Not sure what the reason is, IIRC it used to work on and off for me back then.

The error also says "it may depend on another dll that is missing" and I'm several Windows reinstalls down the line, so maybe that's the reason.

Well I didn't use the plugin you shared because I had it already so I believe they are the same, I think the reason might be what you mentioned because it just working good to me

ps: im using AGS-4.00.00.03-Alpha8 -video non blocking version

Khris

I checked the dependencies and they are kernel32.dll, msvcrt.dll, user32.dll and ws2_32.dll. All of which are in my System32 folder.
I'd love to have this problem solved, maybe somebody has an idea? I already added the dll as file exception to Windows Defender, too.

Vincent

#11
Quote from: Khris on Thu 11/07/2024 12:52:55I checked the dependencies and they are kernel32.dll, msvcrt.dll, user32.dll and ws2_32.dll. All of which are in my System32 folder.
I'd love to have this problem solved, maybe somebody has an idea? I already added the dll as file exception to Windows Defender, too.

I am not good at it but what I can say is that I just putted the dll inside the ags editor folder.
I didn't had to put the dll anywhere else.

ps: im sharing the dll in case this was different: https://file.io/19TAECgoMK87

Khris

The dll I had was a different one, yours works without issues, thanks!

I tested this and unfortunately it's an https issue. The reply you get is the "permanently moved" HTML since you did an http request, not an https one.

Vincent

Nice I am happy at least you got it solved and also thanks for the clarification. But I dont get why you said that you'd probably use an existing online API and http requests via the sockets plugin if this apparently can't be done? Or maybe you wasn't talking about using AGS?

Khris

https has become ubiquitous nowadays (and that's a good thing) so it unfortunately isn't a viable solution to use an API until we get an https capable way to make requests from within AGS (leaving aside whether it's a good idea to have a game that will stop functioning if the server goes down/away).

I'd love a fetch plugin. *wink* *wink*

Vincent

Quote from: Khris on Thu 11/07/2024 16:39:39(leaving aside whether it's a good idea to have a game that will stop functioning if the server goes down/away).

This is a point to be honest. Using an online API to do such task is definitely a life safer but it's risky for this reason you mentioned. Going a few steps backwards I'd like to ask you a noob question which I hope you dont mind. I am using the String.IndexOf function to check if a word has been found. But I was wondering if there is a more solid method to do that. I noticed that if I have these two words like: "she" and "he" or "where" and "here" they both have "he" and "here" so for the function is like they are the same which I'd like to avoid that.

Khris

A quick solve is to do
Code: ags
  if (txt.StartsWith("he ") || txt.IndexOf(" he ") > 0)

But I'd go a different route: parsing the string char by char and putting the words into an array, skipping punctuation, words like "the" and potential double spaces and the like in the process.
This way you can do:
Code: ags
  if (word[0].CompareTo("how") == 0 && word[1].CompareTo("is") == 0) {
    if (word[2].CompareTo("weather")) ...
  }

eri0o

@Vincent , you can switch to the latest ags4 instead of using this older release, which has beyond the new video stuff and other things, it also has String.Split, so you can split using the space character as separator. You need to set AGS to use the extended compiler in the General Settings, which adds a Length property to the dynamic strings, so you can read the Length of the returned array. String.Split returns an array of Strings, you can then go word by word.

Vincent

#18
@Khris Thanks for the advices I will try to work out on it.


@eri0o
Thanks for your suggestion this might come very handy.
Do you mean this one?
https://www.adventuregamestudio.co.uk/forums/ags-engine-editor-releases/ags-3-6-0-patch-11/
Also I can't see to find this extended compiler in the General Settings.

Sorry you might mean this one:
https://www.adventuregamestudio.co.uk/forums/ags-engine-editor-releases/ags-4-0-early-alpha-for-public-test/

@Crimson Wizard  I am sorry for the tag, I am now using the latest ags4 and im trying to enable the extended compiler (I have your module Key Listener) and when I try to compile the game it pop up this error on the output console:



How can I fix it? I see that if I hide that piece of code then I get the same message for others functions inside the module

Crimson Wizard

Right, the new compiler is more strict in terms of syntax, and KeyListener was written at the times when the static extenders were "hacked" into the ags script.

All the static struct's extender functions must have "static StructName" as their first argument,
that is instead of

Code: ags
bool get_Enabled(this KeyListener*, ...)

you should have

Code: ags
bool get_Enabled(static KeyListener, ...)

(notice there's no * in the second variant)

SMF spam blocked by CleanTalk