Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: MiteWiseacreLives! on Fri 27/10/2017 04:49:45

Title: How to determine a Button.ID from a location? SOLVED
Post by: MiteWiseacreLives! on Fri 27/10/2017 04:49:45
So all I want to do is pass the numerical value of the Button above the one the player presses to an integer. (From within another Function as opposed to within function btnInv7_OnClick() )
I tried this:
Code (ags) Select

      GUIControl *control = GUIControl.GetAtScreenXY(gInventory.Controls[startblock].X + 10, gInventory.Controls[startblock].Y - 10); 
      int block = control.ID;     
      if(InvBlock[block].full)
           return false;  // block1 is full

But this just ends in Null pointers, I don't think GUIControl has an ID parameter. I tried several other things like using AsButton, and searching the Beginner Forums with no success.
Thanks,
Mite
Title: Re: How to determine a Button.ID from a location?
Post by: Crimson Wizard on Fri 27/10/2017 10:01:55
First of all, you do not check a pointer returned by GetAtScreenXY:

Code (ags) Select

GUIControl *control = GUIControl.GetAtScreenXY(gInventory.Controls[startblock].X + 10, gInventory.Controls[startblock].Y - 10); 
if (control == null)
   return false; // or otherwise cancel the action


It is essential to do this check when you are using GetAtScreenXY, just in case, to prevent null pointer errors.

2) Secondly, according to the manual, GUIControl.X "specifies its left edge, and is relative to the GUI which contains the control."
But GetAtScreenXY requires absolute screen coordinates. So you need to add GUI.X and Y to these coordinates:
Code (ags) Select

GUIControl *control = GUIControl.GetAtScreenXY(gInventory.X + gInventory.Controls[startblock].X + 10, gInventory.Y + gInventory.Controls[startblock].Y - 10); 
if (control == null)
   return false; // or otherwise cancel the action




3) By the way, if GUIControl did not have ID parameter, your game simply would not compile. As opposed to script languages like Lua or Javascript, where you cannot detect if object has or does not have particular member before running the program, it is impossible to compile AGS Script with variables that do not exist.
Title: Re: How to determine a Button.ID from a location?
Post by: MiteWiseacreLives! on Fri 27/10/2017 14:09:23
Thank you again, CR.. Got it working :-D
dang mixed coordinates slipping by me again.