Looks like I’m reaching too far for this one. I could use some help.
So I’m trying to create a system to store a list of what specific items (gold coins) have been collected in each level. That is, I need to be able say if GoldCoin number X has been collected from LevelName Y.
Rather than have a list within a list (my gut tells me that would be problematic) I’ve created a struct to store gold coin data, and made a list of said struct. The struct simply contains a string for a level name and an int that serves as a composite value to store all the boolean values of what coins are collected.
[Serializable]
public struct GoldCoinData
{
public string Level;
public int Coins;
public GoldCoinData(string level, int coinage)
{
Level = level;
Coins = coinage;
}
}
[..]
public List<GoldCoinData> CollectedGoldCoins = new List<GoldCoinData>();
I don’t recall ever working with structs before. Maybe I’ve already done something wrong.
Since I have a list of these goldGoinData’s, when I need a value I use a foreach loop to find the item in the list with the matching level name. I have honestly no idea how else to find such a value within the list.
public bool IsThisGoldCoinCollected(string level, int CoinNumber)
{
foreach (GoldCoinData gcd in CollectedGoldCoins)
{
if (gcd.Level == level)
{
if ((gcd.Coins & (2 ^ CoinNumber)) > 0)
return true;
}
}
return false;
}
So now I’m trying to create the function that will actually mark one of these coins as collected. But I get an error that reads “Cannot modify member of ‘gcd’ because it is a ‘foreach interation variable’” (line 8 in this section below)
public void SaveThisGoldCoin(string level, int CoinNumber)
{
if (!IsThisGoldCoinCollected(level, CoinNumber))
{
foreach(GoldCoinData gcd in CollectedGoldCoins)
{
if (gcd.Level == level)
{
gcd.Coins += 2 ^ CoinNumber;
return;
}
}
// no data for this level already exists.
CollectedGoldCoins.Add(new GoldCoinData(level, 2 ^ CoinNumber));
}
}
So, how do I get around this problem? If I already have a struct in my list for this particular level, how do I modify the coins value within that given struct?
Or should I be using a class instead of a struct? Or am I going about this completely the wrong way altogether?