- Home /
Dynamic update List<>
Like the title says. Is it possible to update a List<> dynamically?
I currently use a List<> for my player inventory, however when the player have used an item and I remove that item it removes every item before it in the list as well. I assume this is since the list can't update the items before to new positions.
Second question: Should I use an array for this instead?
The thing is that I have a player inventory of 9 objects, and at the moment I never want it to get bigger, so I guess I could use an array. But I think list is better and easier since I would not then have to check how many items the player currently has all the time since I just go through how big the list currently is to check that. But then as I said I need to update it dynamically when the player uses an item.
So how do people usually do here?
// Code that adds item to inventory
if (this.GetComponent<pl_Inventory>().iCurrItemsInInv <= this.GetComponent<pl_Inventory>().iMaxItemsInInv)
{
this.GetComponent<pl_Inventory>().listOfItems.Add(rayHit.collider.gameObject.GetComponent<Item_Base>());
rayHit.collider.gameObject.transform.parent = gHand.transform;
rayHit.collider.gameObject.SetActive(false);
}
else
{
Debug.Log("Inventory is full");
}
// Code that removes the item
/// <summary>
/// Here we get all the puzzle gameobjects. There will be turned off when the player if finished, or even deleted.
/// *This should be the prefab with all puzzle parts in it. Going to be deleted or turned off when done.
/// </summary>
public GameObject clItemNeeded;
if (coll.tag == "Player")
{
gFinishedStairCase.gameObject.SetActive(true);
gPlayer.GetComponent<pl_Inventory>().listOfItems.RemoveAt(clItemNeeded.GetComponent<Item_StairPlanks>().iItemID);
Destroy(clItemNeeded);
}
There's something wrong with your code. Lists can remove any single item without affecting the rest. Post your code.
is iItemID property an actual index of the item in list ?
Because gPlayer.GetComponent().listOfItems.RemoveAt() requires an index.
You can just use Remove with clItemNeeded.GetComponent() no need to get the index and use RemoveAt
Answer by TobiasJohansson · Mar 31, 2014 at 11:28 AM
Okay, added this to the code, now it seems to work:
if (coll.tag == "Player")
{
// Get the index for the item we want to remove
int iRemovableItem;
iRemovableItem = gPlayer.GetComponent<pl_Inventory>().listOfItems.IndexOf(clItemNeeded.GetComponent<Item_StairPlanks>());
// Turn on puzzle piece
gFinishedStairCase.gameObject.SetActive(true);
// Set current item to -0 and bHoldingItem to false so player no longer has any item in his hands.
gPlayer.GetComponent<pl_Inventory>().bHoldingItem = false;
gPlayer.GetComponent<pl_Inventory>().iChosenItem = -1;
// Remove item from list and destory item so player no longer has it.
gPlayer.GetComponent<pl_Inventory>().listOfItems.RemoveAt(iRemovableItem);
Destroy(clItemNeeded);
}
you could make this more efficient (and more maintainable) by storing the reference to the item. also, it's probably better to store the Item_StairPlank component reference somewhere too.
if (coll.tag == "Player")
{
// Get reference to item to remove
var item = gPlayer.GetComponent<pl_Inventory>();
// Get the index for the item we want to remove
int iRemovableItem = item.listOfItems.IndexOf(clItemNeeded.GetComponent<Item_StairPlanks>());
// Turn on puzzle piece
gFinishedStairCase.gameObject.SetActive(true);
// Set current item to -0 and bHoldingItem to false so player no longer has any item in his hands.
item.bHoldingItem = false;
item.iChosenItem = -1; // Comment says -0...
// Remove item from list and destory item so player no longer has it.
item.listOfItems.RemoveAt(iRemovableItem);
Destroy(clItemNeeded);
}
which bit?
depending on the overall code structure, it might be more efficient to store the reference to the Item_StairPlanks component in a public/private variable for the code here to use ins$$anonymous$$d of calling GetComponent each time the player collides with it. if you use it in other places, it might make sense - especially if the code is cleaner/simpler.
without seeing the entire codebase, it's impossible to say whether that's worth doing or not...
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
A node in a childnode? 1 Answer
[C#]Inventory script help. 3 Answers
Adding Item object to Inventory List 0 Answers