Switch statement for loot table instead of if statements.
Hey. I have 3 items, with chances: A=30%, B=30%, C=40%. I am using Random.value or Random.range(a,a+b+c) and then using if statements to check which one is picked out.
var ranV = Random.value;
var a = 0.3; var b = 0.3; var c = 0.4;
if(ranV <= a) { A scenario }
if(ranV > b && <= a+b) { B scenario }
if(ranV > a+b && <= a+b+c) { C scenario }
And I was wondering if I can somehow make this work with switch statement instead of if? Is there any smart way to incorporate this system into a switch statement? Or is this way pretty much the only and the right way of doing these things? Thanks in advance.
A little into any decent intro to program$$anonymous$$g, you should see a cascading if: if(r<a) {} else if(r<a+b) {} else {}
.
If you haven't seen that trick, you're probably missing a bunch of other standard stuff. $$anonymous$$ight look at a decent textbook (don't recommend Unity stuff - it tend to be very uneven on technique.)
That makes sense, thanks. I will incorporate this into my code. I wonder why Fattie says not to though. I've read in a different post Fattie saying to use else-if only if you're experienced programmer, not a beginner. I think it is pretty straight-forward and wonder why not use it?
I dont think its a big deal in this case and many cases in general. But sometimes programmers can go crazy over the if else usage. For example: float calculateSurfaceArea(Shape shape) { if(shape.Type == TYPE_CIRCLE) { calculateCircleArea(shape); } else if(shape.Type == TYPE_TRIANGLE) { calculateTriangleArea(shape); } else if(shape.Type == TYPE_SQUARE) { calculateSquareArea(shape); } }
This should be avoided at all cost because its difficult to manage such code. This can be easily solved by using polymorphism where you can have Circle, Triangle, Square to be derived classes of a base class called Shape and they each can override a "calculateArea" function and have their custom implementation. But in your case if else should be used, so go ahead :)
Yes and thats why I said OP should use if else as you mentioned. I was just pointing out a scenario where it should be avoided as the OP was asking as to why fattie was against this usage of if else.
Answer by hulahoolgames · Jan 09, 2016 at 07:46 PM
You could do something like:
int random = Random.Range(0, 10);
switch(random) {
case 0:
case 1:
case 2:
A scenario;
break;
case 3:
case 4:
case 5:
B scenario;
break;
case 6:
case 7:
case 8:
case 9:
C scenario;
break;
default:
Default scenario;
break;
}
This way you can incorporate your logic into a switch statement, but if statements might be more cleaner to code.
sure, the OP should also master switch
control flow statements
Answer by Fattie · Jan 09, 2016 at 07:34 PM
What you're looking for is "breakaway" code -- which is an excellent way to work.
the only mistake you've made is forgetting the "return" statement. So it's like ..
if ( blah )
{
blah blah
return
}
In your example every passage such as ranV > b && <= a+b
MUST have a breakaway, a return, at the end.
Note that if you have to do "other stuff" after that sequence of choices, just go to all the choices in a separate subroutine.
So,
if(ranV <= a) { A scenario; return; }
if(ranV > b && <= a+b) { B scenario; return; }
if(ranV > a+b && <= a+b+c) { C scenario; return; }
why not just use if else, in case there is other stuff to do after these statements?
never use "else-if" in any language for any reason. it's not even well-defined.
Your answer

Follow this Question
Related Questions
Switching the room on button press. 2 Answers
[HELP] How to switch the mode of displacement ? 0 Answers
Cannot implicitly convert type `void' to `bool' 1 Answer
If statement is being ignored 1 Answer
If statement not starting 1 Answer