Been trying to figure this one out but I’m not really sure how else I can reference the comparison…
When I click equip it runs the following function, passing in the items ID as a parameter. I want to then check through the list and see if an item with this ID already exists, and if not then add the item. The errors I get are:
Assets/_Scripts/Items/EquippedItems.cs(75,22): error CS1502: The best overloaded method match for `System.Collections.Generic.List.Contains(BaseItem)’ has some invalid arguments
Assets/_Scripts/Items/EquippedItems.cs(75,32): error CS1503: Argument #1' cannot convert int’ expression to type `BaseItem’
These are caused by the argument within the if statement, which is what I meant with regards to referencing the comparison. Can anybody please advise?
if (equippedItems.Contains (new BaseItem (itemToAdd.itemID, itemToAdd.itemName, itemToAdd.itemDescription, itemToAdd.itemType, itemToAdd.isEquipped = true)))
You’re doing a Contains check, but creating a new item. Of course it isn’t going to exist. You’re basically asking it to check if this brand new item you are creating already exist in the list.
BaseItem itemToAdd = inventory.GetItemByID (id);
if (equippedItems.Contains (itemToAdd))
{
//Item is already in the list.
Assuming all the other parts are working as intended, this should fix it for you.
Thanks for that, made me feel like an idiot yet again Brathnann xD But I appreciate it as always! Unfortunately though, it still doesn’t work. The item gets added more than once the same as it did before. Everything else is working as it should
Haha…We all make mistakes, so I’m not trying to make you feel like an idiot.
Oh, I see the issue. Same thing. When you add to equipement, do
equippedItems.Add(itemToAdd);
Right now if you find the inventory item, if it doesn’t exist in the equipment, you’re creating a new one and then adding it into the equipment. But you actually want to add the found item. Otherwise it doesn’t match when you look for it again.
Ahh brilliant. I wasn’t aware I could do that with adding new items. Will cut some parts of my code down quite a bit so thank for that. I thought a new item’s parameters had to be declared using the new keyword
If you are creating a new item, you will use new. But what you’re actually doing here is taking the item from one list and adding it to another.
A good way to think of it is when you pull something out of one box, do you want to put that item in a second box(thus adding the found item) or do you want to make a new item that looks like the item you got and put that in the second box(thus using new).
In the first case you have one item being referenced by two list. In the second case you have two items each being referenced by a different list.