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/...)?
tonitoni1998 Offline
Member

Posts: 163
Threads: 54
Joined: Oct 2012
Reputation: 1
#1
does a case (switch statement) has to be "case 1/2/3/...)?

i tried to change my if statement into a switch statement (just to learn how it works)

if statement:
if(GetPlayerLampOil() == 0.0f)
    {
        SetMessage("map2_messages", "OilQuestDone1", 5.0f);
    }
    else if(GetPlayerLampOil() > 0.0f && GetPlayerLampOil() <= 5.0f)
    {
        SetMessage("map2_messages", "OilQuestDone2", 5.0f);
    }
    else if(GetPlayerLampOil() > 5.0f && GetPlayerLampOil() < 12.5f)
    {
        SetMessage("map2_messages", "OilQuestDone3", 5.0f);
    }
    else if(GetPlayerLampOil() == 12.5f)
    {
        SetMessage("map2_messages", "OilQuestDone4", 5.0f);
    }

switch statement:
switch (GetPlayerLampOil)
{
    case 0.0f:
       SetMessage("map2_messages", "OilQuestDone1", 5.0f);
    break;
    
    case > 0.0f && <= 5.0f:
       SetMessage("map2_messages", "OilQuestDone2", 5.0f);
    break;
    
    case > 5.0f && < 12.5f:
       SetMessage("map2_messages", "OilQuestDone3", 5.0f);
    break;
    
    case == 12.5f:
        SetMessage("map2_messages", "OilQuestDone4", 5.0f);
    break;
}

but i always get errors. so does a case has to end with 1 or 2 etc. ?

When you are looking for someone, to do the scripting for your Custom Story, ask me!
02-21-2013, 06:01 PM
Find
palistov Offline
Posting Freak

Posts: 1,208
Threads: 67
Joined: Mar 2011
Reputation: 57
#2
RE: does a case (switch statement) has to be "case 1/2/3/...)?

I'm pretty sure the case label has to be an integer value. You can probably use characters, such as case 'x', but a character is really an integer value anyways, it is just displayed differently.

For more complex logic like what you described in the second code box, you'll want to use if-else statements. They can get a little sore on the eyes, reading through a bunch of if-elses but they get the job done.

02-21-2013, 09:05 PM
Find
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
tonitoni1998 Offline
Member

Posts: 163
Threads: 54
Joined: Oct 2012
Reputation: 1
#4
RE: does a case (switch statement) has to be "case 1/2/3/...)?

Thanks for your work Big Grin
But im a beginner at coding and to be honest i did not understand everything :/ what are buckets? I not really understood the arrays yet, and some other stuff confuses me Tongue so i guess ill first go with the if statement and when i got better with the coding i will look for this again. But Thanks anyway Big Grin

When you are looking for someone, to do the scripting for your Custom Story, ask me!
02-22-2013, 02:50 PM
Find
darksky Offline
Member

Posts: 52
Threads: 8
Joined: Nov 2012
Reputation: 2
#5
RE: does a case (switch statement) has to be "case 1/2/3/...)?

Quote:so to tackle this let's first picture each "range" as a bucket:

tonitoni1998, that just means you split all possible oil values into different sets, and name the sets buckets Smile

and the biggest value of each set is used in the array
(This post was last modified: 02-22-2013, 03:05 PM by darksky.)
02-22-2013, 03:04 PM
Find
tonitoni1998 Offline
Member

Posts: 163
Threads: 54
Joined: Oct 2012
Reputation: 1
#6
RE: does a case (switch statement) has to be "case 1/2/3/...)?

and how do arays work? Tongue i tried to understand it with the wiki but i didnt realy get it Huh has someone an explenation for dumb ones?

When you are looking for someone, to do the scripting for your Custom Story, ask me!
02-22-2013, 03:17 PM
Find
Apjjm Offline
Is easy to say

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

An array is just a collection of values. So you may ask:
  • How do i make an array?
  • How do i get items from an array?
The first part is answered by example - from the code earlier:
float[] oilTargets = {0.0f,5.0f,12.5f};
That line says make me a float array called "oilTargets" that initially contains 3 elements (the numbers in the brackets).

To be able to get items that were put in the array you can use the square brackets to "index" the array:
oilTargets[0]
The above expression says pick the element at index 0 (the first element - computers typically start counting from 0). We could also choose element 1 or element 2 in a similar manner. Note that if our array has N items (length=N) then the last accessible index is N-1 (e.g. if our array has 4 items we can only access 0,1,2,3 indexes).

Arrays become useful when as we don't have to hardcode the number in the square brackets in - we can use a variable there instead:
for(i=0; i<3; i+=1;)
  AddDebugMessage("Entry " + i + " is " + oilTargets[i],false);
The above code loops through the first 3 elements in the array and prints out the value of that element (The variable i takes the values 0,1,2). This loop and look at elements stuff is really handy for avoiding code repetition.

This was very much a superficial introduction to arrays to give some intuition into what is going on in the code i wrote.
02-22-2013, 05:50 PM
Find
tonitoni1998 Offline
Member

Posts: 163
Threads: 54
Joined: Oct 2012
Reputation: 1
#8
RE: does a case (switch statement) has to be "case 1/2/3/...)?

ah man thanks alot!! i think i got it now. so an array is some kind of container where you can store values and call them? lets see if i can use that somewhere Tongue by the way, i took a look on your katamari mod Big Grin looks pretty fun Smile

When you are looking for someone, to do the scripting for your Custom Story, ask me!
02-22-2013, 06:05 PM
Find




Users browsing this thread: 1 Guest(s)