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 LandonC · Mar 19, 2017 at 01:04 AM · optimizationswitchenumif-else

Optimising if else statements to switch and enums, or are there better methods?

Hello, I've hard coded 21 bools to check in my if else statements. Currently i want to optimize them with switch and enums.

To illustrate, here's an example:

 void Activate(Stat stat)
 {
         if(stat.box || stat.item)
         {
                   Debug.Log("take item");
                    return;
         }
         else
         {
                  if(stat.light)
                  {
                          stat.light.enabled;
                          return;
                   }
                  else if(stat.enemy)
                  {
                           stat.enemy.Appear();
                  }
         }
 }

It goes even lengthier complicated with the rest of the bools integrated. So here's what I plan to do:

 enum StatType {Box = 0, Item = 0, Enemy = 0, Light = 0};
 
 void Start()
 {
         ObjectStat();
 }
 
 StatType ObjectStat()
 {
          if(stat.box)
          {
                   ObjectStat.Box = 1;
          }
          if(stat.item)
          {
                   ObjectStat.Item = 1;
          }
          if(stat.light)
          {
                   ObjectStat.Light = 1;
          }
          if(stat.enemy)
          {
                   ObjectStat.Enemy = 1;
          }
 }
 
 void Activate()
 {
         switch(ObjectStat)
         {
                    case ObjectStat.Enemy:
                             if(ObjectStat.Light == 0)
                             { 
                                       stat.enemy.Appear();
                              }
                              break;
                    case ObjectStat.Box:
                            //Debug.Log("take the item");
                              break;
                    case ObjectStat.Item:
                            //Debug.Log("take the item");
                              break;
                    case ObjectStat.Light:
                              stat.light.enabled = true;
                              break;
         }
 }

Will this be more efficient? Or are there other methods that I'm not aware of? Any ideas or tips are much appreciated! :D

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

1 Reply

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

Answer by Commoble · Mar 19, 2017 at 01:07 AM

If you're doing what I think you're doing -- you have a few different object types (Enemy, Box, Item, and Light) and you want them to behave differently, but you want the same thing to activate all of these behaviors, then I suggest making each of these a subclass of some base class. Then you can treat all of these objects the same way, but define the specifics of what they do in their own class.

Comment
Add comment · Show 2 · 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 Bunny83 · Mar 19, 2017 at 01:54 AM 1
Share

Or maybe just use an interface which each different class implements. Base classes usually restrict you too much and couples the classes too much.

Though the bit we have seen of the actual setup is not really enough to pin point or recommend a concrete solution. It highly depends on the actual goal. That's the general problem with questions that only focus on a particular solution for a problem without mentioning the actual problem.

avatar image LandonC Bunny83 · Mar 23, 2017 at 02:38 AM 0
Share

I see, I'm only doing this to make my function less expensive. But I get the idea. I'm not quite sure how to use enums to be honest, at first I thought it is a set of variables that could be use individually but turns out it doesn't work that way. I think setting them as different classes would do a better job, thanks for all the advice!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Switch-case with enum 1 Answer

Operator '==' cannot be used! 2 Answers

Enum is thought to be a float? 2 Answers

Switch case using enum javascript 2 Answers

Enum and Variable with similar name resulting in error 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