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 Inok · Sep 25, 2015 at 03:02 PM · script.optimization

How optimize script

I use switch to choose weapon shoot mechanic. Problem that i not want to check enum value each time when i do shot. I need to check only once (on awake for example) what option i need. Maybe switch not best for this situation, so i listen all possible suggestions.

 public enum Shoot_Type
   {
     BEAM, SHELL
   }
   public Shoot_Type Type_Of_Shoot;

 void Update()
   {
     if (Input.GetButtonDown("Fire"))
       switch (Type_Of_Shoot)
       {
         case Shoot_Type.BEAM:
           Beam_Mechanic();
         break;
         case Shoot_Type.SHELL:
           Shell_Mechanic();
         break;
       }
   }
Comment
Add comment
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

4 Replies

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

Answer by Inok · Sep 25, 2015 at 10:39 PM

Ok, looks like i found solution with help of Suddoha. As he suggest i use "action":

 public Action Shoot_Mechanic; // declare variable of type Action
 
         void Awake()
           {
             switch (Type_Of_Shoot)
             {
               case Shoot_Type.BEAM:
                 Shoot_Mechanic = Beam_Mechanic;
               break;
               case Shoot_Type.SHELL:
                 Shoot_Mechanic = Shell_Mechanic;
               break;
             }
         }
         
         void Update()
            {
              if (Input.GetButtonDown("Fire"))
                Shoot_Mechanic(); // call action that hold chosen in awake method 
            }
     
        void Beam_Mechanic()
       {
         do stuff 1;
       }
       void Shell_Mechanic()
       {
         do stuff 2;
       }
 



Comment
Add comment · Show 1 · 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
avatar image jmgek · Sep 28, 2015 at 11:09 PM 0
Share

Is this actually working for you? I am checking it out and I have not used action.

avatar image
2

Answer by Suddoha · Sep 25, 2015 at 05:14 PM

At the moment there's not much to optimize. The type will only be evaluated when the key is pressed down and there aren't many options. Switch-case and if statements are generally pretty performant.

However, there are other more elegant ways of dealing with such a situation, especially when the behaviour gets more complex and the code starts to get lengthy. You could have a look into delegates / events. There are already nice quick-to-use wrapper such as Action (or the generic version) and Func (which returns a value and can have a few parameters as well). These types can encapsulate different methods with the same signature.

The advantage would be a much shorter and cleaner code (1 line, if you do null-checks, 2 lines) in Update and additionally no comparison (which is what you've asked for), only once each time you switch the type of fire-mechanic and that can be handled in a different method which is not called frequently.

(A similar approach is based on a class-hierarchy or interface system, but that's probably too much for just a single method.)

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
avatar image
0

Answer by Wolf_Tech · Sep 26, 2015 at 05:50 PM

Hmm I would try setting another enum called whatever in start to shoot_type then check if it has changed something like this:

 public enum lastShoot_Type
 {
    BEAM, SHELL
 }
 public  lastShoot_Type lastShootType
 
 public enum Shoot_Type
    {
      BEAM, SHELL
    }
    public Shoot_Type Type_Of_Shoot;
 
 void Start()
 {
    lastShootType = Type_OfShoot;
 }
 
  void Update()
    {
     if(lastShootType != Type_OfShoot)
     {
         lastShootType = Type_OfShoot
     }
 
      if (Input.GetButtonDown("Fire"))
        if(lastShootType == BEAM)
        {
           Beam_Mechanic();
        }
        else if(lastShootType == SHELL
       {
            Shell_Mechanic();
        }
    }

I am not 100% if this will be better but this is how I would do it and if you want you can try it and see if it improves performance.

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
avatar image
0

Answer by jmgek · Sep 25, 2015 at 04:42 PM

 public bool isShootingBeam;
 //Update
 if(isShootingBeam)
     Beam_Mechanic();
 else
     Shell_Mechanic();

Is this what you want? If you don't know what a enum is it is just a "Int" Value that we assign words to.

Comment
Add comment · Show 3 · 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
avatar image Suddoha · Sep 25, 2015 at 05:00 PM 0
Share

That's basically the same with a boolean and only allows 2 different states, so he would need to fall back to the enum approach anyway, if he wanted more than 2 "shoot mechanics". :S

avatar image Inok · Sep 25, 2015 at 10:46 PM 0
Share

How i can not know what is enum if i use it in my example :)

avatar image jmgek Inok · Sep 25, 2015 at 11:07 PM 0
Share

lol sorry, a lot of people don't understand enums. Great posting your answer by the way : )

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

31 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

Related Questions

How Much code can be put in a start function? 1 Answer

Which is best for optimization 1 Answer

Optimal way to display a player's health with hearths? 1 Answer

One big script or lots of small ones? 0 Answers

Should I pool an object that only spawn every second? 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