- Home /
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.
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.
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.
Your answer
Follow this Question
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