- Home /
Magic Spell Types - SpellType.Fire/Water/Earth?
I literally don't know what I'm looking for and someone may have already answered this. But I want to make my own function:
public void UseMagicSpell() {}
and I want to use my own special form of variables instead of a string so, instead of:
public void UseMagicSpell(string spell) {}
OR
UseMagicSpell("FireSpell");
I want to use:
public void UseMagicSpell(SpellType spellType) {}
OR
UseMagicSpell(SpellType.Fire);
I'm not sure that's entirely what I'm wanting but I want to use this UseMagicSpell function so I don't have to do it line by line later on (whatever's in this UseMagicSpell function).
I hope you know what I'm talking about. I know I need a class though, like:
public class SpellType {}
but I don't know what I put inside. If I want SpellType.Fire, SpellType.Water, SpellType.Earth etc. what do I put inside of this class so I can do so later on?
Answer by Kiwasi · Nov 02, 2014 at 09:01 PM
You want an enum
public enum SpellType {Fire, Water, Air, Pokemon};
public SpellType mySpellType = SpellType.Water;
public void Cast (SpellType spellType){
if(spellType == SpellType.Water){
Debug.Log ("Splash");
}
}
Line two was just an example of how your could declare a variable of the SpellType. It actually does nothing useful. If you throw it into a script you will get the warning "Variable mySpellType is assigned its value but not used" or something similar.
Just for reference, an enum is basically an int, but with .value ins$$anonymous$$d of 1,2,3. So SpellType.Water would be 1, and so on. But since the whole point of the enum is to make things readable and consistent, so you wouldn't want to use it with direct values.
As you see in the code above, it says if spellType == SpellType.Water....well you could also use a switch:case set in the same manner, considering you may have a much longer list of spell types.
@kburkhart84 enums are ints by default, but they can also be defined as other types if it makes sense.
The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong.
@Bored$$anonymous$$ormon
That may be true, but I personally just use enums for organization and readable code, not for much else, so the only thing it makes sense to me for is as the the default ints. I can believe that other uses arise by using other data types, but I haven't found it yet :)