Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 topgun128 · Apr 02, 2017 at 07:00 PM · objectlistclass

How to store a class as a variable

Hey guys In my game characters can have spells. All spells inherit from a parent class called Spell. A character is given a list of spell instances. Each character can have different spells. When loading a character I want to create a new spell instance for each of the characters spells. Instead of saving some kind of spell id and doing the folowing:

 public void AddSpell (int spellReference)
 {
     if (spellReference == 0)
     {
         spells.Add(new Lightning());
     }
     else if (spellReference == 1)
     {
         spells.Add(new Poison());
     }
     // and so on
 }

I would like to have some kind of Spellbook that saves all the spells in a list and creates instances from that list. So the basic problem I have is, how do I save a class in a variable so that i can make instances from it. This is how it should look like:

 void DefineAllSpells ()
 {
     List<Object> spellList = new List<Object>();
 
     spellList.Add(Lightning);
     spellList.Add(Poison);
 }
 
 public void AddSpell (int spellReference)
 {
     spells.Add(new spellList[spellReference]());
 }
 

I realize I might be damaged by Python, but this would make many things easier for me. Do you guys have a solution?

Thank you in advance Lucas

A small side note. Initializing a Spell needs an argument. It is just one and all children of Spell take the same type of argument, but still, if your solution also works for a class with arguements, that would help.

Comment
Add comment · Show 2
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 RobAnthem · Apr 02, 2017 at 08:09 PM 1
Share

Umm I'm not sure what you are doing here, seems a bit unorthodox, but as you said, you are used to python. So here's a quick breakdown for you of how something like this is done in C#.

 [Serializable]
 public class Spell
 {
     public string spellName;
     public virtual void Cast(){}
 }

then we inherit

 public class Lightning : Spell
 {
     public int $$anonymous$$Damage;
     public int maxDamage;
 }

then we create our reference

 public class Player
 {
     public List<Spell> spellBook;
     public void AddSpell(Spell spell)
     {
         spellBook.Add(spell);
     }
 }

Now a better, more OOP way of doing this would be.

 public class AttackSpell : Spell
 {
     public int $$anonymous$$Damage;
     public int maxDamage;
     public GameObject spellPrefab;
 }

now we inherit again

 public ProjectileSpell : AttackSpell
 {
    public float spellSpeed;
 }

now for your pool of spells.

 [Serializable]
 public class SpellPack
 {
     public Dictionary<string, Spell> Spells;
     public Spell GetSpell(string spellName)
     {
        return Spells[spellName];
     }
 }


As far as the spell creation goes, you should find a way to do that in-editor, trying to do that in code means you are putting ACTUAL data in your code and that is against the principles of OOP.

avatar image topgun128 · Apr 02, 2017 at 09:24 PM 0
Share

Thank you for your quick answer. I see I have to think differently here. Your object oriented example seems very tidy to me and I like it very much. I will try it first thing tomorrow. Could you explain your concern about actual data in the code a little bit more? I didn't quite catch that. Thank you again for your time.

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Removing a class object from a list(.remove not working) C# 1 Answer

A node in a childnode? 1 Answer

How do I Create 5 Random Racers? 2 Answers

Can I make a list of hashtables or classes with pragma strict 2 Answers

Return a value for a class? 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