- Home /
How can I use an enum as parameter to run a method inside a different method?
So I'm working on a Shoot() method that takes several float params (speedOfShot, Bursts, etc.). I want to create an option for the bullet to have an optional FollowMode, where it follows the player in certain ways. For that I created the enum:
namespace UnityEngine
{
public enum FollowMode
{
Line,
ZigZag
}
}
I also created the Attribute:
[AttributeUsage(AttributeTargets.Method,Inherited = true, AllowMultiple = false)] public class CallMethodAttribute : Attribute { private readonly FollowMode mFollowMode;
public CallMethodAttribute(FollowMode FollowMode)
{
mFollowMode = FollowMode;
}
public FollowMode FollowMode
{
get { return mFollowMode; }
}
}
and the implementation: public class FollowModeImplementation : MonoBehaviour {
[CallMethod(FollowMode.Line)]
public static void Line()
{
Debug.Log("In Line");
//Do something
}
[CallMethod(FollowMode.ZigZag)]
public static void ZigZag()
{
Debug.Log("In ZigZag");
//Do something
}
}
What I'm trying to accomplish is when I put in the parameters into the call of Shoot(params), once I get to FollowMode I'll have the option to choose between them (like in ForceMode in RigidBody) and that in Shoot(params) a function will run according to that selected FollowMode. I've looked up some ways and they all seem to refer towards a dictionary, but I can't manage to create such a dictionary that takes a FollowMode key and a method value, or even an int key and a method value.
I might be going down the completely wrong way, any help would be appreciated!
You started off great with enum. I would suggest you to use it for setting the state of the player which in a way you are doing already. I have one suggestion for you. Use finite state machine design pattern for this as you will have lots of other kind of movements for your player. I wrote one simple example for your case
This state class will only store the state of the game/player/ etc.
class GameState
{
public Follow$$anonymous$$ode Follow$$anonymous$$ode
{
get { return playerCurrentFollow$$anonymous$$ode; }
}
private Follow$$anonymous$$ode playerCurrentFollow$$anonymous$$ode;
}
This is what you might like to start with for the player movement:-
class Player$$anonymous$$over
{
public void $$anonymous$$ovePlayer(Follow$$anonymous$$ode follow$$anonymous$$ode)
{
switch (follow$$anonymous$$ode)
{
case Follow$$anonymous$$ode.Line:
{
//player follows in straight line
break;
}
case Follow$$anonymous$$ode.ZigZag:
{
//player follows zigzag
break;
}
default:
break;
}
}
void PlayerFollowLine()
{
Debug.Log("inside follow line");
}
void PlayerZigZag()
{
Debug.Log("inside zig zag");
}
}
This worked perfectly and much easier than my initial plan. Thank you so much!
Your answer
Follow this Question
Related Questions
Unity enum map - how to make it more efficient/clean? Linear line complexity. Best practices. 1 Answer
Converting bunch of if else statements to an efficient data structure 5 Answers
Hashtable(or generic dictionary) vs Enum - when do I use which? 1 Answer
How to make a certain kind 2D Dictionary? 1 Answer
How to use EnumPopup 1 Answer