Game Development Community

Inventory System

by Do Not Delete · in Torque X 2D · 10/17/2007 (7:17 am) · 2 replies

I was wondering, what is the best way to start coding an inventory system? Something that will manage all the guns, how much ammo they have, which guns they have and have picked up, and etc... An inventory like Gunstyle is what I want to create. (www.paradeofrain.com).

If anyone can help me that would be great!

#1
10/18/2007 (12:46 am)
I don' t know if this is the best way to create an inventory system. But, I took the idea and spent a couple hours and threw this together.... www.envygames.com/share/inventory.zip

Essentially, I created two components: InventoryComponent and InventoryItemComponent.

InventoryComponent is attached to the player. It maintains a strongly typed list of InventoryItemComponent objects. You can use it to get/set an active item (like a selected weapon slot), to add/remove an item (slot), or to simply use an item (reduce the count within a slot).

InventoryItemComponent is attached to a scene object (like a weapon, healthpack, coins) pickup item. You can easily add the component within Torque X Builder. After you add the component to an item, you can set its ItemCount (how many things of this type), ItemCapacity (how many things the player can collect), and ItemName (name of the things). Also be sure to include a T2DCollisionComponent for the pickup things - once you do, you can set the OnCollision property to point to the AddItemToInventory() delegate method. This way, when you collide with something, it automatically is added to the inventory.

Then, all you need to do is setup your InputMap bindings. For example, add something like this to your MovementComponent....

inputMap.BindCommand(keyboardId, (int)Keys.Space, UseInventoryItem,null);
inputMap.BindCommand(keyboardId, (int)Keys.N, NextInventoryItem,null);
inputMap.BindCommand(keyboardId, (int)Keys.R, RemoveInventoryItem, null);

and then implement the resulting action methods...

//use an item, such as a health pack or spell
public void UseInventoryItem()
{
    //assumes:PlayerManager.Instance.GetPlayer(playerIndex).ControlObject = player;

    //get the selected inventory item
    InventoryComponent inventoryManager = PlayerManager.Instance.GetPlayer(_playerNumber).ControlObject.Components.FindComponent<InventoryComponent>();
    
    //you can get some information about the current selection
    int idReadyToUse = inventoryManager.ItemCurrentlySelectedID;
    string ReadyToUse = inventoryManager.ItemCurrentlySelected;

    inventoryManager.UseItem(ReadyToUse, 1); //use this item 1 time
  }

//remove this type from the inventory
public void RemoveInventoryItem()
{
   InventoryComponent inventoryManager = PlayerManager.Instance.GetPlayer(_playerNumber).ControlObject.Components.FindComponent<InventoryComponent>();
   inventoryManager.RemoveItem(inventoryManager.ItemCurrentlySelected);
}

//move on to the next inventory type
public void NextInventoryItem()
{
    InventoryComponent inventoryManager = PlayerManager.Instance.GetPlayer(_playerNumber).ControlObject.Components.FindComponent<InventoryComponent>();
    int maxitems = inventoryManager.NumberItemsAvailable;

    if( inventoryManager.ItemCurrentlySelectedID + 1 < maxitems )
        inventoryManager.ItemCurrentlySelectedID += 1;
    else
        inventoryManager.ItemCurrentlySelectedID = 0;
}

Again, this is just my playing around for a couple of hours, so I don't promise it to be the best implementation of an inventory system. But, it might provide you with something that you can extend.

John K.
#2
10/18/2007 (7:15 am)
Wow, thanks!

I'll try this out today when I get home ;)