Inventory Slots

Subscribe to Inventory Slots 10 posts

avatar for Aceeri Aceeri 98 posts
Flag Post

So I’m working on an inventory system with 24 slots and such. But how could I specify the item and remove the instance of the object, then push the specified item into the array? One other thing is that I need to have 24 slots in specific positions. Each slot is 50 pixels either on the y or x axis. 4 slots per row. 8 rows. Also, for some reason it infinitely checks

Obviously the slots array.

private var slots:Array = new Array();

Inventory Array with unlocked slots. (Going to add that later. Need to fix this first hough.)

var InvUnlocked:int = 24;
var InvArray:Array = new Array(InvUnlocked);

This is just adding the slots to the stage…

for (var i:int = 0; i < 24; i++)
			{
				slots[i] = new Slot();
				this.addChild(slots[i]);
			}

You press E and then it checks through the array for a null area.

if(e.keyCode == Keyboard.E)
			{
				for(var i=0; i<world.numChildren-1; i++)
				{
					if(world.getChildAt(i) is PickUpItem && player.hitTestObject(world.getChildAt(i)))
					{
						for(var i:int = 0; i < 6; i++)
						{
							if(InvArray[i] == null)
							{
								InvArray[i]= //I'm wondering about this...
							}
							trace(InvArray)
						}
					}
				}
			}

This just opens the inventory.

private function handleInventoryClick(e:MouseEvent):void
		{
			if(inv){
				addInventory();
			}
			else
			{
			 	removeInventory();
			}
		}
		private function addInventory():void
		{
			addChild(inv);
			trace("Inventory Works")
		}
		private function removeInventory():void
		{
			removeChild(inv);
			inv=null;
		}

Inventory Class (Nothing in it yet, not sure if I should use it for this though.)

package
{
	import flash.display.MovieClip
	public class Inventory extends MovieClip
	{
		public function Inventory() 
		{
			
		}
	}
}
 
avatar for NineFiveThree NineFiveThree 1378 posts
Flag Post
Originally posted by Aceeri:

24 slots […] 4 slots per row. 8 rows.

First of all, get the math right or explain how the layout should look like exactly.

Originally posted by Aceeri:

But how could I specify the item and remove the instance of the object, then push the specified item into the array?

I’m not sure if I understood that question. But the answer is probably: yes, exactly that: take the reference and pass it to the Inventory.

Originally posted by Aceeri:



You press E and then it checks through the array for a null area.

if(e.keyCode == Keyboard.E)
			{
				for(var i=0; i&lt;world.numChildren-1; i++)
				{
					if(world.getChildAt(i) is PickUpItem &amp;&amp; player.hitTestObject(world.getChildAt(i)))
					{
						for(var i:int = 0; i &lt; 6; i++)
						{
							if(InvArray[i] == null)
							{
								InvArray[i]= //I'm wondering about this...
							}
							trace(InvArray)
						}
					}
				}
			}

This is spaghetti code. There’s the keyboard and the “world” (whatever that is) and hittest and the inventory and it’s all in a mess.
Write your code in many small chunks and combine those to build the whole thing.

The Inventory is a class, you would definitely not mess with its Array.
There would be stuff like:

if (inventory.hasEmptySlot)
inventory.add(item);
 
avatar for vesperbot vesperbot 1883 posts
Flag Post

you definitely have to use Inventory class, determine what and how should you store and retrieve (pick up, transfer or display only) its contents, and then create a data structure within that class and maintain its integrity within it. It’s called encapsulation, and is good practice. You then provide a “put” method for Inventory that takes an item and stores it in the inventory, “get” method (be it by index, by reference or by any other way) that returns an object just released from inventory, etc.

 
avatar for Aceeri Aceeri 98 posts
Flag Post

@NineFiveThree

I did give an explaination on how the layout should look. Its 50 pixels on the y and axis. This makes it seem like you didn’t even read the whole thing.

 
avatar for Amibtious Amibtious 394 posts
Flag Post

953s’ point is the
4 slots per row * 8 rows = 24 slots
thing.

 
avatar for Aceeri Aceeri 98 posts
Flag Post

Oh, sorry. I counted the slots wrong x.x I meant 6 rows 4 slots per row.

 
avatar for Ace_Blue Ace_Blue 1128 posts
Flag Post

Soo… two posts just to focus on that? What about everything else 953 said? He’s trying to point you in the right direction and so is vesperbot, why don’t you respond to their other comments, or at least thank them if you’re still digesting the info (which, if you’re as new as you seem to be, is perfectly understandable)?

How many slots the inventory will ultimately contain is a moot point, and so is the size of each slot. If you code it well, changing that kind of stuff around should be a matter of altering a few constants at the top of the file. So, to reiterate, what you need is an Inventory class, of which your inventory variable will be an instance. The class should contain several public functions such as:
putIn(item:Item):Boolean // Places the Item you pass it in the proper inventory slot. Returns true if it managed, false if it didn’t.
getOneOut(slot:int):Item // Takes one Item from slot ‘slot’ and returns it, so you’ll know what it was. Note that putting that item wherever it is you put items (mouse pointer, ground, whatever) is a separate operation.
getStackOut(slot:int):Stack // Takes the whole stack of items (assuming your inventory allows item stacking) in slot ‘slot’ and returns it.
switchStacks(input:Stack, slot:int):Stack // Replaces the stack in slot ‘slot’ with the stack given as input, and returns the original contents of slot ‘slot’.
isFull(slot:int):Boolean // Tells you whether a slot already contains a stack or not.
isAllFull():Boolean // Tells you whether the entire inventory contains at least one empty slot.

The Array of contents should be an Array (or a vector, for a marginal speed improvement!) of Stacks, a Stack being an Item with an extra number indicating the nature of the items stacks, and how many there are, respectively. Bonus points if you make an isFull() function part in the Stack class, because that’s ultimately where it belongs. An Item you should already have, it’s the class that defines the pickable items in your game. Good luck!

 
avatar for Aceeri Aceeri 98 posts
Flag Post

Closed Thread

 
avatar for NineFiveThree NineFiveThree 1378 posts
Flag Post
Originally posted by Aceeri:


Closed Thread

The hr tag is powerful indeed (almost as mighty as blink), but not that powerful to close a thread.

Are your questions answered? Could you be so kind and give at least some feedback?

 
avatar for Aceeri Aceeri 98 posts
Flag Post

Well, I figured that I could make a slots array and when the item is spawned on the map, it will be inserted into a pickUpItem array. Then it will remove the instance of the object if you press e while colliding with it.