Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Is there a better way to return Int or Null

Discussion in 'Scripting' started by Artpen, Jun 3, 2022.

  1. Artpen

    Artpen

    Joined:
    Jan 24, 2015
    Posts:
    291
    Hi guys,

    I need to get an index of an object from the list so I can use it in other functions.
    What I did seems overcomplicated
    Is there a more cleaner way to return int or nothing ?

    Thank you

    Code (CSharp):
    1. // Select node
    2.     private int? SelectNodeIndex(Vector3 position)
    3.     {
    4.         if (polygons != null)
    5.         {
    6.             foreach (var polygon in polygons.Items)
    7.             {
    8.                 for (int i = 0; i < polygon.Nodes.Count; i++)
    9.                 {
    10.                     if (IsSamePosition(TouchWorldPosition(position), polygon.Nodes[i].Position))
    11.                     {
    12.                         isSelected = true;
    13.                         return i;
    14.                     }
    15.                     else
    16.                     {
    17.                         return null;
    18.                     }
    19.                 }
    20.             }
    21.  
    22.             return null;
    23.         }
    24.         else
    25.         {
    26.             return null;
    27.         }
    28.     }
     
  2. Artpen

    Artpen

    Joined:
    Jan 24, 2015
    Posts:
    291
    To many returns in my code looks messy
     
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,524
    First of all avoid too deep nesting. When you have a method you can use early-exit guard clauses at the beginning. This avoids unnecessary nesting. Further more your else statement inside your for loop doesn't make much sense. After the first loop iteration you return no matter what. I guess you only want to return null when no element matches the condition.

    Something like this:

    Code (CSharp):
    1.     private int? SelectNodeIndex(Vector3 position)
    2.     {
    3.         if (polygons == null)
    4.             return null;
    5.         foreach (var polygon in polygons.Items)
    6.         {
    7.             for (int i = 0; i < polygon.Nodes.Count; i++)
    8.             {
    9.                 if (IsSamePosition(TouchWorldPosition(position), polygon.Nodes[i].Position))
    10.                 {
    11.                     isSelected = true;
    12.                     return i;
    13.                 }
    14.             }
    15.         }
    16.         return null;
    17.     }
    Though I'm not sure how you want to use that returned index. You iterate through all your "polyons" and the index may match a node in "some" of the polygons. You don't return which one. Of course we don't know what those "Nodes" inside a polygon actually are. But just returning the index into the Nodes array of "some" polygon may not be that helpful.

    However those may be logical issues that can only be solved by yourself.
     
    karliss_coldwild likes this.
  4. Artpen

    Artpen

    Joined:
    Jan 24, 2015
    Posts:
    291
    Hi @Bunny83

    Thank you for your advise. The reason for this function is to be able to find a node index, so I can move it later.
    I currently have this function, I though to brake it into two will make sense.
    So one to select an object and second to move it?

    But I just discovered that I will need to get a polygon index as well. As I may have multiple polygons.

    Please let me know what you think as it will help me a lot.

    Thanks again

    Code (CSharp):
    1.   // Move node
    2.     public void MoveNode(Vector3 position)
    3.     {
    4.         if (polygons != null)
    5.         {
    6.             foreach (var polygon in polygons.Items)
    7.             {
    8.                 for (int i = 0; i < polygon.Nodes.Count; i++)
    9.                 {
    10.                     if (IsSamePosition(TouchWorldPosition(position), polygon.Nodes[i].Position))
    11.                     {
    12.                         isSelected = true;
    13.                         selectedNodeIndex = i;
    14.                     }
    15.                 }
    16.  
    17.                 if (isSelected)
    18.                 {
    19.                     Debug.Log("Moving node.");
    20.                     polygon.Nodes[selectedNodeIndex].Position = Vector3.MoveTowards(polygon.Nodes[selectedNodeIndex].Position, TouchWorldPosition(position), 100f);
    21.                 }
    22.             }
    23.  
    24.         }
    25.  
    26.     }
     
  5. Artpen

    Artpen

    Joined:
    Jan 24, 2015
    Posts:
    291
    Each polygon has a collection of nodes and edges. Each node is basically Vector3 position with some helper functions.
    So Polygons and Nodes are Runtime collection which I can change at runtime.
    I use these runtime collections to dynamically update mesh generation