Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
does a case (switch statement) has to be "case 1/2/3/...)?
Apjjm Offline
Is easy to say

Posts: 496
Threads: 18
Joined: Apr 2011
Reputation: 52
#3
RE: does a case (switch statement) has to be "case 1/2/3/...)?

For just a few cases - go with if-else statements. However, if you have a bunch of cases then there are ways you can transform your problem into something that can be tackled by switch/case (<10 cases) or some other means (tons of cases). Im going to write about how you would go about the latter two solutions as there is some more ground to cover here and it may help you or somebody else, even though it may turn out just sticking with if-else statements is better for this specific instance of this problem.
Spoiler below!

In the case of your oil problem we are dealing with a bunch of ranges (with no gaps between the ranges) - so to tackle this let's first picture each "range" as a bucket:
  1. We go into bucket 0 if there is no oil
  2. Otherwise if there is <5 oil go into bucket 1
  3. else <12.5 bucket 2
  4. ...and so on...

The buckets have integer values and identify the range - perfect for switch case stuff (and some nifty tricks too). All we need now is some way to represent the ranges and find out the appropriate bucket of some number. Thankfully, doing this is not too tricky - Firstly we shall define the ranges in a global array, so adding extra buckets is easy:
float[] oilRanges = { 0.0f, 5.0f, 12.5f };
The values in this array represent the (inclusive) upper bound of the bucket. Next we shall make a function that will work out which bucket (range index) a float value lands in:
int findOilRangeIndex(float value)
{

  //For each upper bound of the range of our buckets...
  for(int i=0; i<int(oilRanges.length()); i++)
    //If the value is less than this upper bound, then this is the correct bucket.
    if(value <= oilRanges[i]) return i;

  //No matching bucket - return -1 to indicate an error.
  return -1;
}
The above code will loop through upper bound in the array and determine which bucket the value lies in. It is worth noting here that this approach assumes the ranges have no gaps and the upper bounds are specified in ascending order in the global array.

With the tough part out the way...
void someFunction()
{
   int bucket = findOilRangeIndex(GetPlayerLampOil());
   switch(bucket)
    {
      case 0:
          //Some code for oil=0
          break;
      case 1:
          //Some code for 0<oil<=5
          break;
      case 2:
          //Some code for 5<oil<=12.5
          break;
      default:
          //Error case where the oil is outside of the buckets
    }
}

In the case of you example of just wanting to alter what the output of setmessage is there is a far more compact way of going about the problem. I notice your entries start as OilQuestDone1 (for oil=0) and go OilQuestDone2, OilQuestDone3... - you can simply display the correct message and avoid writing the whole switch case stuff by adding 1 to the bucket id and adding this number to the end of the entry:
void someFunction()
{
   int entry =  findOilRangeIndex(GetPlayerLampOil()) + 1;
   SetMessage("map2_messages", "OilQuestDone" + entry, 5.0f);
}
Which is far more compact when you get past two or three ranges.

Please not code provided here has been written in the forum editor and is untested. It should work but i could have made a typo at somepoint Wink.

(This post was last modified: 02-22-2013, 07:21 AM by Apjjm.)
02-22-2013, 07:13 AM
Find


Messages In This Thread
RE: does a case (switch statement) has to be "case 1/2/3/...)? - by Apjjm - 02-22-2013, 07:13 AM



Users browsing this thread: 1 Guest(s)