- Home /
Creating an inventory/weapon array and ability to scroll through it
I'm horrible at arrays but I hope this might be a chance to truly learn how they work.
What I want is to create an inventory system similar to how you handle weapons in an fps. I've already created the script to pick an item up, making the item "equipped" and "accessed" through booleans. Accessed means the player owns the object and can choose to equip it. Equipped means that it's the item that the player is currently holding. I also have animations that will play whenever the player equips weapons and unequips them. And all the functions and stuff are pretty much finished as well.
But now I need some lines of code that makes the game skip the items that are not accessed yet so that when I use the scrollwheel to go through the items I will only equip items that I have access to and not the ones I am yet to encounter. I'm thinking maybe some loop searching through an array or something.
I guess the order of the events would be: Scrollwheel up/down -> if(itemcount > 2) { -> Unequipitem(); (Which includes the unequip-animation, yields everything until the animation is finished and then hides the weapon using a layer-switch) -> The loop to search for the next weapon -> When it's found use Equip(item); (item would be the name of the item to be equipped (or maybe a number could work as well))
Answer by Berenger · Feb 03, 2012 at 05:39 AM
You'd have less trouble by having an AccessedWeapons array, that you update when you find a weapon. That way, you don't go through the array containing (if I understand correctly) all the existing items.
I see, thanks. Got any clues as to how I should begin coding the array?
Well, one way would be to create it of the size maximal, the number of existing item. (Item[] accessedItem = new Item[biItemTotal]) then, with a counter nbAccessedItem, each time you pick one up accessedItem[ nbAccessedItem++] = pickedUpItem (make sure it doesn't exist already).
A list might be more appropriate though.
Your answer
Follow this Question
Related Questions
Creating a dynamic list in OnGUI 1 Answer
[C#]Inventory script help. 3 Answers
Stackable Inventory Item 1 Answer
how to detect wich item i have in my inevntory 0 Answers
Item and Inventory for a RPG game 2 Answers