I have a script that basically asks you to select a race from an enum. So lets say I pick orc, I would like that to created (or change) a secondary enum for sub-races (hill orc, etc…). I would prefer to have all sub-races on 1 enum variable so that intrinsic values for modifiers and such will be easily implemented.
So.
Is there a way to remove unwanted options (anything that isnt an orc sub-race) from enum but keep values of the orc sub-races relative to position?
It seems to me enums aren’t the way to go here. Instead, I would go with a more Object-Oriented approach.
Have an abstract class, Race which each character has as an attribute. This means a line of code in your Character class much like:
Race thisRace = new OrcRace();
This allows you to have inherited classes like OrcRace, HumanRace and whatever suits your fancy. You can then further, if required, inherit to create HillOrcRace, ForestOrcRace, etc.
Now, due to the magic of polymorphism, you can access each value of the specific race by just treating all of them as Races. Some code to illustrate:
using UnityEngine;
using System.Collections;
public abstract class Race {
public abstract int GetBaseStrength();
//same for all base stats
public abstract string GetRaceName();
public abstract Race[] GetSubRaces();
public abstract Faction GetFaction();
}
/*******************************************/
using UnityEngine;
using System.Collections;
public class OrcRace : Race {
//Skill is an enum here
private Skill[] raceSkills;
public OrcRace(){
raceSkills = new Skill[]{Skill.Slash, Skill.Growl};
}
public override int GetBaseStrength ()
{
return 5;
}
//this could be an enum instead of a string
public override string GetRaceName ()
{
return "Orc";
}
public override Skill[] GetRaceSpecificSkills ()
{
return raceSkills;
}
public override Faction GetFaction ()
{
//Faction is also an enum
return Faction.Horde;
}
}
/*******************************************/
using UnityEngine;
using System.Collections;
public class HumanRace : Race {
public override int GetBaseStrength ()
{
return 2;
}
//this could be an enum instead of a string
public override string GetRaceName ()
{
return "Human"
}
public override Skill[] GetRaceSpecificSkills ()
{
return null;
}
public override Faction GetFaction ()
{
//enum
return Faction.Alliance;
}
}
Notice I don’t inherit from MonoBehaviour, so these cannot be added to GameObjects. Race could inherit from MB, if you feel they should be on a GameObject.
I will admit that if you need to list these, you will have to either use reflection to get all classes extending Race, create an enum with the same values as the class names you want to have in your game or have a list of all possible Races stored somewhere and have a method in Race to give all possible subraces.
Hmm I’ve never used this kind of programming but my first guess would be no.
Seems this discussion is related to what you are trying to do:
Sounds like you MIGHT be able to create a list and then set up your new enumeration after removing items from the list? I capitalized “might” because I have no idea to be honest.
This sounds like a lot of work to me to use enumerations for this. Any reason why you don’t just use a number corresponding to their race selection and based on that number you use a specific range of numbers for the sub-races?
I think that would allow you to contain all your data easier, but once again I don’t know anything about enumerations so I could be dead wrong.
Edit: sorta interesting topic since I know nothing about enumerations. But I just want to add that using a number system would also allow you to store PlayerPrefs super easy for the race/sub-races/etc if you do plan to save all that stuff for multiple characters or whatever.
I tend to keep things overly simplified which keeps my logic very easy to understand but in many cases not as compact as it could be.
Personally for me though it seems like a lot of extra work to simply store 2 values, I would just have an int variable for both race and sub-race possibilities. I’m sure there could be disadvantages to that approach though.