- Home /
Can't Assign Item In Array
So I've been working on an inventory script, I'm trying to test it out. Everything works fine except for the AddItem
method, shown below.
public void AddItem(Item item) {
// Check if a stack is already there for us to stack the item with
for(int i = 0; i <= 25; i++) {
// If nothing is in this inventory slot, check the next one.
if(Items[i] == null) {
continue;
}
// If stack is found, increase the stack size.
if(Items[i].ContainingItem.Name == item.Name) {
Items[i].StackSize++;
return;
}
}
// Create a new item stack.
ItemStack st = new ItemStack(item, 1);
// If no stacks of the same item is found, then
// check for space in inventory to put a new item stack into.
for(int i = 0; i <= 25; i++) {
// If item in inventory slot is null (no itemstack inside)
if(Items[i] == null) {
// Add a new item stack.
Items[i] = st;
return;
}
}
/*foreach(ItemStack i in Items) {
Debug.Log(i.ContainingItem.Name);
}*/
}
Here's my problem: whenever I add an item, it adds nothing, and the Debug.Log
stuff doesn't print anything, suggesting that nothing is being added to the array. Oddly enough, if I remove the 2 return
statements the entire inventory is litterally filled with nothing but that item. Will somebody please fix this, or atleast explain why this is happening?
EDIT: I've only included the AddItem method, I assure you everything else is working as expected.
EDIT: Items is a built-in array, I thought to use this because it would work very well with the inventory slots stuff. ItemStack is a class with 3 properties: ContainingItem, StackSize, and MaxStackSize. Stacksize is just a public variable (I know, it's bad, I'm sorry :P)
When an item that doesn't exist in inventory is added into a non-full inventory it should just add that item.
When an item that does exist in inventory is added it should increase stack size, I haven't checked if that works yet.
When the inventory is full, the item is dismissed and never seen from again. If you want the entire script here it is, but it's a bit huge and messy, I'll try to comment as much as I can.
Pastebin link to my inventory script is here: http://pastebin.com/kxupp6Ap
Item here: http://pastebin.com/YwB6ru29
and ItemStack here: http://pastebin.com/4YRSc5Bz
Answer by Bunny83 · Oct 12, 2014 at 04:53 AM
Some more background would help:
Is Items a built-in array or a container class like List?
Is ItemStack a class or a struct?
Is StackSize an int-field or an int-property (or maybe something else?)
What things you have already tried to debug your problem? Where have you put Debug.Logs, what did you log and what was the result?
In general there are 3 different cases when you add an item:
You add a new item that doesn't yet exists in the inventory. The inventory is not full.
You add an item that does already exist in the inventory
You try add an item that doesn't yet exists, but the inventory is full.
With which case you have problems and what's the exact result when you add the item? How far does the method run? Debug.Log?
With the information given it's near to impossible to figure out what might be wrong. The given code looks solid except that the full inv situation isn't handled at all and the item would just vanish.
Items is a built-in array, I thought to use this because it would work very well with the inventory slots stuff. ItemStack is a class with 3 properties: ContainingItem, StackSize, and $$anonymous$$axStackSize. Stacksize is just a public variable (I know, it's bad, I'm sorry :P)
When an item that doesn't exist in inventory is added into a non-full inventory it should just add that item.
When an item that does exist in inventory is added it should increase stack size, I haven't checked if that works yet.
When the inventory is full, the item is dismissed and never seen from again. If you want the entire script here it is, but it's a bit huge and messy, I'll try to comment as much as I can.
Pastebin link to my inventory script is here: http://pastebin.com/kxupp6Ap
Item here: http://pastebin.com/YwB6ru29
and ItemStack here: http://pastebin.com/4YRSc5Bz
So please help me! >_< I also practically spammed pastebin while doing this.
@$$anonymous$$arkPwns1: Everything works as it should except one small thing ;) Is it possible that you are used to work with indices that start at 1? Anyways, your problem isn't the adding of the item, but your loop that displays the items. You initialize "buttonnum" with 0 but you immediately increase it by one. That means you effectively are skipping the first item (index 0)
You should get used to 0 based indices since most languages use 0-based indices (for several reasons).
Your Items array seems to be too large. It holds 26 items ins$$anonymous$$d of 25, that's why you didn't get an index-out-of-bounds exception on the last element since your index is off by 1
Your answer
Follow this Question
Related Questions
Scroll List Problem 1 Answer
Real Simple Inventory and Vendor System Help 1 Answer
Array Overflow Problem 1 Answer
C# Adding to an Array 1 Answer