I’d like to do something like this:
public enum MyEnum{
Null,
Choice1 = MyFirstFunction(Vector2 p),
Choice2 = MySecondFunction(Vector2 p),
}
void Start(){
MyFirstFunction(myPoint);
}
I’d like to do something like this:
public enum MyEnum{
Null,
Choice1 = MyFirstFunction(Vector2 p),
Choice2 = MySecondFunction(Vector2 p),
}
void Start(){
MyFirstFunction(myPoint);
}
You might could use a dictionary for something close to that, otherwise maybe add the delegate in an array at the location of the enum and don’t change it.
You can always write an extension method.
enum MyEnum {method1, method2}
public static MyExtension {
public static void Invoke (this MyEnum myEnum) {
switch (myEnum) {
case MyEnum.method1 :
// call method1
// and so on
}
}
}
Just because you can do this doesn’t mean you should.
What’s the point of this?
Can’t you just use methods, and delegates to store references? An enum is just to associate a name with a numeric value. Methods already HAVE a name.
Are you trying to create an enum to restrict which methods are allowed to be used?
enums are useful only because they give more type safety to their values. The values are constant. As such, delegates don’t make sense for your use. Delegate is to method what integer is to enum. Use methods.
static class Methods {
static void Choice1(Vector2 p) {Choice2(p);}
static void Choice2(Vector2 p) {Choice1(p);}
}
Giving us more info/context on what you’re really trying to achieve would help us give you better answers.
But like @fire7side said, you could use a dictionary:
public enum Choices { Choice1, Choice2, etc }
public Dictionary<Choices, Action> dict = new Dictionary<Choices, Action>();
dict[Choices.Choice1] = Method1;
dict[Choices.Choice2] = Method2;
etc
// later on
dict[Choices.Choice1]();
Dictionary has cleaner collection initializer syntax.
public Dictionary<Choices, Action> dict = new Dictionary<Choices, Action>{
{Choices.Choice1, Method1},
{Choices.Choice1, Method2}
};
I just wanted to say I think I bumped into this line of design and thinking and the reasoning for wanting this feature, it was I have started to use enumeration to encapsulate all atomic actions in my program (AI behaviours etc).
I also find myself writing this Enumerator switch type functions, writing the same words over and over to add one new thing (like a Frog or something).
I think the Dictionary suggestion combined with Lambdas was where I was looking until I decided to work with unity a bit more. Also the enumerators can have a parser feature.
Myself though I am trying to work with Unity a bit more than making my own Megalithic script
or use Func<> instead of Action<> if you wanna return a value
public enum EaseType
{
Linear,
SineIn, SineOut, SineInOut,
QuadIn, QuadOut, QuadInOut,
CubicIn, CubicOut, CubicInOut,
QuartIn, QuartOut, QuartInOut,
QuintIn, QuintOut, QuintInOut,
ExpoIn, ExpoOut, ExpoInOut,
CircIn, CircOut, CircInOut,
BackIn, BackOut, BackInOut,
ElasticIn, ElasticOut, ElasticInOut,
BounceIn, BounceOut, BounceInOut,
Spring
}
private readonly static Dictionary<EaseType, Func<float, float, float, float>> Types = new Dictionary<EaseType, Func<float, float, float, float>>
{
{EaseType.Linear, Mathf.Lerp},
{EaseType.SineIn, SineIn}, {EaseType.SineOut, SineOut}, {EaseType.SineInOut, SineInOut},
{EaseType.QuadIn, QuadIn}, {EaseType.QuadOut, QuadOut}, {EaseType.QuadInOut, QuadInOut},
{EaseType.CubicIn, CubicIn}, {EaseType.CubicOut, CubicOut}, {EaseType.CubicInOut, CubicInOut},
{EaseType.QuartIn, QuartIn}, {EaseType.QuartOut, QuartOut}, {EaseType.QuartInOut, QuartInOut},
{EaseType.QuintIn, QuintIn}, {EaseType.QuintOut, QuintOut}, {EaseType.QuintInOut, QuintInOut},
{EaseType.ExpoIn, ExpoIn}, {EaseType.ExpoOut, ExpoOut}, {EaseType.ExpoInOut, ExpoInOut},
{EaseType.CircIn, CircIn}, {EaseType.CircOut, CircOut}, {EaseType.CircInOut, CircInOut},
{EaseType.BackIn, BackIn}, {EaseType.BackOut, BackOut}, {EaseType.BackInOut, BackInOut},
{EaseType.ElasticIn, ElasticIn}, {EaseType.ElasticOut, ElasticOut}, {EaseType.ElasticInOut, ElasticInOut},
{EaseType.BounceIn, BounceIn}, {EaseType.BounceOut, BounceOut}, {EaseType.BounceInOut, BounceInOut},
{EaseType.Spring, Spring}
};
Thanks for the help and great answers everyone.
Thanks for Erik’s answer (not sure where it went) for reminding me Enums can only be used with numbers.
What I ended up doing is something like this:
public static enum TestEnum {
test1,
test2,
test3}
public static TestEnum testEnum;
public static delegate void Test();
public static Test[] test = { test1(), test2}
Then just did this test[(int) testEnum];