Drag and Drop Inventory with LibGDX

Many games are in need of a UI for an inventory. So does our game Pocket Galaxy and that’s why we thought to share our solution with you.

Our inventory basically offers the following features:

  • Inventory is placed in an extra window
  • Grid-based layout
  • Random amount of slots
  • Icons are displayed for the items
  • Items can stack
  • Tooltips with additional information
  • Items can be moved around via Drag and Drop
  • It’s possible to define valid and invalid targets

Especially the point about drag and drop might scare many people, since that’s usually a lot of work, but with LibGDX and its scene2d framework (and the DragAndDrop class) it’s a piece of cake. The last point on the list means that you are even able to use this to drag and drop items (for example armor/weapons) on a character slot to activate those, or to an action bar with hotkeys, or into a crafting grid like in minecraft.

The end result might look like this, but can easily be changed with another Skin.

Inventory preview.

Click here for a live preview

The full code can be found here in our public repository on GitHub.

This tutorial is divided into four seperate parts:

To start with the first part, click here.

Leave a Reply

Your email address will not be published. Required fields are marked *