Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

For Loop Woes

Discussion in 'Scripting' started by IchBinJager, Apr 27, 2015.

  1. IchBinJager

    IchBinJager

    Joined:
    Dec 23, 2014
    Posts:
    127
    Code (csharp):
    1.  
    2.  
    3. byte mag_max = 20;
    4. new byte[] magazine = byte[3] {20,20,20};
    5.  
    6. byte Get_Best_Mag()
    7. {
    8.  byte best_mag = 0;
    9.  
    10.  for ( byte this_mag = 0; this_mag == magazine.Length - 1; this_mag ++ )
    11.  {
    12.   if ( magazine [this_mag] == mag_max )
    13.   {
    14.    return this_mag;
    15.   }
    16.  
    17.  if ( magazine[this_mag] > magazine[best_mag] )
    18.  {
    19.   best_mag = this_mag;
    20.  }
    21.  
    22.  }
    23.  return best_mag;
    24.  
    25. }
    26.  
    27. It gives me a "Not all paths return a value" error when I try to do this. I'm probably doing something stupid, but can someone help?
     
  2. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Explain what you're trying todo...
     
  3. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    Are you sure this function gives you the error? Here it is again, with cleaner formatting.

    Code (csharp):
    1. byte Get_Best_Mag() {
    2.     byte best_mag = 0;
    3.     for ( byte this_mag = 0; this_mag == magazine.Length - 1; this_mag ++ ) {
    4.         if ( magazine [this_mag] == mag_max ) {
    5.             return this_mag;
    6.         }
    7.         if ( magazine[this_mag] > magazine[best_mag] ) {
    8.             best_mag = this_mag;
    9.         }
    10.     }
    11.     return best_mag;
    12. }
    Line 11 is guarantees you that your code returns a value in the end. I don't see anything wrong with it. :confused:



    Also:
    Code (csharp):
    1. // Why this:
    2. this_mag == magazine.Length - 1
    3. // and not this:
    4. this_mag < magazine.Length
    5. // ?
     
  4. IchBinJager

    IchBinJager

    Joined:
    Dec 23, 2014
    Posts:
    127
    It doesn't return another index other than 0 even when the value at 0 is less than twenty.
     
  5. IchBinJager

    IchBinJager

    Joined:
    Dec 23, 2014
    Posts:
    127
    I even put a Debug.Log() function in each loop block. BUT, when they should be getting called, I get no messages.
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    You're doing this:

    Code (csharp):
    1. for(byte this_mag =0; this_mag == magazine.Length-1; this_mag ++){
    While you should be doing this:

    Code (csharp):
    1. for(byte this_mag =0; this_mag <= magazine.Length-1; this_mag ++){
    You're using == comparrison to find when you need to break out of the loop, which is immideately.

    To understand what's going on, remember that a foor loop that looks like this:

    Code (CSHARP):
    1. for(int i = 0; i < myArray.Length; i++) {
    2.     //Code goes here
    3. }
    Is essentially a nice way to write this:

    Code (CSHARP):
    1. int i = 0;
    2. while(i < myArray.Length) {
    3.     //Code goes here
    4.     i++;
    5. }
    If we write what you wrote in that form, it should be easy to see that the loop never runs:

    Code (CSHARP):
    1. byte this_mag = 0;
    2. while(this_mag == magazine.Length-1) {
    3.     //code
    4.     this_mag++;
    5. }
    Unless your magazine array is exactly 1 long, your loop will never run.

    Also note that there's no reason to use byte as the index in the for-loop. Hell, since you're on a 32 bit engine, there's no real reason to use it at all unless you're planning on writing large amounts of small values to a file, or have some other weird edge case.
     
    SubZeroGaming likes this.
  7. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    ^ I thought that's what the issue was, but wasn't entirely sure what is it he's exactly doing...who uses bytes? lol. Please explain why you're using bytes. I'd love to know.
     
  8. IchBinJager

    IchBinJager

    Joined:
    Dec 23, 2014
    Posts:
    127
    Because they are the smallest variable aside from bool I think. I worry too much about costing performance for computers I suppose, but hey at least when I make stuff they are optimized out of the box, somewhat at least.
     
  9. CrasiusCurio

    CrasiusCurio

    Joined:
    Apr 7, 2015
    Posts:
    1
    When you operate on bytes like this, they're implicitly converted to integers - so with the casting and such, you're actually losing performance.
     
    SubZeroGaming likes this.