- Home /
check if something is on a list by one argument
I've looking for a way to do this, I created a custom class for the Items stored in the players inventory (a list), but I want to check if the Item is there using its "name" argument so if it is not there add it, and if it is there augment its quantity by +1
I've look into List.Contains but since what I need to compare it to its not of the same class I cant use it.
here the code of the Inventory manager (ignore the comments, they are not mine)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
// This behaviour script would be attached to the player
public class InventoryManager : MonoBehaviour {
// stores items picked up
private int ItemID;
public AudioClip[] PotionSounds;
private List<IItem> itemsInInventory = new List<IItem>();
void Update() {
Debug.Log(itemsInInventory[0].name + "_" + itemsInInventory[0].Cantidad);
if (Input.GetButtonDown("use")== true){
Use();
}
if (itemsInInventory[ItemID].Cantidad == 0){
itemsInInventory.RemoveAt(ItemID);
}
// check for use input to use item here, or in OnGUI, and call Use()
}
public void AddItem(GameObject Item){
// We cast to IItem to match our list type.
// We can do this because Potion implements IItem.
// This means we could store different types of items in the list,
// as long as they implement IItem.
Potion ItemScript = Item.GetComponent<Potion>();
itemsInInventory.Contains
itemsInInventory.Add(new IItem(Item.name,ItemScript.Cantidad,ItemScript.Icon,ItemScript.PotionValue, ItemScript.PotionParticles,ItemScript.Type));
}
// adds new item to end of list
void Use() {
if (itemsInInventory[ItemID].Cantidad > 0){
if (itemsInInventory[ItemID].Type == "Potion"){
GameObject Player = GameObject.FindGameObjectWithTag("Player");
Player.SendMessage("AddjustCurrentHealth", itemsInInventory[ItemID].Value);
Player.audio.PlayOneShot(PotionSounds[(Random.Range(0,PotionSounds.Length))]);
Instantiate(itemsInInventory[ItemID].Particles, Player.transform.position, Quaternion.identity);
itemsInInventory[ItemID].Cantidad += -1;
}
}
//if (itemsInInventory.Count > 0) {
// execute UseItem method of first item in list
/*itemsInInventory[0].UseItem();
// remove item from list since it has been used
itemsInInventory.RemoveAt(0);*/
}
}
IItem class:
// Interface, containing only signature (design), not implementation
using UnityEngine;
using System.Collections;
public class IItem {
// classes that implement Item should define this method
public string Type;
public string name;
public int Cantidad;
public Texture2D Icon;
public int Value;
public GameObject Particles;
public IItem(string newName, int newCantidad, Texture2D newIcon, int newValue, GameObject newParticles, string newType){
name = newName;
Cantidad = newCantidad;
Icon = newIcon;
Value = newValue;
Particles = newParticles;
Type = newType;
}
}
Thanks
Answer by whydoidoit · Oct 02, 2013 at 12:26 PM
Use Linq:
using System.Linq;
...
if(!itemsInInventory.Any(i=>i.name == "somename"))
{
//Add it?
}
Thanks a lot!, hey, you are from Unitygems.com right? love you tutorials!
I have a doubt though, "the its not there","is there" part is clear but when the item is there, how can I know which of the item on the list is it to add +1 to its quantity? for example
if(itemsInInventory.Any(i=>i.name == "somename"))
{
itemsInInventory[x].Cantidad += 1;
}
which Item is "x"?
I kind of solved my own doubt, dont know if its the best solution, I used a for loop to find the Item that has the same name:
for(int t = 0; t < itemsInInventory.Count; t++) {
if (itemsInInventory[t].name == Item.name){
itemsInInventory[t].Cantidad += 1;
return;
}
}
Your answer
Follow this Question
Related Questions
Custom Class List - Passing the Class Vs Passing an Index 0 Answers
Does a new instance of a custom class return null? 2 Answers
C# find specific piece of data and where it is in list 1 Answer
Gather all GameObjects with a certain name in a Scene and Apply them to a list? 3 Answers
Finding things with Raycasthit 1 Answer