- Home /
Adding Multiple Keys/Values to a hashtable inside Foreach Loop
I am working on synchronization for my inventories/chests.
Apparently the easiest data I can transfer over photon RPC is a Hashtable. So Foreach item in my inventory, I am trying to create a temporary HT, and add ID and Amount Keys, with their value's. But it doesn't seem to like me adding two keys inside of a for loop.
foreach (Item _item in inventory) {
ExitGames.Client.Photon.Hashtable _itemInfo = new ExitGames.Client.Photon.Hashtable ();
_itemInfo.Add ("ID", _item.itemID);
_itemInfo.Add("Amount", _item.itemAmount);
photonView.RPC ("SendItemInfo", PhotonTargets.All, _itemInfo, photonView.viewID);
}
This want's to give me: InvalidOperationException: Collection was modified; enumeration operation may not execute.
Answer by redeemer · May 18, 2015 at 05:37 AM
I don't know if the Photon Hashtable warks as a regular one, but if that's the case, you don't have to add the ID and then the item, you add both at a time. I'd change this two lines
_itemInfo.Add ("ID", _item.itemID);
_itemInfo.Add("Amount", _item.itemAmount);
for
_itemInfo.Add(_item.itemID, _item.itemAmount);
Hope this helps
This is actually what I do, with the ID being the key and the value being the amount. But if I can do it the other way, I can add ID, amount, Quality. whatever I want really.
Since I don't know if you can use a custom Hastable (under my point of view would be the best option if allowed). Then I'd suggest you create your own string data structure to parse, something like:
string _ownData = _item.itemAmount + ";" + _item.itemQuantity + ";" + _item.itemQuality;
_itemInfo.Add(_item.itemID, _ownData);
Then, when you want to read the values, you can just split them like:
string[] _parsedOwnData = string.Split(";");
You only have to be sure you decide the order. In this example, you will have:
item amount in _parsedOwnData[0] item quantity in _parsedOwnData[1] item quality in _parsedOwnData[2]
This way you can add all the values you want in a simple string as long as you maintain the order when creating the string and reading it.
This is actually a very good idea. Idk why I didn't think about it. I can definitely add a string value to a photon hashtable. So this should work! I've already messed with splits and parse, for things like chat commands. So this should solve all my problems :)
Answer by hanger · May 18, 2015 at 05:38 AM
Possibly the RPC is modifying inventory. One way to fix this is to make a copy of inventory, then iterate over that copy.
Answer by abhi_360 · May 18, 2015 at 06:24 AM
During the loop your inventory is being changed indirectly
just try foreach (Item _item in inventory.ToList()) also add system.linq as ToList() exits in Linq
I did redesign quite a bit, so my database is no longer being affected by user's inventories. Which works great. But I am still sending the itemID and the amount as $$anonymous$$ey/Value with one hashtable. This works for anything I have, stacked or unstackable. I just wish there was a way to add a third value for quality of an item. :/
normally for any kind complex data storage i simply would create a new data structure like struct Inv{int itemid;string itemname,.....} then store it to a dictionary
Answer by pawan_unity · Sep 25, 2021 at 08:53 AM
ExitGames.Client.Photon.Hashtable props = null;
props = new ExitGames.Client.Photon.Hashtable
{
{"IsPlayerState",playerState},
{"GW_Count",GlobalConstant.GW_Count}
};
}
PhotonNetwork.LocalPlayer.SetCustomProperties(props);
Your answer
Follow this Question
Related Questions
Custom class and hashtable 1 Answer
Dictionary vs Hashtable 1 Answer
How to call value by key when key is an object? 1 Answer