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
1
Question by unityExp · Jul 26, 2013 at 04:17 PM · c#hashtablecollectionsmultidimensional

C# Multidimensional Collection to Store and Retrieve Character Information

Hi guys,

I've been reading up a lot on Unity3D. Although I have a strong web development (PHP, MySQL, JS) background I chose to go with Unity C# instead of Javascript due to my dream project possibly requiring use of a third-party network suite that supports Unity3D.

I've been playing with Arrays/Collections for the last two days almost full-time and I'm up to speed with how to work with them for the most part. However, I'm a little uncertain about some concepts especially multidimensional collections. Its likely I'm at a disadvantage owing to my non-programmer background and being spoiled by the flexibility of PHP arrays.

Following is what I came up with. However, is that the right way to go about it? All I know is I want a string INDEX and the value needs to be of any type.

 Dictionary<string, Hashtable> myPlayer = new Dictionary<string, Hashtable>();
 
 myPlayer["DATA"] = new Hashtable();
 myPlayer["DATA"]["Health"] = 100;
 myPlayer["DATA"]["Mana"] = 100;
 myPlayer["DATA"]["Speed"] = 6;
 myPlayer["DATA"]["Gold"] = 70;
 
 myPlayer["CUSTOM"] = new Hashtable();
 myPlayer["CUSTOM"]["Gender"] = "Female";
 myPlayer["CUSTOM"]["Style"] = "Gamma_H03";
 myPlayer["CUSTOM"]["Face"] = 1;
 myPlayer["CUSTOM"]["Body"] = "Medium";
 myPlayer["CUSTOM"]["Suit"] = "Alpha_S01";
         
 myPlayer["ITEMS"] = new Hashtable();
 myPlayer["ITEMS"]["Potions"] = 5;
 myPlayer["ITEMS"]["Food"] = 10;
 myPlayer["ITEMS"]["Bolts"] = 20;
 myPlayer["ITEMS"]["Stones"] = 3;
         
 myPlayer["WEAPONS"] = new Hashtable();
 myPlayer["WEAPONS"]["Range"] = "Crossbow";
 myPlayer["WEAPONS"]["Surround"] = "Bomb";
 myPlayer["WEAPONS"]["Combat"] = "Knife";



What is the best practice to store and retrieve character information in a massive RPG with heavy character customizations and such like the above?

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

1 Reply

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

Answer by Loius · Jul 26, 2013 at 04:58 PM

Your approach will work.

However, I must say I'm a huge anti-fan of using strings and any-types together. While it works, it's impossible to make it maintain itself. YOU have to remember the exact strings required to access any specific item, and if YOU becomes YOUR TEAM or FUTURE YOU then you're not gonna have a great time (I used blender python... it suuuucks trying to relearn the strings!)

I highly, highly recommend you use custom classes wherever possible, for type safety, autocompletion, and compile-time rather than run-time error catching. I'd build a class for "An Item", for example, and "A Player" would have an array of "An Item"s.

If you still need anytypes inside your custom class, at least they're limited in scope.

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 unityExp · Jul 30, 2013 at 09:15 PM 1
Share

Thanks! Waited it out to see if i get any other inputs, but this is fine for now. I'll adapt to better practices as I get better. I'll definitely give creating custom classes a more detailed look as I learn. :)

avatar image ATMEthan · Jul 31, 2013 at 01:55 PM 1
Share

I second Loius' answer. Building a "container class"(A class that holds data) as I call them would be much more efficient than having hashtable that has all the data on a player.

I would suggest you make the change now unityExp.

Another benefit of doing it this way is it will most likely make your code readability increase. Now, if you are the other programmer on the project readability isn't that important. You wrote the code, you know what you wrote(hopefully). But if other programmers start co$$anonymous$$g on the project it could make it very difficult for them to understand what is going on.

One last thing... You said: "I'll adapt to better practices as I get better." You'll only truly get better if you try to adopt better practices, fail, and learn what went wrong so you can correct it next time.

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

18 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Overload methods of Hashtable using System and Photon namespaces 1 Answer

Dictionary suddenly loses its items 1 Answer

Hashset and ArrayList Not Updating Upon Button Click 2 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