BUG: GUI x and y coordinates not always accessible

Started by NiksterG, Tue 28/04/2009 02:02:35

Previous topic - Next topic

NiksterG

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)
Check out my games! (Not all made with AGS)

Gilbert

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?

NiksterG

Sure thing.

Here is my code:
Code: ags

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));
}
Check out my games! (Not all made with AGS)

Gilbert

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):
Code: ags

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.

Trent R

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
To give back to the AGS community, I can get you free, full versions of commercial software. Recently, Paint Shop Pro X, and eXPert PDF Pro 6. Please PM me for details.


Current Project: The Wanderer
On Hold: Hero of the Rune

NiksterG

#5
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:

Code: ags

    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
Check out my games! (Not all made with AGS)

SMF spam blocked by CleanTalk