Access an int in a list which works together with an items list.

Updated on March 5, 2020 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
5 on January 10, 2016

So the above question might sound kinda weird so let me go more in depth.
I’ve got these two lists:

 public List itemsInInventory = new List();
 public List stack = new List();
 

Now the stack is only called when need to (which is most of the times for this part of the inventory.)
But I don’t know how I can access the right int in this list that works together with the right item so:
itemsInInventory[0] would work together with stack[0] and so on.

Now I got this pickup script on the items to get picked up:

 //The part that handles everything that is stackable.
 else if (item.stackable && Input.GetKeyDown(KeyCode.E) && canPickUp)
 {
 inv.item = GetComponent();
 Debug.Log("Picked up item!");
 CheckIfInInv();
 }
 }
 void CheckIfInInv()
 {
 if(inv.itemsInInventory.Any((myObject) => myObject.name == item.itemName))
 {
 //HERE IT NEEDS TO ADD A STACK
 transform.SetParent(useableItem);
 this.gameObject.SetActive(false);
 this.gameObject.transform.localPosition = new Vector3(0, 0, 0);
 pickUp.enabled = false;
 pickUpText.SetActive(false);
 }
 else
 {
 transform.SetParent(useableItem);
 this.gameObject.SetActive(false);
 inv.itemsInInventory.Add(this.gameObject);
 inv.stack.Add(inv.stack.Count + 1);
 setImageToSlot = inv.itemsInInventory.Count;
 inv.slots[setImageToSlot - 1].image.overrideSprite = item.itemImage;
 pickUpText.SetActive(false);
 }
 }
 

So where I set in caps (sorry) where it needs to add one to the right stack in the list. according to the item which it gets set to. (The item already exists in the list of items when a stack has to be added) Does anyone know how I can access the right stack according to the right item in the list?

  • Liked by
Reply
3 on January 10, 2016

I think it would save you a lot of headache if you’d use a dictionary.
http://www.dotnetperls.com/dictionary 
 
In your case you would want something like this:

Dictionary<Item, int> Inventory = new Dictionart<Item, int>();
 
Item MyItem = new Item();
Item MyItem2 = new Item();
int ItemCount = 12;
int Item2Count = 5;
 
Inventory[MyItem] = ItemCount;
Inventory[MyItem2] = Item2Count;
 
Console.WriteLine(Inventory[MyItem]); // Output = 12
Console.WriteLine(Inventory[MyItem2]); // Output = 5

Guru
on January 12, 2016

So I was trying to get into this but now I can’t find a way on how to check the name of the pickup to be the same as an item

Guru
on January 12, 2016

There’s a property in the Dictionary called Keys. I think you want to look at that.

on March 5, 2020
There’s a property in the Dictionary called Keys. I think you want to look at that.From Dion Dokter

 A Dictionary class is a data structure that represents a collection of keys and values pair of data. The key is identical in a key-value pair and it can have at most one value in the dictionary, but a value can be associated with many different keys.  The Dictionary is based on a hash table, that means it uses a hash lookup, which is a rather efficient algorithm to look up things, on the other hand, a list you have to go element by element until it finds the result from beginning to the result each time. Accessing a value by using its key is very fast, close to O(1), because the Dictionary class is implemented as a hash table. The speed of retrieval depends on the quality of the hashing algorithm of the type specified for TKey in the Dictionary object.

Show more replies
  • Liked by
Reply
Cancel
0 on January 10, 2016

It probably won’t help, but I wrote something that I think might be similar, just using 2 sets of booleans to point at an item to use it…

    public void ItemPoint(){
        if (Input.GetKeyDown(KeyCode.O) && pointItem1 == true)
        {
            pointItem1 = false;
            pointItem2 = true;
        }
        else if (Input.GetKeyDown(KeyCode.O) && pointItem2 == true)
        {
            pointItem2 = false;
            pointItem3 = true;
        }
        else if (Input.GetKeyDown(KeyCode.O) && pointItem3 == true)
        {
            pointItem3 = false;
            pointItem4 = true;
        }
        else if (Input.GetKeyDown(KeyCode.O) && pointItem4 == true)
        {
            pointItem4 = false;
            pointItem1 = true;
        }
    }

    public void ItemUse()
    {
        if (Input.GetKeyDown(KeyCode.I) && item1 > 0 && pointItem1 == true)
        {
            if (thudsound)
                AudioSource.PlayClipAtPoint(thudsound, transform.position);
            PHealth += 10;
            item1 -= 1;
        }
        else if (Input.GetKeyDown(KeyCode.I) && item2 > 0 && pointItem2 == true)
        {
            if (thudsound)
                AudioSource.PlayClipAtPoint(thudsound, transform.position);
            mUpgrade1 = true;
            item2 -= 1;
        }
        else if (Input.GetKeyDown(KeyCode.I) && item3 > 0 && pointItem3 == true)
        {
            if (thudsound)
                AudioSource.PlayClipAtPoint(thudsound, transform.position);
            regen = true;
            item3 -= 1;
        }
        else if (Input.GetKeyDown(KeyCode.I) && item4 > 0 && pointItem4 == true)
        {
            item4 -= 1;
        }
    }
}

  • Liked by
Reply
Cancel