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
2
Question by jtsmith1287 · Oct 16, 2014 at 05:23 PM · c#dataskillsinterfaces

C# -- Populate dictionary automatically (not manually, yuck)

The title is vague cause I'm not 100% I'm doing this in a decent way. So I'm creating modular ability system in my game where the player can swap out any of their 4 skills/abilities from a list of ... a lot. Here's roughly what we're doing:

 // One of many small classes in ability file
 
 class SpecificAbility : IAbility{
     
     // ability stuff like an Execute() method for starting specific ability functions.
 }    
 
 

// Different file

 class FooStuff {
     
     public Dictionary<string, IAbility> AbilityDict = new Dictionary<string, IAbility>();
 }
 
     void UpdateAbilityDict(){
     
         AbilityDict.Add("SpecificAbility", new SpecificAbility());
     }


So, this works, and then we can easily toss abilities around allowing for the player to modify what they're using. Cool, great. However, we currently have 16 abilities, and are planning for up to 80. Now, I do NOT want to type AbilityDict.Add() 80 times, and I'm sure I don't need to explain why this is just bad practice. I come from Python, so dynamically loading each ability into this dictionary is childsplay where I come from. C#? I've got no idea, lol. I'm not even sure what the tactic is even called to do this so google as been less than helpful. Halp! Thanks guys. :)

Comment
Add comment · Show 7
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 Habitablaba · Oct 16, 2014 at 05:27 PM 0
Share

Are you adding to this dictionary during game play, or are you setting it up once at the start, then just reading from it for the rest of the game?

avatar image jtsmith1287 · Oct 16, 2014 at 05:37 PM 0
Share

Just to be safe, I'd say both. I'd like to allow for in game updating if needed. $$anonymous$$ainly it's a one time load though at the beginning.

avatar image Anxo · Oct 16, 2014 at 06:08 PM 0
Share

would the dictionary string entry always be the name of the class?

avatar image jtsmith1287 · Oct 16, 2014 at 06:18 PM 0
Share

As of now I'm still debating that part. It could be the user facing string, or just used internally. It was initially class name because I was attempting to use reflection, but turns out I don't understand it all that well and am unsure of its limitations and performance costs.

For the sake of this answer let's just say it'll always be the class name. Thanks.

avatar image Landern · Oct 16, 2014 at 06:23 PM 0
Share

You should serialize and deserialize the objects, now the Dictionary type isn't serializable on the face, but there are plenty of examples on "Serializable Dictionary C#" on stackover flow and has been covered here. If you serialize to X$$anonymous$$L you can hand edit the xml(or json or whatever) and deserialize it from disc into your dictionary of whatever type, if the the player can some how manipulate the abilities(gain level or whatever) you can save(serialize it) out rinse repeat. When you deserialize, the construction/instantiation of objects is just part of the game. If you need this just for population, you can do the same thing obviously.

Show more comments

2 Replies

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

Answer by Bunny83 · Oct 17, 2014 at 01:14 AM

Your answer is Reflection ;)

This is one of the cases where it's ok to use it since there's no other way around that. If you're happy with using the classname as dictionary key that's quite easy. If you want to specify a seperate name you would have to add a custom Attribute to your class to give it a custom name.

Those two helper methods will give you either a list of all types in your current AppDomain or all classes which are assignable to a specified type which could be a base class or an interface.

 public static class ClassUtils
 {
     public static IEnumerable<System.Type> AllTypes()
     {
         var assemblies = System.AppDomain.CurrentDomain.GetAssemblies();
         foreach(var assembly in assemblies)
         {
             var types = assembly.GetTypes();
             foreach(var type in types)
             {
                 yield return type;
             }
         }
     }
     public static IEnumerable<System.Type> AllTypesDerivedFrom(System.Type aBaseType)
     {
         foreach(var T in AllTypes())
         {
             if(aBaseType.IsAssignableFrom(T) && T != aBaseType)
                 yield return T;
         }
     }
     public static T GetFirstAttribute<T>(this System.Type aType) where T : System.Attribute
     {
         var attributes = aType.GetCustomAttributes(typeof(T),false);
         if(attributes.Length == 0)
             return null;
         return (T)attributes[0];
     }
 }

Now you just need to define a custom attribute like this:

 public class CustomAbilityName : System.Attribute
 {
     public string customName;
     public CustomAbilityName(string aCustomName)
     {
         customName = aCustomName;
     }
 }

And this is how one of your child classes could look like:

 [CustomAbilityName("Fireball")]
 class SpecificAbility : IAbility{
     // ...
 } 

Your initialization of your dict would look like:

 var classes = ClassUtils.AllTypesDerivedFrom(typeof(IAbility));
 foreach(var T in classes)
 {
     IAbility inst = (IAbility)System.Activator.CreateInstance(T);
     string name = T.Name;
     var att = T.GetFirstAttribute<CustomAbilityName>();
     if (att != null)
         name = att.customName;
     AbilityDict.Add(name, inst);
 }

Note: using that attribute is pure optional. If no attribute specified it would use the classname.

Comment
Add comment · Show 5 · 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 jtsmith1287 · Oct 17, 2014 at 01:46 AM 0
Share

if inst is just an instance of the class, then having user facing names wouldn't be hard at all. I could simply add a .Name member to the base class and then have: AbilityDict.Add(inst.name, inst);

Thanks for this answer. I am unfamiliar with most of it, but I believe this is what I am looking for. Would you say my method of adding the custom name will work?

Also, I realized after posting this that it would be better for me to store references to the class so that I can create new instances as needed. Is this possible at all, and if so, is it possible using the logic you've provided?

avatar image Bunny83 · Oct 17, 2014 at 02:02 AM 0
Share

Just added the attribute stuff as well since i'm not at home this weekend ;)

Sure, you can use a list / dict of System.Type objects as well and use the Activator to create an instance when you need it. But creating them at start probably won't hurt.

ps: In AllTypesDerivedFrom there was an important check missing

  T != aBaseType

otherwise it would return the base class / interface as well and you probably can't create an instance of that ;)

avatar image jtsmith1287 · Oct 17, 2014 at 02:12 AM 0
Share

Could I have the userFacingName member be static and access it via the Type, so I can still use a member to set the name and save the Type into the dict ins$$anonymous$$d of the name?

avatar image Habitablaba · Oct 17, 2014 at 02:18 AM 0
Share

This is absolutely the answer I wish I could have written. Thank goodness there are people who are good at reflection ;)

avatar image Bunny83 · Oct 17, 2014 at 02:41 AM 0
Share

@jtsmith1287: Yes you can, however that's a more hacky way ;) Since it's not clear that a static variable (which itself is unique to each class) is used by this initialization system. Attributes are exactly made for this. But no one will stop you from reading a static field ;)

this would be the change:

 // ...
 string name = T.Name;
 var nameField = T.GetField("Name", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
 if(nameField != null && nameField.FieldType == typeof(string))
     name = (string)nameField.GetValue(null);
 
 AbilityDict.Add(name, inst);
avatar image
1

Answer by ThePunisher · Oct 16, 2014 at 10:34 PM

Your answer here is XML serilization.

What you can do (once you understand how XML serialization works) is create your XML file that lists all your abilities and all the properties pertaining to them. Modify your ability class to have abilities include a ID (which can just come from the class name if need be, but I'm assuming in some cases you can have the same class serve the purpose of a fireball and an icebolt but the dmg type property will be different along with other properties).

 class SpecificAbility : IAbility
 {
   [XMLElement("AbilityID")]
   public string ID;
 // ability stuff like an Execute() method for starting specific ability functions.
 } 

Then read in (deserialize in this case)the XML as a container like the following:

 public class AbilityContainer
     {
         [XMLElement("Ability")]
         List<IAbility> Abilities = new List<IAbility>();
     }



Then you can just iterate through the list of abilities and add them to a dictionary with the ID as their key.

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 ThePunisher · Oct 16, 2014 at 10:57 PM 0
Share

Also, I saw your comment about lots of iteration and this will work better for what you need (if I understood correctly) because your iterations will consist of modifying an X$$anonymous$$L file to, for example, change the damage amount on an ability. Then (if you read the X$$anonymous$$L file in during runtime) reloading the xml/restarting the game ins$$anonymous$$d of having to rebuild/recompile, which results in faster iteration in my experience.

avatar image jtsmith1287 · Oct 17, 2014 at 01:31 AM 0
Share

I just want to say that this is solid and I dig it. In my case, however, the abilities themselves don't contain many members. It's mostly logic, so X$$anonymous$$L won't save me a lot of time. It would basically just create an unnecessary layer of abstraction. Every ability will be pretty different, and the only shared member is the player invoking it. +1 though for the response. I'll certainly be checking back in on this answer for some of my other needs (which are similar). Thanks!

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

34 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 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

Flip over an object (smooth transition) 3 Answers

Best way to store and use small game data? 1 Answer

Syncing variables between two objects of same script 1 Answer


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