Pages traduites Pages à traduire Pages en cours de traduction
A propos
 

Ceci est une ancienne révision du document !



L'analyseur de texte(text parser)

Vous pouvez désormais utiliser un analyseur de texte dans vos jeux, comme le faisaient les plus anciens jeux Sierra. Dans l'éditeur, allez dans le panneau “Text parser” (analyseur de texte). Là, vous verrez une courte liste de mots qui vous sont fournis. Chaque mot a un nombre à côté de lui.

Basiquement, vous ajoutez le mot que vous voulez utiliser par un clic droit sur la liste, et sélectionnez “Add word” (ajouter mot). Toutefois, le véritable intérêt de l'analyseur est sa capacité à reconnaître des synonymes - lorsque deux mots signifient la même chose. Par exemple, si vous voulez que le joueur tape “look at fence” (regarder la clôture), il pourrait taper à la place “look at wall” (regarder le mur), si c'est sa façon de voir le dessin. Où un anglais taperait ”colour” , un américain tapera peut-être ”color”, les deux devraient avoir le même effet.

Pour ajouter un synonyme à un mot existant, sélectionnez le mot. Faites un clic droit dessus et choisissez “Add synonym” (ajouter synonyme). Vous remarquerez que le nouveau mot donne le même nombre que l'ancien. Tous les mots avec le même nombre sont considérés comme identiques par l'analyseur.

Vous remarquerez que la liste fournie a beaucoup de mots avec le numéro 0. Il s'agit d'un numéro spécial, qui indique que l'analyseur doit ignorer complètement le mot. Dans notre exemple précédent, le joueur devait taper “look at the fence”, “look at fence”, ou juste “look fence”. En ajoutant des mot comme “at” et “the” dans la liste à ignorer, ils sont enlevés automatiquement des entrées tapées par l'utilisateur. Pour ajouter de nouveaux mots à ignorer, il suffit de les ajouter comme synonymes.

Comment utiliser l'analyseur de texte ? Well, you'll need a text box GUI control somewhere in order for the user to type in their input, or you could just use the InputBox command (but it has quite a short line length).

When the user has typed in their text (you'll probably know this by the text box's event being activated), you call the Parser.ParseText script function which tells AGS what input string to use in subsequent commands. You then simply use the Said command to test what the player typed in.

You type the whole sentence (but NOT including any ignore words), and AGS will compare it to the user's string, considering all synonyms identical. For example (assuming our text box is called “txtUserInput”):

  String input = txtUserInput.Text;
  Parser.ParseText(input);
  if (Parser.Said("look fence")) {
    Display("It's an old wooden fence.");
  }
  if (Parser.Said("eat apple")) {
    Display("You'd love to, but you don't have one.");
  }

There are a couple of special words you can use with the Said command. “anyword” will match any word that the user types in. For example, Said(“throw anyword away”) will match if they type “throw dagger away”, or “throw trash away”. “rol” (short for Rest-of-Line) will match the rest of the user's input. So, you might want to do:

if (Parser.Said("kill rol")) {
  Display("You're not a violent person.");
}

This way if they try to kill anything they will get the generic response.

Sometimes, you want to accept two different words that are not synonyms as the same thing. For example, the words “take” and “eat” normally have totally different meanings, so you wouldn't make them synonyms of each other. However, if the player has a headache tablet, for instance, then “take tablet” and “eat tablet” would both be valid. This is where the comma ”,” comes in - if you include a comma in your input, all synonyms of all words separated by the comma will match. So:

if (Parser.Said("eat,take tablet"))

will match eat or take and all their synonyms, then tablet and its synonyms.

Another fairly common task with a parser is to check for optional words - for example, if there is a brick wall on the screen, you want the player to be able to type “look wall” and “look brick wall”. Although this can be done with two OR'ed Said commands, AGS makes it easier. You can use [brackets] to signify an optional word. So:

if (Parser.Said("look [brick] wall"))

will match “look wall” and “look brick wall”.

Now this is all very well, but in different rooms you have different items to interact with - for example, in one room there might be a tree that the player should be able to type “look at tree” to look at, and so on. Putting all this in your global script would make a big mess. So, enter the CallRoomScript function. Using this, you can do:

  Parser.ParseText(input);
  String badWord = Parser.SaidUnknownWord();
  if (badWord != null)
    Display("You can't use '%s' in this game.", badWord);
  else if (Parser.Said("eat apple")) {
    Display("You'd love to, but you don't have one.");
  }
  ... // other game-wide commands
  else
    CallRoomScript (1);

Then, the room script can check for things that the player can do in the current room. See the CallRoomScript description for more information.

 
ags17.1302969774.txt.gz · Dernière modification: 16/04/2011 18:02 par kitai
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki