Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Stranga on Tue 29/09/2020 14:35:02

Title: Select inventory item with a button [SOLVED]
Post by: Stranga on Tue 29/09/2020 14:35:02
Hello everyone,

What I want to achieve is in the title. I'm trying to make a keyboard playable game and somewhat replicating the Resident Evil classic inventory. I was wondering if I'm able to check/activate what inventory item a GUI Button is hovering over. I'm using a single button as a cursor to switch between different elements.

Firstly, is it possible to do this or would I have to manipulate the mouse and process clicks to use the inventory?

Secondly, I have an idea on how I could do it. I tried this:

Code (ags) Select

InventoryItem *iat = InventoryItem.GetAtScreenXY(btnInvCursor.X, btnInvCursor.Y);

player.ActiveInventory = iat;


But it doesn't seem to work(I'm not sure if GetAtScreen works with GUI layers?). Any help would be greatly appreciated!

:)
Title: Re: Select inventory item with a button
Post by: Crimson Wizard on Tue 29/09/2020 14:43:28
Button coordinates are not screen coordinates, they are relative to parent GUI.

I'd imagine you should try something like:
Code (ags) Select

InventoryItem.GetAtScreenXY(btnInvCursor.X + parentGUI.X, btnInvCursor.Y + parentGUI.Y);


This is still not necessarily correct though, as I don't know how exactly the button is positioned, because above will get you coordinates of button's top-left corner. Maybe you should also offset by half size to get location under button's center:
Code (ags) Select

InventoryItem.GetAtScreenXY(btnInvCursor.X + parentGUI.X + btnInvCursor.Width / 2, btnInvCursor.Y + parentGUI.Y + btnInvCursor.Height / 2);



EDIT: fixed math.
Title: Re: Select inventory item with a button
Post by: Stranga on Tue 29/09/2020 14:49:57
Thanks for your help Crimson, I have a box as a cursor so I didn't think that it would take the coordinates from the corner of the button. I'll try offsetting on both the X and Y axis as it's a perfect square cursor. 

EDIT: Hmm, this doesn't seem to work as the player.ActiveInventory is still registering as null. I could possibly make a separate GUI as a button and use that? Although, I'd prefer to have everything on the on GUI if that's even possible.
Title: Re: Select inventory item with a button
Post by: Khris on Tue 29/09/2020 15:26:06
I'd use a different approach: store the index of the current item. When an arrow key is pressed, change the index, then move the button to highlight the item.

Say the current item index is 2, and the user presses the right arrow key. We increase the index to 3, then calculate the button position based on that.
That way, pressing the activation button will only require reading  invWindow.ItemAtIndex[selectedIndex]

In general, the game state should only be concerned with abstract data (like the index of the currently selected item), and coordinates should only come into play at the last step: displaying the game state on screen.
Title: Re: Select inventory item with a button
Post by: Stranga on Tue 29/09/2020 15:35:56
I was just typing that out as you posted Khris! :) It seems to function a lot smoother as well!