RPG inventory

How would one go about implementing this?

Think wow style loot window full of boxes and an inventory full of boxes. When you click an item it hovers below your cursor until you drop it in a slot again.

Thanks guys!

Keep an array of Items. The mouse also has a single Item slot. When an Item in the bag is clicked swap it with the mouse Item slot. Done! =D

Would each item be an object and the array an object array?

You would make an Item class, and have all other items subclass that class. Then you can define your Item array as simply

Item[] inventory = new Item[INVENTORY_SLOTS];

Thank you! This seems like a solid plan of attack.

Hello

You can assign each item with an unique id, and make an item array. Visually make all the elements of your inventory draggable, and with the “snap to its box container” ability. Make sure you have a thumbnail for each of your elements in a library, and name them with the id assigned. That way, you can make a “for” loop to place all the pieces of elements on each container, just as your array is organized.

Good luck.

Why would he need an ID for each item? If he wants to draw all the Item icons, just do something like this:


private static final int INVENTORY_SLOTS= 30;

private static final int INVENTORY_COLUMNS = 6;

private Item[] inventory = new Item[INVENTORY_SLOTS];

...

for(int i = 0; i < INVENTORY_SLOTS; i++){
    if(item[i] == null){
        continue;
    }
    int slotX = i % INVENTORY_COLUMNS;
    int slotY = i / INVENTORY_COLUMNS;
    drawImageWithSomeLibrary(
            items[i].getIcon(), //Image to draw
            inventoryWindowPositionX + slotX * SLOT_WIDTH, //x coordinate
            inventoryWindowPositionY + slotY * SLOT_HEIGHT //y coordinate
    );
}


I have a Inventory Demo which shows 4 gems which you can drag around. The source code is available on my site.

Well, it depends on where the item is getting stored, etc. An Item ID can make storing things on DB easier, or if the item IDs are semi-permanent, could make the save foot print a bit smaller by just needing item_id x number_of_items to describe each slot.

ps: Don’t forget that mission quests can be done by making then ‘invisible’ items in your inventory.

Item Interface would be better than Item abstract class. Then you can have Super class like Sword, Potion, Scroll.

Thank you for the amazing example and code Mathius this will definitely help in creation!

One thing i didnt mention is that items will have various random statistics and abilities (think diablo). This would render an item ID fairly redundant as each item is unique.

Im still fleshing out a flow chart but what I have in mind so far is,

abstract class Item
subclass Weapon
subclass Armour
subclass Ammo
subclass Bag
subclass Scroll or Book
subclass Resource
subclass General

Each subclass contains a rightClickAction method and various other methods and variables unique to its type. All the items will feature two constructors one with arguments dropLevel and dropQuality for generating the item randomly then another constructor that sets all the stats of the weapon so objects may be recalled from a saved game. Inventory will be an item array that stores these items and the GUI will simply move these around within the array.

Now, good in theory…