Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Zany on Fri 13/10/2017 15:53:28

Title: get object position using hotspot interact
Post by: Zany on Fri 13/10/2017 15:53:28
Hi All

I'm dragging an object to a specific hotspot area, then once I place it on the hotspot, the "hotspot interact" function needs to get the position of the object and confirm that if it is above the hotspot and then display another object visible. Can some one please point me in the right direction.
Title: Re: get object position using hotspot interact
Post by: eri0o on Fri 13/10/2017 19:00:33
How are you dragging the object?
Title: Re: get object position using hotspot interact
Post by: Cassiebsg on Fri 13/10/2017 23:09:49
Yes, post your code, so we can understand exactly what and how you are doing it.
Title: Re: get object position using hotspot interact
Post by: Zany on Mon 16/10/2017 07:52:14
I found this code on the ags site a while back couldn't find the link but I'm using this:

Code (ags) Select
function noloopcheck drag(Object *oPL){
  int oPX = mouse.x - oP.X;
  int oPY = mouse.y - oP.Y;
 
  while(mouse.IsButtonDown(eMouseLeft)){
    oP.X = mouse.x - oPX;
    oP.Y = mouse.y - oPY;
    Wait(1);
  }
}

function oP_Interact()
{
  drag(oP);
}
Title: Re: get object position using hotspot interact
Post by: Crimson Wizard on Mon 16/10/2017 11:29:08
Checking if an object is above hotspot can be tricky in AGS, because AGS has only function that tells if there is a hotspot under particular coordinates, but you cannot get its boundaries in script, neither ask game to check if object is touching it.

There are still complex ways to do this, like, for example, precalculating all the hotspot positions on screen by running Hotspot.GetAtScreenXY repeatedly with certain step and saving this information. Unless I am mistaken, this is what AGSers from the French community did with their "Display Hotspot locations" module (name may be different, I forgot how it was actually called and could not find it on forums).


I can suggest using one of the two alternate solutions:
1) Replace hotspot with another object, then you may use Object.IsCollidingWithObject to check if one is above another.
2) Write down coordinates of the bounding rectangle of your hotspot and check if object touches that rectangle, example:
Code (ags) Select

#define HOTSPOT_X1 100
#define HOTSPOT_X2 200
#define HOTSPOT_Y1 50
#define HOTSPOT_Y2 150

bool IsObjectOverHotspot(Object *oP)
{
    // You would have to calculate oPWidth and oPHeight for an object, because AGS does not export such parameters.
    // They could be calculated from the size of sprite used for object:
    int oPWidth = Game.SpriteWidth[oP.Graphic];
    int oPHeight = Game.SpriteHeight[oP.Graphic];
    if (oP.X >= HOTSPOT_X1 && oP.X < HOTSPOT_X2 || oP.X + oPWidth >= HOTSPOT_X1 && oP.X + oPWidth < HOTSPOT_X2) &&
       (oP.Y >= HOTSPOT_Y1 && oP.Y < HOTSPOT_Y2 || HOTSPOT_Y1 + oPHeight >= HOTSPOT_Y1 && HOTSPOT_Y1 + oPHeight < HOTSPOT_Y2);
    {
        return true;
    }
    return false;
}


Then use this function after drag is finished:
Code (ags) Select

function noloopcheck drag(Object *oPL){
  int oPX = mouse.x - oP.X;
  int oPY = mouse.y - oP.Y;
 
  while(mouse.IsButtonDown(eMouseLeft)){
    oP.X = mouse.x - oPX;
    oP.Y = mouse.y - oPY;
    Wait(1);
  }

  if (IsObjectOverHotspot(oP)) {
    // do something
  }
}



PS.
Spoiler

I will take a chance to shamelessly advertise my Drag & Drop module that can drag objects, characters, inventory items, and your own scripted custom entities (has demo game available) :tongue::
http://www.adventuregamestudio.co.uk/forums/index.php?topic=53332.0
[close]

Title: Re: get object position using hotspot interact
Post by: Zany on Mon 16/10/2017 13:06:49
Thanx Crimson I will definitely use that if I don't find an easier or shorter solution. What I was thinking was if my object was a single object and not multiple then maybe I could manipulate it as if it were an inventory item and if I clicked on the hotspot with use of the function "use inventory" it would know to do something, the only matter is that it would require the user to go into the inventory and in essence I just needed the object to remain on screen. All possible solutions are welcomed...
Title: Re: get object position using hotspot interact
Post by: Crimson Wizard on Mon 16/10/2017 13:11:42
Quote from: Zany on Mon 16/10/2017 13:06:49What I was thinking was if my object was a single object and not multiple then maybe I could manipulate it as if it were an inventory item and if I clicked on the hotspot with use of the function "use inventory" it would know to do something, the only matter is that it would require the user to go into the inventory and in essence I just needed the object to remain on screen. All possible solutions are welcomed...

What about this: player first clicks on object in room, you give player a temporary inventory item, making it ActiveInventory. If player deselects it, then temp item gets removed. If player uses it on a hotspot you do your action.

BTW, I don't remember if I tried that before, but I wonder if that will work if you can set ActiveInventory to an item which is not in character's inventory.

Alternatively, without even using items, you may change cursor graphic to something resembling an object, but then you also need to correctly script cursor changes yourself.