Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by OmryH · Aug 14, 2018 at 10:51 AM · enumdictionary

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Piyush_Pandey · Aug 14, 2018 at 11:35 AM 1
Share

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");
     }        
 
 }

avatar image OmryH Piyush_Pandey · Aug 14, 2018 at 12:16 PM 0
Share

This worked perfectly and much easier than my initial plan. Thank you so much!

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by OmryH · Aug 14, 2018 at 12:29 PM

Solved with @Piyush_Pandey 's help!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

90 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges