Multiple Parameters

I was trying out some things after completing the course, and one problem I have is having multiple parameters for completing the quest. Needing three of the same item to complete it.

If I have only one item in my bag the quest can still be completed. I’m I using the parameters wrong or I missed something in the course?

Make the first parameter the item, and the second parameter the number of items

You’ll want a Predicate of HasItems for this (leave HasItem for single items)

Then in Inventory in Evaluate, if the predicate is HasItems, then check the item against parameter[0] and the quantity against parameter[1]

I’m not sure how to make this work, the parameter is a string and I was trying to add GetNumberInSlot for the number of the item but it’s an int. Sorry, I don’t know how I can pass that in.

There’s a handy feature in C# to handle this:

int.TryParse

if(int.TryParse(parameter[1], out quantity)
{
    Debug.Log($"quantity = {quantity}");
}

I hadn’t thought to split the predicates between HasItem and HasItems, that’s a good idea.

One other useful thing to add is in the Inventory script. There’s a bool method for HasItems(item), but the methods to determine the quantity of the item won’t give the expected result if the item isn’t stackable. If you don’t want to have to match your item checks to whether the item is stackable, this overload for HasItems is working for me:

/// <summary>
/// Is there an instance of the item in the inventory? If so, return slot.
/// </summary>
public bool HasItem(InventoryItem item, out int slot)
{
    for (int i = 0; i < slots.Length; i++)
    {
        if (object.ReferenceEquals(slots[i].item, item))
        {
            slot = i;
            return true;
        }
    }
    slot = -1;
    return false;
}

Then you can use this to check whether you have enough of whatever item:

int slot;
InventoryItem item = InventoryItem.GetFromID(parameterArray[0]);
if (HasItem(item, out slot))
{
    if(GetNumberInSlot(slot) < int.Parse(parameters[1]))
    {
        return false;
    }
}
else
{
    return false;
}
return true;

This topic was automatically closed after 21 hours. New replies are no longer allowed.

Privacy & Terms