- Home /
Preventing Items from shifting in a list
I'm using a list for the inventory, and when the player drops an item (Inventory.remove(index)) all of the items in the inventory shift up a spot to fill in the empty space. My question is:
Is there a way to stop this from happenning?
Should I be using an array?
If I use an array how would I add an item to the inventory (Equivalent to list.add)?
Does anyone have any suggestions?
Thanks
Without seeing your code, this becomes just a discussion question. Consider creating a item for your inventory called 'No Item'. When you drop an item, you would place a 'No Item' in the list. When adding an item, you would first search out any slots that have 'No Item' in the slot and replace them, only adding a new item to the end of the list if no 'No Item' slots are found.
I'll suggest another approach. If you are modeling an inventory with a certain number of slots, an array might be exactly what you are looking for. You could quickly check if any inventory slots are empty by walking the array, looking for nulls. If you find a null, add the inventory item to that index. If not, tell the player that they don't have space for a new item.
Actually using an array does sound appealing to me now, and it would also allow me to be able to click and drag things around the inventory as well. I think I will remodel my system to implement an array
Answer by pborg · Jul 02, 2013 at 03:08 AM
You could set each element to be a class (if not already); in each list add a Boolean called canbeseen. if a player or any other script tries to view the list or modify it just set it so that it won't be shown unless canbeseen. When you try to place something in that spot, just overrideit. Hope this helps!
Your answer
Follow this Question
Related Questions
Scroll List Problem 1 Answer
Setting generic list length 2 Answers
Can't add GameObjects to ArrayList 1 Answer
A node in a childnode? 1 Answer
How can I store game data outside of scripts.(e.g Total list of inventory items) 2 Answers