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
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@ronronmx: in that case you'd just do
– Eric5h5_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!
– ronronmx