Text Parser - Hotspots/Rooms

Started by Thanyx, Fri 25/11/2016 11:23:44

Previous topic - Next topic

Thanyx

Dear AGS colleagues,

I am sorry for this newbie question, but I am not able to find an answer to this anywhere.

I'm creating classic text parser sierra like game (KQ, SQ, PQ, etc.).
I cannot find how to code situation, when player has to type exact text parser on exact hotspot. For example, you cannot just write "Take apple", when you stand on the other side of the room, right.
So, how do I code that when player stands near hotspot/object, you are able to write "take apple", and if he/she stands far from it, game will give you "you are too far".

And with this question comes another: How to write these codes not in global script, but in room script. I've seen an article about "CallRoomScript", but I somehow did not understand.

Thank you very much for any help given,

Best regards,
Thanyx


Danvzare

First create a region in your room so the game knows where you need to be standing to be able to interact with it, then check to see if the player is in that region using code similar to this:
Code: ags
if Region.GetAtRoomXY(player.x, player.y) != region[2]
{
//insert code here
}


To edit your room script, just double click on your room, then double click on your room script. It should open up for you.


Khris

To check how far the player is from a hotspot, you can either calculate the distance based on coordinates or use a region. For actual AGS-hotspots, i.e. drawn ones, you can set a WalkTo-point in the editor. Put it on the floor, right below the hotspot.
To calculate the distance you can use a custom function:
Code: ags
// header
#define INTERACT_DIST 40
import bool IsCloseH(Hotspot *h);

// global script
bool IsCloseH(Hotspot *h) {
  int dx = h.WalkToX - player.x, dy = h.WalkToY - player.y;
  return dx*dx + dy*dy*4 < INTERACT_DIST*INTERACT_DIST; // factor 4 for perspective correction
}

(You can write a similar function for objects that uses o.X + System.SpriteWidth[o.Graphic]/2 and o.Y)

In your room script, you can now do this:
Code: ags
function hStatue_Interact() {
  if (IsCloseH(hStatue)) {
    Display("It's cool to the touch.");
  }
  else Display(too_far_away); // global string variable
}


As for handling parser code in your room script, you start with this in the global script:
Code: ags
function txtBoxParser_OnActivate(...) {
  Parser.ParseText(txtBoxParser.Text);
  CallRoomScript(1);
}


In your room script you add this at the bottom:
Code: ags
function on_call(int p) {
  if (p == 1) { // parser call
    if (Parser.Said("take apple")) oApple.RunInteraction(eModeInteract);
    ...
  }
}


SMF spam blocked by CleanTalk