How to determine a Button.ID from a location? SOLVED

Started by MiteWiseacreLives!, Fri 27/10/2017 04:49:45

Previous topic - Next topic

MiteWiseacreLives!

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

      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

Crimson Wizard

#1
First of all, you do not check a pointer returned by GetAtScreenXY:

Code: ags

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

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.

MiteWiseacreLives!

Thank you again, CR.. Got it working :-D
dang mixed coordinates slipping by me again.

SMF spam blocked by CleanTalk