Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by J_Troher · May 17, 2015 at 11:45 PM · hashtable

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image J_Troher · May 18, 2015 at 06:56 AM 0
Share

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.

avatar image redeemer · May 18, 2015 at 10:46 AM 1
Share

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.

avatar image J_Troher · May 19, 2015 at 05:43 AM 1
Share

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 :)

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image J_Troher · May 18, 2015 at 06:57 AM 0
Share

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. :/

avatar image abhi_360 · May 19, 2015 at 05:35 AM 1
Share

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

avatar image
0

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);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Manipulating JS hashtables on iOS platforms 1 Answer

One script on multiple game objects problem 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges