does a case (switch statement) has to be "case 1/2/3/...)? - Printable Version +- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum) +-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html) +--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html) +---- Forum: Development Support (https://www.frictionalgames.com/forum/forum-39.html) +---- Thread: does a case (switch statement) has to be "case 1/2/3/...)? (/thread-20433.html) |
does a case (switch statement) has to be "case 1/2/3/...)? - tonitoni1998 - 02-21-2013 i tried to change my if statement into a switch statement (just to learn how it works) if statement: Code: if(GetPlayerLampOil() == 0.0f) switch statement: Code: switch (GetPlayerLampOil) but i always get errors. so does a case has to end with 1 or 2 etc. ? RE: does a case (switch statement) has to be "case 1/2/3/...)? - palistov - 02-21-2013 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/...)? - Apjjm - 02-22-2013 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!
RE: does a case (switch statement) has to be "case 1/2/3/...)? - tonitoni1998 - 02-22-2013 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 RE: does a case (switch statement) has to be "case 1/2/3/...)? - darksky - 02-22-2013 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 and the biggest value of each set is used in the array RE: does a case (switch statement) has to be "case 1/2/3/...)? - tonitoni1998 - 02-22-2013 and how do arays work? i tried to understand it with the wiki but i didnt realy get it has someone an explenation for dumb ones? RE: does a case (switch statement) has to be "case 1/2/3/...)? - Apjjm - 02-22-2013 An array is just a collection of values. So you may ask:
Code: float[] oilTargets = {0.0f,5.0f,12.5f}; To be able to get items that were put in the array you can use the square brackets to "index" the array: Code: oilTargets[0] 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: Code: for(i=0; i<3; i+=1;) 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/...)? - tonitoni1998 - 02-22-2013 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 |