LibGDX - Draw inventory in one place (The correct way)

Hi all.

Sorry for the noob question but i’m building my inventory system from scratch. I need to keep the inventory in once place at all times (When the user presses “I”).

At the moment i’m making the inventory use its own spritebatch for the rendering as this keeps it in one place, however it is drawn pretty small. The inventory is also drawn in the bottom left but the “contains” method that tells me if the user is hovering over a slot is returning in the top right. Do I also need to give the batch a camera so it flips the inventory to the top of the screen? Or am I missing something :point:

I think it’s more easy create an inventory and other interface elements using a Stage and Scene2D. If you are using the same batch (and this batch is projected to the camera matrix and this camera moves) you will have to solve many problems.

The Game: Running on “world virtual coordinates”.
Interface: Running on “Screen coordinates”.

You can do this using the same batch and camera, but I think you will get more work, including all other elements.

The issue with Scene2D is that it has drag and drop support but it is not good for what I need. I need the player to be able to drag items out of inventory and throw them on the ground and I don’t believe this is possible. :frowning:

Yes you can.

I did it in my game: https://play.google.com/store/apps/details?id=com.drawyourway.games.android
You draw horizontal lines in “screen coord” and the line is drawn in game coord.

You can convert (the right word is to project actually) Screen coord --> Game Coord or Game Coord --> Screen Coord using camera.project() or camera.unproject().

So, you can apply this after your drag and drop. In your case, create a new object in the game world (using the new coord).

:open_mouth: I read online that you couldn’t do that due to needing to drop it onto a source. Maybe I can make the array the source and then for every object in the array, draw the items at their coords.

I would do camera.project to get screen to world cords, correct? :slight_smile:

You can’t drop the same source object, because the interface it’s only an image, but you can get all the information needed to spawn the object in the game.

Screen to World: camera.unproject(Vector3);

Your Vector3 will have the new coord and you can use an ID from the item dropped to know which item need to spawn.
:smiley:

Thank you very much for your help! I’ll try creating a drop and drop with Scene2D and the Stage instead

Hmm.

I have a table built for my inventory system and each cell is an imagebutton (for the slots) but I have an issue. I can’t find out anyway of getting which child from the table was touched so I can begin dragging it.

Also, if I already have image buttons for my table, won’t I need to create a new image for every single weapon/potion/armor to change the image on the table? If I simply overlay it then how will I get the information? D:

You can implement an interface with all the values you need to your class extending ImageButton (to know/show the info about the iten), I think a Table can be hard to manage drag&drop.
Maybe you can create your own array and grid, and just set a region when you need (in the inventory slot and when is dragged).

Here’s an example: http://pixelscientists.com/demos/inventory/index.html
Git: https://github.com/PixelScientists/libgdx-utils

That’s what I was thinking ;D storing info on current item, stack size and max stack inside a class called ‘Slot’ that extends the imagebutton. Can simply get the info I need that way and shouldn’t have to deal with annoying tables :point: