Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: NiksterG on Tue 28/04/2009 02:02:35

Title: BUG: GUI x and y coordinates not always accessible
Post by: NiksterG on Tue 28/04/2009 02:02:35
This is really weird.

First, a little background: I'm trying to make a game that has a right-click menu. Whenever the player right-clicks on an inventory item, a menu GUI with a few options opens at (mouse.x, mouse.y). Depending on which option is clicked, I run a function. One of those functions accesses the GUI's x and y coordinates to find out which item was clicked using the InventoryItem.GetAtScreenXY() function.

Now the weird part: it seems that if the GUI is visible, it's x and y are null, yet at the same time a valid integer. My game crashes if I call the GetAtScreenXY() function while the GUI is visible, but if it's not, then it runs fine. I've checked with a display the GUI coods while it's visible, and it returns a valid location. Any ideas what's going on?

The error I'm getting:
AddInventoryToCharacter: invalid invnetory number

I've checked and double checked, it should be calling the right inventory number. Besides, all I change is the visibility setting og the GUI and it works.
(And yes, it is spelled invnetory in the error  :P)
Title: Re: BUG: GUI x and y coordinates not always accessible
Post by: Gilbert on Tue 28/04/2009 02:31:37
Hmmm Your description only mentioned checking what inventory item is on a screen location, but didn't mention you had to add an inventory item to a character, yet the error displayed was about adding inventory items. Can you elaborate on that?
Title: Re: BUG: GUI x and y coordinates not always accessible
Post by: NiksterG on Tue 28/04/2009 02:38:25
Sure thing.

Here is my code:

function btnMonstDefDiscard_OnClick(GUIControl *control, MouseButton button)
{
    gRClickMonstDef.Visible = true; // if true, game crashes. if false, game runs fine.
    cPlayerDiscard.AddInventory(InventoryItem.GetAtScreenXY(gRClickMonstDef.X, gRClickMonstDef.Y)); // game crashes at this line
    cPlayerHandInBattle.LoseInventory(InventoryItem.GetAtScreenXY(gRClickMonstDef.X, gRClickMonstDef.Y));
}
Title: Re: BUG: GUI x and y coordinates not always accessible
Post by: Gilbert on Tue 28/04/2009 03:07:11
Are you sure the upper-left corner pixel of gRClickMonstDef corresponds to an inventory item? If not, the returned inventory item would be null. I couldn't understand why it wouldn't crash when the GUI is off though, maybe there's some other GUI beneath it that contains an inventory item?

Try to do this to check (for both GUI visible or not):

function btnMonstDefDiscard_OnClick(GUIControl *control, MouseButton button)
{
    gRClickMonstDef.Visible = true; // if true, game crashes. if false, game runs fine.
    If (InventoryItem.GetAtScreenXY(gRClickMonstDef.X, gRClickMonstDef.Y)==null) Display("null!");
  //  cPlayerHandInBattle.LoseInventory(InventoryItem.GetAtScreenXY(gRClickMonstDef.X, gRClickMonstDef.Y));
}


No matter what, always check whether the retrieved inventory item is null before adding ot to a character.
Title: Re: BUG: GUI x and y coordinates not always accessible
Post by: Trent R on Tue 28/04/2009 03:16:12
What if you checked -1 from each X, Y value. Not sure, but could the Inv.GetXY (that's supposed to be shortened) be triggering something crazy if a Gui is in the way?

~Trent
PS-I checked the error typo too. Hehe, nice find. :P
Title: Re: BUG: GUI x and y coordinates not always accessible
Post by: NiksterG on Tue 28/04/2009 03:27:36
I tried the code, and like you predicted, the item was null. Even if I hard code the values in, it's still null.

But, after some more experimenting, I found the problem. When the GUI is off, it can get the inventory item underneath it because the GUI is not there. When it is visible, it's X and Y coordinate is actually the top left pixel of the GUI. Of course, there is no inventory item on that pixel - or GUI, for that matter. Calling this:


   cPlayerDiscard.AddInventory(InventoryItem.GetAtScreenXY(gRClickMonstDef.X - 1, gRClickMonstDef.Y - 1)); // game crashes at this line


fixes the problem. (subtract 1 from the GUI's coordinates.)

EDIT: yup, Trent, i guess you found it before me. Nice work! And btw, that spelling error has been in AGS since 2.72 at least, if I remember correctly. hehe  ::) ;D