When equipping a backpack how can i make the inventory expand?

Hello. I have been working on a project that simulates a zombie apocalypse and i am trying to make it as realistic as possible but then a problem occurred. I simply have no idea how to make the inventory expand after equipping a backpack or pants with lot of pockets. Any help is previously appreciated. Thank you :slight_smile:

Depending on your data structure that you use for your inventory: likely a hashmap or a list, you will want to have a block of code that executes when you put on the backpack.

Very Much Rough/Pseudo Code:

class Backpack{
    function OnEquip{
        BroadCastMessage{"IncreaseInventory",8);
    }
}

class Inventory{
    function IncreaseInventory(int howMany){
        inventoryList.slots += 8;
    }
}

You can easily set up code to do that, there are many different ways.

What might be better is to have an Inventory Manager object on your character that can hold any amount of objects that have inventory components. And when you pick up an item it just gets put in the first available slot of any inventory item by the Inventory Manager. Thus, when you bring up the inventory menu, you can iterate through each inventory, iterating through each inventory item, seperating them in the overall list by where the items are being held. You will probably want to use transform parenting for your inventories, and have checks for number of transform children to determine the inventory’s current capacity. This way, when you drop the backpack item, all the items in the backpack are dropped along with it etc.

Okay, I did a rough draft of what you might be looking for, I haven’t tested it, so … expect problems:

//this is the size of our left pocket
var leftPocket : int = 4;
//this is what is actually inside our left pocket
var itemsInLeft = Array();

//this is the size of our right pocket
var rightPocket : int = 4;
//this is what is actually inside our right pocket
var itemsInRight = Array();

var haveBackpack : boolean = false;

function OnGUI(){

	//whatever object is added to the leftPocket/rightPocket array, it will show you in your GUI
	//show whats in the left pocket
	for(var i = 0; i < itemsInLeft.length; i++){
		GUI.Label(Rect(20, 10 + (30 * i), 100, 30), "Left: " + itemsInLeft*.gameObject.name);*
  • }*

  • //show whats in the right pocket*

  • for(var i = 0; i < itemsInRight.length; i++){*
    _ GUI.Label(Rect(Screen.width - 100, 10 + (30 * i), 100, 30), "Right: " + itemsInLeft*.gameObject.name);_
    _
    }*_

}

function Update(){

* if(haveBackpack){*

* leftPocket = 8;*
* rightPocket = 8;*

* }*
}

//call this anywhere, with the object you want to add, and the pocket
function AddItem(item : GameObject, pocket : String){

* //we are refeering to the left pocket, also check if the pocket isnt full yet*
* if(pocket == “left” && itemsInLeft.length <= leftPocket){*
* itemsInLeft.Add(item);*
* }*

* //again, but with the right*
* if(pocket == “right” && itemsInRight.length <= rightPocket){*
* itemsInRight.Add(item);*
* }*
}
So whats happening is that you have an array, both for your left and right pocket. You can add an object by calling the AddItem and include the item you want to add, and which pocket. This also updates your GUI.
Let me know how it goes

@oliver-jones
I got a CS8025 error can someone help how to fix it