Getting certain values from an item in a list
Hi there!
Let me start off by saying that I might be thinking of this problem entirely the wrong way. If I am, please let me know of a better solution for my problem. I'll go into more detail about what exactly Im trying to get done later on in the thread.
Okay so here's my issue. I have the following snippet of code. The function is (supposed to be, I havent implemented it yet) called every time an item is picked up. The item is picked up by the player colliding with it and then pressing a button. The item picked up contains the variable LootID (thats literally all it contains) which is the same as the itemID of the item it dropped.
So here's the code:
public void OnPickup ()
{
for (int i = 0; i<itemList.Count; i++)
{
//if currently numbered item in list's itemID is equal to the ID on the looted object
//Check if there's weight available for it
//Then Add one to the selected item's itemQtyOwned
//Then set pickedup bool to true so the item you picked up can be destroyed
//Else
//Set pickedup bool to false so the item you picked up isnt destroyed
}
}
What Im trying to figure out is how I can use i to determine the item it's currently at in the process of looking through all existing items to then select that item's itemID variable so I can compare that to the picked up item's lootID. I just have no idea how to grab a specific item from a list like that and I had a hard time finding this anywhere. Hope someone can help me out here :)
So for those who want more info about what Im trying to get done here, here goes. Im trying to build a skyrim-like inventory using not inventory slots but weight. There's a few scripts managing a few processes. First there's the inventoryManager script that contains the class inventoryManager and the subclass Item and all the constructors for the different kinds of items. It also contains all items and a list of all items and the code to add (and in the future remove) items from the inventory (on pickup). Then there's the items that you can pick up which contain a snippet of code with their lootID, which as stated before corresponds with the itemID of the item that dropped. And then there's the code managing the UI side of things, which I havent properly worked out yet because I havent gotten the back-end working yet. If you've got any feedback on how Im developing this system or have any more questions about it, feel free to post it. Anything is appreciated. Its my first time working out an inventory and I couldnt really find much about what Im trying to do online so yeah, any help is appreciated.
Thanks for your time and help!
well if we're talking about weigh as in kg / lb (but obviously some other unit) then each item will have a weight assigned.. correct? then you could have a static int/float that has the total weight of the inventory.. when an item is picked up it adds the weight of the item to the total weight and when an item is used then it subtracts this... is this what you're looking to do?
Also.. are you using scriptable objects for your items? this is generally the best way to store data (ie. weight)
Yes that's what I'm looking to do, but thats quite simple. No I'm not using a scriptable object for my items because I want to be able to change the int variable deter$$anonymous$$ing how many you have in your inventory in runtime. Or do you suggest I manage this some other way?
Your answer
Follow this Question
Related Questions
Error in Inventory Script 1 Answer
How do you sort a list of JSONNodes by key value? 1 Answer
Inventory Add Item error 2 Answers
list.contains problem 1 Answer
[Quiz Game] How to prevent Question asked twice. HELP 1 Answer