Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by quantumface · Jan 09, 2016 at 06:47 PM · switchif statement

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.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Jan 10, 2016 at 05:37 PM 0
Share

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.)

avatar image quantumface Owen-Reynolds · Jan 11, 2016 at 04:40 PM 0
Share

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?

avatar image hulahoolgames quantumface · Jan 11, 2016 at 04:59 PM 0
Share

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 :)

Show more comments
avatar image hulahoolgames Owen-Reynolds · Jan 11, 2016 at 09:38 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Jan 09, 2016 at 07:59 PM 0
Share

sure, the OP should also master switch control flow statements

avatar image quantumface · Jan 10, 2016 at 09:22 AM 0
Share

I guess if's are the only good way then. Thanks.

avatar image
1

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; }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hulahoolgames · Jan 09, 2016 at 07:48 PM 0
Share

why not just use if else, in case there is other stuff to do after these statements?

avatar image Fattie hulahoolgames · Jan 09, 2016 at 07:59 PM 0
Share

never use "else-if" in any language for any reason. it's not even well-defined.

avatar image quantumface · Jan 10, 2016 at 09:24 AM 0
Share

Thanks, I'll be sure to use return from now on.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

33 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges