Select inventory item with a button [SOLVED]

Started by Stranga, Tue 29/09/2020 14:35:02

Previous topic - Next topic

Stranga

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

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!

:)

Crimson Wizard

#1
Button coordinates are not screen coordinates, they are relative to parent GUI.

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

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

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



EDIT: fixed math.

Stranga

#2
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.

Khris

#3
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.

Stranga

I was just typing that out as you posted Khris! :) It seems to function a lot smoother as well!

SMF spam blocked by CleanTalk