1

So I'm having a hard time understanding how multiple arrays and loops work. For example, in a game you have the trading functionalities and you create a method like this:

public void TradeItems(string[] giveItemKey, int[] giveAmount, string[] takeItemKey, int[] takeAmount)
{

}

You can call for the method to trade fewer items for more or vice versa. Example, you give 3 Double daggers & 4 Boots for 2 Armor.

TradeItems(new string[] { "Double Dagger", "Boots" }, new int[] { 3,4}, new string[] {"Armor" }, new int[] {2 });

So based on that method, how would you go about it? Would it be something like this?

public void TradeItems(string[] giveItemKey, int[] giveAmount, string[] takeItemKey, int[] takeAmount)
{
  if (HasItemsToTrade())
    {
        for (int i = 0; i < giveItemKey.Length; i++)
        {
            RemoveItems(giveItemKey[i], giveAmount[i]);
        }
        for (int i = 0; i < takeItemKey.Length; i++)
        {
            AddItems(takeItemKey[i], takeAmount[i]);
        }
    }
    else
    {
        Debug.Log("You don't have required items to trade");
    }

}

Also note that the giveItemKey array and takeItemKey array could have, equal or unequal lengths etc.

I appreciate any help and knowledge i can get about these.

2
  • Are you finding a better solution for void TradeItems() function? Commented Jul 10, 2020 at 5:27
  • yeah like perhaps a much cleaner or easier structure Commented Jul 10, 2020 at 14:25

1 Answer 1

1

In general I would rather use a Dictionary instead of two separate collections of items that eventually match in sizes..

public void TradeItems(Dictionary<string, int> give, Dictionary<string int> take)
{
    if (HasItemsToTrade(give))
    {
        foreach(var givePair in give) 
        {
            RemoveItems(givePair.Key, givePair.Value);
        }

        foreach(var takePair in take)
        {
            AddItems(takePair.Key, takePair.Value);
        }
    }
    else
    {
        Debug.Log("You don't have required items to trade");
    }
}

So your HasItmesToTrade would probably iterate over the entries in give and check if they are in your inventory like e.g.

private bool HasItemsToTrade(Dictionary<string, int> give)
{
    foreach(var kvp in give)
    {
        if(!inventory.ContainsKey(kvp.Key)) return false;

        if(inventory[kvp.Key] < kvp.Value) return false;
    }

    return true;
}

So you would rather call it like

TradeItems(new Dictionary<string, int> { {"Double Dagger", 3}, {"Boots", 4} }, new Dictionary<string, int>{ {"Armor", 2 } });
Sign up to request clarification or add additional context in comments.

1 Comment

Much cleaner and simpler code for sure. gonna see if it works well with my scriptableobject items

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.