Help! How to create a variable where you can type text and have AGS identify that

Started by BowsetteGamer, Mon 22/04/2024 13:24:10

Previous topic - Next topic

BowsetteGamer

Hello guys, how are you? Well, let me explain:

How do I create a Mechanism in which I write a text that, for example, says; "Hello"

Press the Enter button

and then AGS identifies that "Hello" and responds by saying "Hi, how was your day?"

That is possible to do, please help guys.  ???  ???  ???

eri0o

Well you have three problems

  • 1. You need to figure some way to handle text input
  • 2. You need to figure some way to parse text input
  • 3. You need to give the player some feedback on what you interpreted after parsing their input

That's sort of the gist. If you don't want to care about 1 now you can use Game.InputBox as a placeholder for the time being. (See more in the manual).

Now for 2 you can use the Parser that AGS has (See Text parser in the manual) or you can develop your own approach by using String functions to parse the text some way you like.

Than in 3 you provide feedback given you parsed the player input.

It's hard to mention more without more context on the entire thing you have in mind.


BowsetteGamer

Quote from: eri0o on Mon 22/04/2024 14:50:25Well you have three problems

  • 1. You need to figure some way to handle text input
  • 2. You need to figure some way to parse text input
  • 3. You need to give the player some feedback on what you interpreted after parsing their input

That's sort of the gist. If you don't want to care about 1 now you can use Game.InputBox as a placeholder for the time being. (See more in the manual).

Now for 2 you can use the Parser that AGS has (See Text parser in the manual) or you can develop your own approach by using String functions to parse the text some way you like.

Than in 3 you provide feedback given you parsed the player input.

It's hard to mention more without more context on the entire thing you have in mind.



What I have in mind is:
Use a GUI, along with the text box and also the "Game.InputBox" function

Have the player type some text and press the "enter" button

About the parsing text input, I've been reading it, I'm still not sure how to do it, but it's basically what I need for AGS to read it.

and finally AGS gives an answer. But the detail is... How do I do it? Where do I start? And I have a lot of doubts about what I'm reading in the manual, so basically how would I write the code?

eri0o

Uhm... The AGS Text Parser it comes with is made for very specific game play in mind that is more like the old games with a text parser.

This is why I asked exactly what sort of input you want to take and what sorts of answer you would also want to give.

The answer can go from a simple if with a display text to full blown natural language processing...

Anyway let's try something small and simple. We will create a GUI named gTextInput, with a Button named btnGo and a TextBox named textBoxInput.

Spoiler
[close]

We will click the button, in the properties panel we will click the events and click the tree dots in the OnClick event. AGS will open the Global Script.

Code: ags
function btnInputGo_OnClick(GUIControl *control, MouseButton button)
{

}

We need to fill something in that function. Instead of doing that I will right click on Scripts in the Explore Project panel on top right and add a new Script, which I will named TextInput.

I will add the following script there

Code: ags
// new module script

void GoClicked()
{
  String input = textBoxInput.Text;
  input = input.LowerCase();
  if(input.StartsWith("hello"))
  {
    Display("Hi, how was your day?"); 
  } else {
    Display("Not even a hello?");
  }
}


Spoiler
[close]

I will now add to TextInput.ash an import for this function so we can call it from GlobalScript.

Code: ags
// new module header

import void GoClicked();

Spoiler
[close]

OK, now just need to go back to the GlobalScript and call this function from the button code there.

Code: ags
function btnInputGo_OnClick(GUIControl *control, MouseButton button)
{
  GoClicked();
}

Spoiler
[close]

Now running the game it should show up

Spoiler
[close]

Anyway, this is just the basics to explore the idea though.

Khris

I feel the most important part is this one:
Quote from: BowsetteGamer on Mon 22/04/2024 13:24:10AGS identifies that "Hello" and responds by saying "Hi, how was your day?"
Quoteand finally AGS gives an answer

As eri0o said, the parser can help you process commands like "go north" or "look at table". But carrying a conversation is a whole other level.

How sophisticated is this supposed to be? I mean it has been done in the past, for instance there's been a MAGS game where you could communicate with another person by typing into a chat basically. It was very well done and a lot of work. I can't remember the game's name though.

But just to clarify: any answer AGS is supposed to give has to be hardcoded by you. Like
Code: ags
  if (prompt == "Hello") Display("Hi, how are you?");
  else if (prompt == "I'm fine") Display("That's great.");

Note that this only works for exact matches, so "Im fine" or "I'm  fine" has to be detected separately.

heltenjon

Quote from: Khris on Tue 23/04/2024 07:23:42How sophisticated is this supposed to be? I mean it has been done in the past, for instance there's been a MAGS game where you could communicate with another person by typing into a chat basically. It was very well done and a lot of work. I can't remember the game's name though.
Probably this? Made for OROW V.
https://www.adventuregamestudio.co.uk/site/games/game/1059-chatroom/

BowsetteGamer

Quote from: eri0o on Tue 23/04/2024 03:01:31Uhm... The AGS Text Parser it comes with is made for very specific game play in mind that is more like the old games with a text parser.

This is why I asked exactly what sort of input you want to take and what sorts of answer you would also want to give.

The answer can go from a simple if with a display text to full blown natural language processing...

Anyway let's try something small and simple. We will create a GUI named gTextInput, with a Button named btnGo and a TextBox named textBoxInput.

Spoiler
[close]

We will click the button, in the properties panel we will click the events and click the tree dots in the OnClick event. AGS will open the Global Script.

Code: ags
function btnInputGo_OnClick(GUIControl *control, MouseButton button)
{

}

We need to fill something in that function. Instead of doing that I will right click on Scripts in the Explore Project panel on top right and add a new Script, which I will named TextInput.

I will add the following script there

Code: ags
// new module script

void GoClicked()
{
  String input = textBoxInput.Text;
  input = input.LowerCase();
  if(input.StartsWith("hello"))
  {
    Display("Hi, how was your day?"); 
  } else {
    Display("Not even a hello?");
  }
}


Spoiler
[close]

I will now add to TextInput.ash an import for this function so we can call it from GlobalScript.

Code: ags
// new module header

import void GoClicked();

Spoiler
[close]

OK, now just need to go back to the GlobalScript and call this function from the button code there.

Code: ags
function btnInputGo_OnClick(GUIControl *control, MouseButton button)
{
  GoClicked();
}

Spoiler
[close]

Now running the game it should show up

Spoiler
[close]

Anyway, this is just the basics to explore the idea though.

OHH MY GOD THIS IS A GOOD TUTORIAL!!!  :shocked:  :shocked:
This is an excellent tutorial thank you for taking the time to teach me sincerely You @eri0o, @Crimson Wizard, @heltenjon and @Khris are the best on this topic. My apologies for not having responded much earlier, I was focusing on some aspects of my personal life, sorry guys. And well, I wanted to thank you very much for the help you have given me. A question (Sorry if this is disrespectful) Do you program as a Hobby or do you work with programming?

BowsetteGamer

Quote from: Khris on Tue 23/04/2024 07:23:42I feel the most important part is this one:
Quote from: BowsetteGamer on Mon 22/04/2024 13:24:10AGS identifies that "Hello" and responds by saying "Hi, how was your day?"
Quoteand finally AGS gives an answer

As eriOo said, the parser can help you process commands like "go north" or "look at table". But carrying a conversation is a whole other level.

How sophisticated is this supposed to be? I mean it has been done in the past, for instance there's been a MAGS game where you could communicate with another person by typing into a chat basically. It was very well done and a lot of work. I can't remember the game's name though.

But just to clarify: any answer AGS is supposed to give has to be hardcoded by you. Like
Code: ags
  if (prompt == "Hello") Display("Hi, how are you?");
  else if (prompt == "I'm fine") Display("That's great.");

Note that this only works for exact matches, so "Im fine" or "I'm  fine" has to be detected separately.

"Please note that this only works for exact matches, so "I'm fine" or "I'm fine" should be detected separately."

That's true, I was writing code and the program detected it anyway.
Well, the idea I have of this is simple, you write to him and he answers you according to what he has programmed. When it is finished you will see it @Khris, it is a surprise for you.
When it's finished, @Khris, one question, will you try the game and let me know what you think?

Your opinion is very important to continue improving my coding skills.

BowsetteGamer

Quote from: heltenjon on Tue 23/04/2024 10:14:41
Quote from: Khris on Tue 23/04/2024 07:23:42How sophisticated is this supposed to be? I mean it has been done in the past, for instance there's been a MAGS game where you could communicate with another person by typing into a chat basically. It was very well done and a lot of work. I can't remember the game's name though.
Probably this? Made for OROW V.
https://www.adventuregamestudio.co.uk/site/games/game/1059-chatroom/

I was watching the game and honestly the person who programmed the game worked a lot and very well on his project, it's incredible

SMF spam blocked by CleanTalk