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 /
avatar image
0
Question by Superstition · Sep 21, 2019 at 05:06 PM · if-statementsalternative

an alternative to if with many multiple variable settings

Just something i was trying to think up an alternative, let me explain with some random, probably non-compilable psuedocode:

 enum AnimalColor
     {
         Red,
         White,
         Blue
     }
 
     AnimalColor Pig = AnimalColor.Red; //Set random color
     AnimalColor Cow = AnimalColor.White; //Set Random color
     int Barn = 0;
     string BarnName = "Shed"; //Arbritary name;
 
     void SortBasedOnColors()
     {
 
         //depending on colors of BOTH animals sort into different barn
         if (Pig == AnimalColor.Red && Cow == AnimalColor.Red){
             Barn = 1;
             BarnName = "ImWatchingSupergirlWhileCoding";
         }
 
         if (Pig == AnimalColor.Red && Cow == AnimalColor.White){
             Barn = 2;
             BarnName = "NobodyWillEverKnow";
         }
 
         if (Pig == AnimalColor.Red && Cow == AnimalColor.Blue){
             Barn = 3;
             BarnName = "YouCantProveAThing";
         }
 
         if (Pig == AnimalColor.White && Cow == AnimalColor.Red){
             Barn = 4;
             BarnName = "Wednesday";
         }
 //etc... etc... etc..
     }

So as you can see, depending on how many are in your selection base, in this example AnimalColor enum, that is a but load of if/elseif statements.

  • What would be a better possible alternative, so one wouldn't need to put every possibility in a if?

  • What would you do if there was one more animal?

The complexity would just increase with each added color or animal leading to a huge set of if's. It would probably just to restriction the whole mess, but I'm curious what others have thought of.

Have fun. =)

Edit: Edited for changes to complexity of the problem, comments on awesome int answer below explain added changes.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Captain_Pineapple · Sep 21, 2019 at 05:37 PM

Hey there,

how about this:

 int count = System.Enum.GetNames(AnimalColor).Length;
 Barn = (int)Pig * count + (int)Cow;

you can expand this as far as you want or could even use this in some kind of loop to be independent of fixed amounts of animal types:

         AnimalColor[] allAnimals;
         int count = System.Enum.GetNames(AnimalColor).Length;
         int factor = 1;
         int Barn = 0;
         for (int i=0;i<allAnimals.Length;i++)
         {
             Barn += factor * (int)allAnimals[i];
             factor *= count;
         }

Not tested of cause, but makes sense in my head. Let me know if it worked.


EDIT: As @Bunny83 pointed out correctly as usual, my code had a fault since i added an offset of +1 to each cast animaltype. while it would still work it wouldn't be optimal. I corrected the code as proposed by Bunny.

Comment
Add comment · Show 8 · 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 Superstition · Sep 21, 2019 at 05:59 PM 0
Share

I see what you did there, increment the barn for each animal, that would work in that situation. But what about if barn isn't a int? but a string, and you want to make it some random thing like if you have a red cow and brown pig you wanted to name the barn Wednesday? In the case of a int or byte you can do marching squares or something like that. And I suppose one could expand that to the infinite. But god what a mess it would be. Like the poor soul that figured out the first marching cubes table that everyone uses forever. Right? Your solution kinda re$$anonymous$$ds me of turning a 2d array into a 1d array. Which may bring something interesting to $$anonymous$$d... possibly a array table the index being an animal, and the value being the answer. $$anonymous$$ight even be visually interesting in a custom inspector...

avatar image Captain_Pineapple Superstition · Sep 21, 2019 at 06:23 PM 1
Share

if Barn is not a int then just use an array of type whateverbarnis (like string or to be generic: object) and use the integer that my codesnippet generates as an index to access the n'th element of the array. It wont get simpler than that.

avatar image Superstition Captain_Pineapple · Sep 21, 2019 at 07:46 PM 0
Share

I think you might be right. Thank you for going through the motions with me. =)

avatar image Bunny83 Superstition · Sep 21, 2019 at 06:24 PM 0
Share

Not really this does not flatten a 2d array into a 1d array. It flattens an n dimensional array into a 1d array. What you just added just makes no sense. You want to specify an arbitrary string with a certain combination but you haven't given any reasonable pattern. So it can not be simplified. Do you even have the slightest idea how the complexity of x^y grows? If you have 4 colors and 4 animals you will have 256 different combinations (==4^4). However if you have 10 colors and 10 animals you will have 10,000,000,000 combinations (10^10). It's completely unclear what you want to do here.

avatar image Superstition Bunny83 · Sep 21, 2019 at 07:41 PM 0
Share

There isn't a pattern. That's kinda my point. The complexity get stupid high stupid quick, so how can i keep it organized in a way better then having a if statement for every combination. If i got a red cow and a purple pig i WANT the answer to be barn named cheese. But what is the best way to keep it all in a organized mess other then a zillion if statements. Does any of that make sense?

Show more comments
avatar image Bunny83 · Sep 21, 2019 at 06:08 PM 1
Share

Why do you have a "+1" on each animal? That would offset the smallest possible result up the the sum of all factors. For two animals that would be 4 nad for 3 that would be 11. There's a good reason we have everything zero based ^^. If you want to start the Barn number at 1, just offset that by one.

 int factor = 1;
 int Barn = 1;
 for (int i=0;i<allAnimals.Length;i++)
 {
     Barn += factor * ((int)allAnimals[i]);
     factor *= count;
 }
avatar image Captain_Pineapple Bunny83 · Sep 21, 2019 at 06:20 PM 0
Share

yeah you are right, in my head there was something about having a few combinations accumulating on 0 if i don't offset that but yeah, it should be without offset. I even f***ing edited afterwards to add the damn offsets :D

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

113 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 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 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 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

Object doesn't move 2 Answers

Tips on how to handle multiple if statemens 1 Answer

Alternative to Team Lisence? 0 Answers

Different Explosion for Different Collisions 3 Answers

Problem with Counting Collisions 2 Answers


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