Search Unity

Not recognizing Dictionary keys?

Discussion in 'Scripting' started by ChrisMG, Sep 29, 2018.

  1. ChrisMG

    ChrisMG

    Joined:
    Sep 21, 2018
    Posts:
    28
    Here is my code which i am using for making my fighting game:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TrialScript : MonoBehaviour
    6. {
    7.     public string InputChain = "";
    8.     public string Technique = "";
    9.  
    10.     IDictionary<string, string> TechniqueCombination = new Dictionary<string, string>()
    11.     {
    12.         {"DF","Hadouken"},
    13.         {"DB","Light Tatsu"}
    14.     };
    15.     public string[] keys;
    16.  
    17.     static public KeyCode up = KeyCode.W;
    18.     static public KeyCode down = KeyCode.S;
    19.     static public KeyCode left = KeyCode.A;
    20.     static public KeyCode right = KeyCode.D;
    21.     static public KeyCode lpunch = KeyCode.Keypad7;
    22.     static public KeyCode mpunch = KeyCode.Keypad8;
    23.     static public KeyCode hpunch = KeyCode.Keypad9;
    24.     static public KeyCode lkick = KeyCode.Keypad4;
    25.     static public KeyCode mkick = KeyCode.Keypad5;
    26.     static public KeyCode hkick = KeyCode.Keypad6;
    27.     static public KeyCode none = KeyCode.None;
    28.  
    29.     public int Alarm1 = -1;
    30.     public int Alarm2 = -1;
    31.     public bool inputhappenning = false;
    32.     public bool techniquehappening = false;
    33.     public float forward;
    34.  
    35.     public Fighter FirstFighter;
    36.     public Fighter SecondFighter;
    37.  
    38.     public int instance_id;
    39.  
    40.  
    41.  
    42.     void Start () {
    43.         instance_id = GetInstanceID();
    44.  
    45.         var keysCol = TechniqueCombination.Keys;
    46.         var keysList = new List<string> (keysCol);
    47.         keys = keysList.ToArray();
    48.     }
    49.  
    50.    
    51.     void Update () {
    52.         var valuesAsArray = System.Enum.GetValues(typeof(KeyCode));
    53.  
    54.         Transform trm = FirstFighter.ControlledRoot;
    55.         float theirpos = trm.position.x; // First fighter position
    56.         float mypos = transform.position.x;
    57.         forward = Mathf.Sign(theirpos-mypos);
    58.         if (Input.GetKeyDown(up))
    59.         {
    60.             InputChain += "U";
    61.         }
    62.         if (Input.GetKeyDown(down))
    63.         {
    64.             InputChain += "D";
    65.         }
    66.         if (Input.GetKeyDown(left))
    67.         {
    68.             if (forward == -1) { InputChain += "F"; }
    69.             else if (forward == 1) { InputChain += "B"; }
    70.         }
    71.         if (Input.GetKeyDown(right))
    72.         {
    73.             if (forward == 1) { InputChain += "F"; }
    74.             else if (forward == -1) { InputChain += "B"; }
    75.         }
    76.         //Alarm Thing #1
    77.         if (!Input.anyKey && (!inputhappenning) && InputChain!="")
    78.         {
    79.             Alarm1 = 15;
    80.             inputhappenning = true;
    81.         }
    82.         if (Input.anyKey)
    83.         {
    84.             inputhappenning = false;
    85.         }
    86.  
    87.         if (Alarm1!=-1)
    88.         {
    89.             Alarm1 -= 1;
    90.         }
    91.  
    92.         if (Alarm1 == 0)
    93.         {
    94.             InputChain = "";
    95.             inputhappenning = false;
    96.         }
    97.  
    98.         //Alarm Thing #2
    99.         //if (Technique != "" && techniquehappening == false && !Input.anyKey)
    100.         //{
    101.         //    Alarm2 = TechniqueFrames[Technique];
    102.         //    techniquehappening = true;
    103.         //}
    104.            
    105.  
    106.         //if (Alarm2!=-1)
    107.         //{
    108.         //    Alarm2 -= 1;
    109.         //}
    110.         //if (Alarm2==-1)
    111.         //{
    112.         //    techniquehappening = false;
    113.         //}
    114.         //if (Alarm2==0)
    115.         //{
    116.         //    Technique = "";
    117.         //}
    118.         //Checking if InputChain contains Key values
    119.         foreach (string x in keys)
    120.         {
    121.             if (InputChain.EndsWith(x) && !Input.anyKeyDown)
    122.             {
    123.                 Technique = TechniqueCombination[x];
    124.             }
    125.         }
    126.         //foreach (KeyCode x in valuesAsArray)
    127.         //{
    128.         //    if (Input.GetKeyDown(x))
    129.         //    {
    130.         //        Debug.Log(x);
    131.         //    }
    132.         //}
    133.     }
    134.     void OnGUI()
    135.     {
    136.         GUI.Label(new Rect(10, 10, 1000, 20), InputChain);
    137.         GUI.Label(new Rect(10, 20, 1000, 20), Technique);
    138.         GUI.Label(new Rect(10, 30, 100, 20), Alarm1.ToString());
    139.         GUI.Label(new Rect(10, 40, 100, 20), Alarm2.ToString());
    140.  
    141.  
    142.  
    143.     }
    144. }
    145.  
    When using the above code it is made so that if InputChain ends with a key from dictionary TechniqueCombination, Technique will be equal to the value of that key. However when using the code DF does indeed appear but Technique doesn't change, however DB does work and Light Tatsu does appear, is there something wrong in this code?
     
  2. ChrisMG

    ChrisMG

    Joined:
    Sep 21, 2018
    Posts:
    28
    Replace lines 52.to 57. and write forward =1 for convenience sake