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 ronronmx · Aug 18, 2011 at 06:51 AM · switchenumfor-loopforeach

How to set a value for each level without using a switch statement

I want to shorten my code a little but I'm unsure on how. Here's what I currently have:

 // Upgrade levels
     public enum UpgradeLevel
     {
         Level0 = 0,
         Level1,
         Level2,
         Level3,
         Level4,
         Level5,
         Level6,
         Level7,
         Level8,
         Level9,
         Level10,
         NumberOfUpgradeLevels,
     }
     private UpgradeLevel _camSwitchCountLevel;
     
     
 
 private int _camSwitchCount;
 public int CamSwitchCount  
     {
         get
         { 
             switch( _camSwitchCountLevel )
             {
                 case UpgradeLevel.Level0: _camSwitchCount = 1; break;
                 case UpgradeLevel.Level1: _camSwitchCount = 2; break;
                 case UpgradeLevel.Level2: _camSwitchCount = 3; break;
                 case UpgradeLevel.Level3: _camSwitchCount = 4; break;
                 case UpgradeLevel.Level4: _camSwitchCount = 5; break;
                 case UpgradeLevel.Level5: _camSwitchCount = 6; break;
                 case UpgradeLevel.Level6: _camSwitchCount = 7; break;
                 case UpgradeLevel.Level7: _camSwitchCount = 8; break;
                 case UpgradeLevel.Level8: _camSwitchCount = 9; break;
                 case UpgradeLevel.Level9: _camSwitchCount = 10; break;
                 case UpgradeLevel.Level10: _camSwitchCount = 11; break;
                 
                 default:
                     Debug.LogError( "The upgrade level has to be between Level1 and Level10. " +
                                     "Please make sure you assign the correct value!" );
                 break;
             }
             return _camSwitchCount; 
         }
         
     }

I'd like to turn this 'switch' statement into a simple for/foreach loop which would assign the correct value depending on the current level stored in _camSwitchCountLevel.

Thanks in advance for your time! Stephane

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

2 Replies

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

Answer by Eric5h5 · Aug 18, 2011 at 07:31 AM

I don't see any reason for using an enum. It's just a simple numerical progression, so an int is fine.

 int upgradeLevel = 0;
 ...
 _camSwitchCount = upgradeLevel + 1;
Comment
Add comment · Show 4 · 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 ronronmx · Aug 18, 2011 at 05:32 PM 0
Share

Eric5h5 and Bunny83, thank you both for your answers.

Eric5h5: I'm using an enum because of the following code I have - which I didn't include in my question - but since I'm rather new to enums, I'm wondering if I'm using them right:

 using UnityEngine;
 using System;
 
 
 public class BikeUpgrades {
 
     // Singleton access
     private static readonly BikeUpgrades _instance = new BikeUpgrades();
     
     
     // Upgrade levels
     public enum UpgradeLevel
     {
         Level0 = 0,
         Level1,
         Level2,
         Level3,
         Level4,
         Level5,
         Level6,
         Level7,
         Level8,
         Level9,
         Level10,
         NumberOfUpgradeLevels,
     }
     public UpgradeLevel upgradeLevel;
     private UpgradeLevel _camSwitchCountLevel;
 
 
 // Types of upgrades.
     public enum UpgradeType
     {
         UT_SwitchDuration = 0,
         UT_SwitchCount,
         UT_BoostPower,
         UT_TopBoostSpeed,
         UT_$$anonymous$$axBoost,
         UT_RechargeBoostWait,
         NumberOfUpgradeTypes,
     }
     private UpgradeType _upgradeType;
     
 // I A$$anonymous$$ USING BOTH ENU$$anonymous$$S FOR THIS ARRAY
 private int[,] _pointsNeededToUpgradeType = new int[(int)UpgradeType.NumberOfUpgradeTypes, 
                                                         (int)UpgradeLevel.NumberOfUpgradeLevels];
 // Constructor
 public BikeUpgrades () 
     {
         if( _instance != null )
         {
             //Debug.LogWarning ("Cannot have two instances of singleton.");
             return;
         }
         
         _pointsNeededToUpgradeType[0,0] = 200;


          // CONSTRUCTING THE ARRAY BY LOOPING THROUGH EACH ENU$$anonymous$$
         for( int type = 0; type < (int)UpgradeType.NumberOfUpgradeTypes - 1; type++ )
         {
             for( int level = 0; level < (int)UpgradeLevel.NumberOfUpgradeLevels - 1; level++ )
             {
                 _pointsNeededToUpgradeType[type, level] = level * 200 + 200;
             }
         }    


         _camSwitchCountLevel        = BikeUpgrades.UpgradeLevel.Level0;
         
     }
 }


The '_pointsNeededToUpgradeType[,] array uses both enums above to set the correct amount of points needed for each upgrade at each level. That's really the reason why I'm using an enum. Is this the correct way to achieve this?

Again, thanks for your time! Stephane

avatar image ronronmx · Aug 18, 2011 at 05:36 PM 0
Share

Actually it's a different progression depending on the upgrade type. For example, i have another upgrade called '_camSwitchDuration' which I increment by 0.5f for each level, and which starts at 2.0f. Doing "upgradeLevel + 0.5f;" wouldn't work in that case since it has to start at 2.0f, and not at the level number.

I hope I'm not being too confusing with my questions, and thanks again for your help!

avatar image Eric5h5 · Aug 18, 2011 at 06:25 PM 1
Share

@ronronmx: in that case you'd just do _camSwitchDuration = 2.0f + upgradeLevel * 0.5f; You can do what you like with the code, of course, but using enums for upgradeLevel seems confusing and unnecessary to me. Also the question is about making code shorter, so I would still recommend making upgradeLevel an int, since it would make the code shorter and (to me) make more sense.

avatar image ronronmx · Aug 18, 2011 at 07:19 PM 0
Share

Eric5h5, thanks a lot for your help and for the tips on enums usage in my case, I really appreciate it and you were very helpful!

avatar image
1

Answer by Bunny83 · Aug 18, 2011 at 11:23 AM

You can actually cast the enum's values to int:

 _camSwitchCount = (int)_camSwitchCountLevel + 1;

That works only when you keep your enum values linear.

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 ronronmx · Aug 18, 2011 at 07:20 PM 0
Share

Thanks for your help Bunny83.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Using Mathf.SmoothDamp() in a foreach loop 1 Answer

BCE0051 Error 1 Answer

Switch Case for Basic AI.... 1 Answer

How to use Enum? 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