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.
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:
We go into bucket 0 if there is no oil
Otherwise if there is <5 oil go into bucket 1
else <12.5 bucket 2
...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:
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:
//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.
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:
RE: does a case (switch statement) has to be "case 1/2/3/...)?
Thanks for your work
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 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
When you are looking for someone, to do the scripting for your Custom Story, ask me!
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.
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 by the way, i took a look on your katamari mod looks pretty fun
When you are looking for someone, to do the scripting for your Custom Story, ask me!