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.
void TradeItems()function?