Search Unity

possible to do switch case in a loop?

Discussion in 'Scripting' started by Alex Cruba, Feb 8, 2013.

  1. Alex Cruba

    Alex Cruba

    Joined:
    Aug 16, 2011
    Posts:
    564
    I don't think I've to hardcode every number, could I do it automaticaly?

    Code (csharp):
    1. int random_value = Random.Range( 1, 4 );
    2.            
    3.             switch( random_value ) {
    4.            
    5.                 case 1:
    6.                 number = 1;
    7.                 break;
    8.                
    9.                 case 2:
    10.                 number = 2;
    11.                 break;
    12.                
    13.                 case 3:
    14.                 number = 3;
    15.                 break;
    16. }
     
    Last edited: Feb 8, 2013
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The point of switch/case is that you hardcode every item. If you don't want that, don't use switch/case. In your code, just do "number = Random.Range(1, 4);".

    --Eric
     
  3. Alex Cruba

    Alex Cruba

    Joined:
    Aug 16, 2011
    Posts:
    564
    Any other way when I need 100 different cases? lol
     
  4. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    6,208
    Create an array of delegates, and randomly pick one of them.
     
  5. Alex Cruba

    Alex Cruba

    Joined:
    Aug 16, 2011
    Posts:
    564
    The problem is I need the cases because they are used to call different pictures.
     
  6. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    6,208
    I believe it can be done without a giant switch. If you can give more information about what one of your case functions might look like, I could probably help further.
     
  7. Alex Cruba

    Alex Cruba

    Joined:
    Aug 16, 2011
    Posts:
    564
    I display random pics. The number is the style number. So 1 is style 1, 2 is style 2. That way I change pics. It is randomly done to have kind of a slotmachine effect. 100 pics should be shown per action, but in random selection. That's all.

    I was just interested int technical side, because I feel really uncomfortable with case switch not being possible to write code in one line, not possible to enbed a while or foreach...
     
    Last edited: Feb 9, 2013
  8. Mike L

    Mike L

    Joined:
    Sep 14, 2010
    Posts:
    1,035
    what your original code does is the same thing that eric posted........ except his cuts the need for the whole switch stuff..... why cant that work? the code is literally doing the exact same thing......
     
  9. Alex Cruba

    Alex Cruba

    Joined:
    Aug 16, 2011
    Posts:
    564
    Because number will get a random value out of 1-4. That way I get one pic out of 4. I need different 100 pics displayed one after the other but randomly. Just the order should be another each time. You know big bang theory intro? his one but randomly everytime.

     
    Last edited: Feb 9, 2013
  10. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    You need to give more info to get better help. Your sample code for example imply that you could have just done
    Code (csharp):
    1. int number = Random.Range( 1, 4 );
    and skip the whole random_number and switch step.

    So, are you trying to figure out filenames for your textures to load? Then just name them like "pic1.png", pic2.png",etc and do
    Code (csharp):
    1.  
    2. int number = Random.Range( 1, 4 );
    3. string selected_file = "pic" + number.ToString() + ".png";
    4.  
    If textures prefabs then ...
    Code (csharp):
    1.  
    2. public Texture2D[] textures; // you added these in inspector
    3. ...
    4. int number = Random.Range( 1, 4 );
    5. Texture2D selectedTexture = textures[number];
    6.  
    Like Jaimi said, give us an example of what one of your "case" blocks looks like. I don't think anyone can give you much usefull info if you don't show us what you have attemped. I assume it is not just literally
    Code (csharp):
    1.  
    2. case 1:
    3. number = 1;
    4. break;
    5.  
     
    Last edited: Feb 9, 2013
  11. Lypheus

    Lypheus

    Joined:
    Apr 16, 2010
    Posts:
    664
    Create a list of integers from 0 to N-1, where N is the number of pics.

    Let list size be L.
    Choose a random number from 0 to L-1, call it x.
    Remove the x'th element from your list, this is the next image number (or just store image refs, either way).
    Repeat with new list size L -1.

    If it were me, I'd store my images in an array, the each time you want to effectively 'shuffle' your list of images, I'd create an list of indexes to match the array and keep removing one by one to determine next image index to return, or just use a shuffle algorithm in the first place, google Fisher-Yates.
     
  12. Alex Cruba

    Alex Cruba

    Joined:
    Aug 16, 2011
    Posts:
    564
    Sorry I really did not know that my question will make such a wave. My question was not how to do it another way, but ok. Here is a short version of my code. Pressing a button 100 picture icons are displayed in random order. It's so simple, I really don't know how to tell it more easier.

    My only intention for this thread was to shorten the hardcoding for 10, 100, maybe 1.000.000.000.000 icons with one simple loop or something like that.

    Again, I don't need one out of 1.000.000.000.000 icons. I need 1.000.000.000.000 out of 1.000.000.000.000 icons, but in a random order displayed. Like the youtube example - just with a new order of pics every time the button is pressed.

    Code (csharp):
    1. GUI.Label(new Rect((Screen.width - 220) / 2, Screen.height - 540, 50, 50), new GUIContent(""), change_icon + picnumber);
    2.            
    3. public void icon_randomizer() {
    4.    
    5.     int random_value = Random.Range( 1, 11 );
    6.            
    7.             switch( random_value ) {
    8.            
    9.                 case 1:
    10.                 picnumber = 1;
    11.                 break;
    12.                
    13.                 case 2:
    14.                 picnumber = 2;
    15.                 break;
    16.                
    17.                 case 3:
    18.                 picnumber = 3;
    19.                 break;
    20.                
    21.                 case 4:
    22.                 picnumber = 4;
    23.                 break;
    24.                
    25.                 case 5:
    26.                 picnumber = 5;
    27.                 break;
    28.            
    29.                 case 6:
    30.                 picnumber = 6;
    31.                 break;
    32.            
    33.                 case 7:
    34.                 picnumber = 7;
    35.                 break;
    36.            
    37.                 case 8:
    38.                 picnumber = 8;
    39.                 break;
    40.            
    41.                 case 9:
    42.                 picnumber = 9;
    43.                 break;
    44.            
    45.                 case 10:
    46.                 picnumber = 10;
    47.                 break;
    48.                                
    49.             }  
    50.        
    51.     }
     
    Last edited: Feb 9, 2013
  13. JessyDL

    JessyDL

    Joined:
    Nov 27, 2012
    Posts:
    60
    Kinda a juxtaposition, but ok. People are giving you a different way because even your shorter version is still longer and unmaintainable. You are writing a case for every pic number and tbh I don't see why you can't write "picnumber = random_value" instead...

    Eric gave you that answer already and as long as we aren't getting more info/specs of what you want to accomplish, this will remain inadequately solved. For example, you posted the BB intro, but do you want to present these images in succession (second interval) or do you already have the code for that, etc... not enough info here.

    If you use the cases for the pic selection as well (you should state that in your opening question), either have a naming scheme for the images so that this name can be generated in code(f.e. Img1, Img2, Img3, etc...), or have them all in the same folder and load them all to fill in an array, etc... So many ways to do this that isn't hardcoding (avoid this at all times as it's usually wrong).
     
  14. Alex Cruba

    Alex Cruba

    Joined:
    Aug 16, 2011
    Posts:
    564
    I think it's better to stop here. I do the switch case just with less pictures.
     
  15. Swearsoft

    Swearsoft

    Joined:
    Mar 19, 2009
    Posts:
    1,632

    wut? dude seriously?

    instead of
    Code (csharp):
    1. int random_value = Random.Range( 1, 11 );
    do:
    Code (csharp):
    1. picnumber = Random.Range( 1, 11 );
    and delete the rest (the switch case that is)
     
  16. john-essy

    john-essy

    Joined:
    Apr 17, 2011
    Posts:
    464
    Why not just create a for loop and increase an index every 1 second, so the program will show just that current index?
     
  17. Lypheus

    Lypheus

    Joined:
    Apr 16, 2010
    Posts:
    664
    If you are trying for a randomized list of images, take your time and implement what I posted above, it is doing exactly what you want.
     
  18. Lypheus

    Lypheus

    Joined:
    Apr 16, 2010
    Posts:
    664
    Thats not what he's after, he wants to have a list of images shuffled not a random pic from the list. So if he has 10 images, he wants those ten I ages to come back in a random order, ensuring each image is displayed only once per call and all images get one render, but in a random order each time.

    So essentially, he wants to 'shuffle' his list of images, easily done if he takes the time to implement what I've posted.

    I suspect they are trolling for some to post a working code solution at this point ;)
     
  19. EliteMossy

    EliteMossy

    Joined:
    Dec 2, 2012
    Posts:
    513
    why not use an array of the images, and randomize the array.

    Code (csharp):
    1.  
    2.             List<int> numbers = new List<int>();
    3.             List<int> shuffled = new List<int>();
    4.         for (int i = 0; i < 9; i++)
    5.         {
    6.             numbers.Add(i);
    7.         }
    8.         while (shuffled.Count != numbers.Count)
    9.          {
    10.              int rand = Random.Range(0, numbers.Count);
    11.              if (!shuffled.Contains(rand))
    12.              {
    13.                  shuffled.Add(rand);
    14.              }
    15.          }
    16.  
     
    Last edited: Feb 10, 2013
  20. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
  21. Ralyx

    Ralyx

    Joined:
    Feb 10, 2013
    Posts:
    23
    Exactly. But hopefully display the image somewhere as well...