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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Variable as class type on switch statement

Discussion in 'Scripting' started by Vad3rInHale, Sep 19, 2018.

  1. Vad3rInHale

    Vad3rInHale

    Joined:
    Apr 19, 2015
    Posts:
    96
    Hello,

    I am trying to make a switch statement on an enum assign a derived class type to a variable.
    This way I could just say GetComponents<ClassType>() instead of this mess here.

    Appreciate any thoughts or recommendations,
    Alex

    Code (CSharp):
    1. Type t = new ChessPiece().GetType();
    2.  
    3.  
    4.         // ROOKS
    5.         if (currentMovePiece == GamePiece.Rook)
    6.         {
    7.             t = Rook;
    8.             moved = TryToMove(GetComponentsInChildren<t>());
    9.         }
    10.  
    11.         // QUEENS
    12.         if (currentMovePiece == GamePiece.Queen)
    13.         {
    14.             moved = TryToMove(GetComponentsInChildren<Queen>());
    15.         }
    16.  
    17.         // KINGS
    18.         if (currentMovePiece == GamePiece.King)
    19.         {
    20.             moved = TryToMove(GetComponentsInChildren<King>());
    21.         }
     
  2. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Why not just use the non-generic GetComponentsInChildren(type) method? Combine that with a Dictionary<enum, Type> and that should clean up a lot of switch statements.
     
    Vad3rInHale likes this.
  3. Vad3rInHale

    Vad3rInHale

    Joined:
    Apr 19, 2015
    Posts:
    96
    You did it my friend, thank you! Now, I only need one line to handle every case :)

    Code (CSharp):
    1. Dictionary<GamePiece, Type> PieceToType = new Dictionary<GamePiece, Type>
    2.     {
    3.         {GamePiece.Pawn, typeof(Pawn)},
    4.         {GamePiece.Knight, typeof(Knight)},
    5.         {GamePiece.Bishop, typeof(Bishop)},
    6.         {GamePiece.Rook, typeof(Rook)},
    7.         {GamePiece.Queen, typeof(Queen)},
    8.         {GamePiece.King, typeof(King)}
    9.     };
    10.  
    11.     void MoveCurrentPiece()
    12.     {
    13.         bool moved = false;
    14.  
    15.         moved = TryToMove(GetComponentsInChildren(PieceToType[currentMovePiece]));
    16.      
    17.     }
    18.  
    19.     bool TryToMove(Component[] pieces)
    20.     {
    21.         foreach(ChessPiece p in pieces)
    22.         {
    23.             if (p.mySide != currentTurnSide) continue;
    24.             if (p.AttemptMoveToPosition(currentMoveFile, currentMoveRank, capturing))
    25.             {
    26.                 if (originalFile != 0 && p.originalFile != originalFile)
    27.                 {
    28.                     return false;
    29.                 }
    30.                 return true;
    31.             }
    32.         }
    33.  
    34.         return false;
    35.     }
     
    BlackPete likes this.