Hi everyone!

I seem to be in a pattern of asking a sorta difficult question followed by a really simple one. This time, I'm trying to make a simple 6-item inventory list you can scroll through with the mouse wheel and select with the middle mouse button. If possible, I'm also trying to make the menu loop. (As in, scrolling up from slot 6 will highlight slot 1, and scrolling down from slot 1 will highlight slot 6.)

I have an inventory of 6 instantiated panels as you can see below. Here's the scene tree and code for my UI scene:

`extends Control

@onready var inv: Inv = preload("res://inventory/inventory.tres")
@onready var slots: Array = $TextureRect/GridContainer.get_children()

func _ready():
updateSlots() #this function updates what items appear in each slot. it only happening at the start of the game is WIP

func updateSlots():
for i in range(min(inv.items.size(), slots.size())):
slots.update(inv.items)
`

And the scene and code for each individual panel object:


`extends Panel

@onready var item_visual: Sprite2D = $CenterContainer/Panel/display

func update(item: InvItem): #this is for showing the item in the slot
if !item:
item_visual.visible = false
else:
item_visual.visible = true
item_visual.texture = item.texture
`

I can store info in these panels no problem, but I'm stumped on how to highlight, scroll through or select them. What kind of code would I need to write to be able to do this? Please let me know if you can help!! Thank you once again!!

  • Toxe and xyz replied to this.
  • crobison I have not tried this but here are a couple of thoughts to get you going.

    First of all you are trying to rebuild what already exists: an ItemList control. It lets you select items from the list. I would try using that one first and set it to a single row with six columns.

    Then overwrite the _gui_input function to get GUI events and if it's the mouse wheel (MOUSE_BUTTON_WHEEL_UP / ..._DOWN) manually set the selected list item. And if you would select item index -1 or 6 just wrap it around to index 5 or 0.

    crobison I have not tried this but here are a couple of thoughts to get you going.

    First of all you are trying to rebuild what already exists: an ItemList control. It lets you select items from the list. I would try using that one first and set it to a single row with six columns.

    Then overwrite the _gui_input function to get GUI events and if it's the mouse wheel (MOUSE_BUTTON_WHEEL_UP / ..._DOWN) manually set the selected list item. And if you would select item index -1 or 6 just wrap it around to index 5 or 0.

      crobison I seem to be in a pattern of asking a sorta difficult question followed by a really simple one.

      Yeah looks like a really trivial problem compared to your last question 🙂

      Toxe This seems like a good solution, thank you :]