- Home /
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
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;
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
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!
@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.
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!
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.
Your answer
Follow this Question
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