- Home /
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?
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.
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. :)
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
Follow this Question
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