Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: ddavey1983 on Fri 03/02/2023 19:58:59

Title: Text parser help
Post by: ddavey1983 on Fri 03/02/2023 19:58:59
Hello

I'm not great with coding, I've managed to make some decent-enough games using templates and Heath-Robinson style solutions to problems that would probably be easier to solve with basic coding knowledge but...

I would like to make a text-parser game in the style of Police Quest 2 and that era of Sierra. The manual is a bit vague, most of the posts on the forums are too specific and the only template I've found doesn't seem to run on the latest version. I watched a video tutorial but it's geared towards an older version and some of the important functions seem to be missing.

So... does anyone know of a recent template or a idiot's guide style tutorial for making a text parser game?

Thanks!
Title: Re: Text parser help
Post by: Khris on Sat 04/02/2023 10:20:17
Important functions won't be missing but rather they've been replaced with oo style functions I assume.

Anyway, the basic idea is to create a GUI with a text input box on it. Give both a suitable name, like gCommand and tbCommand.

Next, you'll want to show the GUI when the user starts to type. Open the global script and find the on_key_press function. In there, add this:
  if (!gCommand.Visible) {
    if (keycode >= eKeyA && keycode <= eKeyZ) {
      tbCommand.Text = String.Format("%c", keycode);
      gCommand.Visible = true;
      return;
    }
  }
What this does is if the player presses a letter key while the command GUI isn't visible, it shows the GUI and puts the letter into the text box. Which should have focus now, so the player can simply continue typing their command.

Next you'll want to implement the F3 feature, which repeats the last command. We need to store the command for this, so put this directly above function on_key_press(...):
String last_command;
Again inside the function, add
  if (keycode == eKeyF3 && !String.IsNullOrEmpty(last_command)) {
    tbCommand.Text = last_command;
    gCommand.Visible = true;
  }

Finally, we need to actually run the typed command. Double click the text box in the GUI editor or use the events pane to add the OnActivate function to the global script. It will run when the player presses enter while the textbox has focus.
Inside, put this:
  String command = tbCommand.Text;
  tbCommand.Text = ""; // clear text box
  gCommand.Visible = false; // hide GUI
  if (command.Length == 0) { gCommand.Visible = false; return; } // hide GUI, do nothing
  last_command = command; // store command for F3 key

  Parser.ParseText(command); // pass typed text to AGS parser

  // handle commands
  if (Parser.Said("look")) CallRoomScript(1); // run on_call(1) in the current room's script
  if (Parser.Said("inv") || Parser.Said("i")) show_inventory();
  // etc
Title: Re: Text parser help
Post by: ddavey1983 on Sun 05/02/2023 22:56:54
That's great, thank you very much! I'll have a look and see what I can come up with.