Search Unity

How to get Key from a Dictionary

Discussion in 'Getting Started' started by Stradmann, Jan 17, 2019.

  1. Stradmann

    Stradmann

    Joined:
    Jun 11, 2018
    Posts:
    38
    Hi, and thanks. I've got a dictionary that is created with <mision(ScriptableObject), misionposition(Randomly generated)> to store this information when its generated. Once this dictionary is created i want to asign the mision info to a button. How i can acces to this info from my asignedMision Dictionary?

    Code (CSharp):
    1.  if (!haveMisionsAsigned && gameRecord.searchedArea == null)
    2.         {
    3.             for (int i = 0; i < areaArray.Length; i++)
    4.             {
    5.                 if (gameRecord.availableAreas[areaArray[i]])
    6.                 {
    7.                     gameRecord.misionsAsigned.Add(misionController.AssignMision("Busqueda", areaArray[i]), areaArray[i].localizations[Random.Range(0,areaArray[i].localizations.Length)].transform);
    8.                 }
    9.             }
    10.         }
    11.         else if (!haveMisionsAsigned && gameRecord.searchedArea != null)
    12.         {
    13.             for (int i = 0; i < 3; i++)
    14.             {
    15.                 gameRecord.misionsAsigned.Add(misionController.AssignMision(misionTypeArray[Random.Range(0, misionTypeArray.Length)], gameRecord.searchedArea), gameRecord.searchedArea.localizations[Random.Range(0,gameRecord.searchedArea.localizations.Length)].transform);
    16.             }
    17.         }
    18.         if (!gameRecord.estaEnMarxaUnaMisio)
    19.         {
    20.            //Get the mision info from dictionary (like mision.misiondescription) to add it in a funcionality of a button
    21.         }
    May be there is other easiest way. In that case, tell it to me too please. Thanks
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    I'm a little bit too confused by your code style to make sense of what exactly you're trying to do here.

    Do you need to loop through a dictionary? Or do you just want to get a single item with a known key?
    Code (CSharp):
    1. public Dictionary<string, YourObject> myDict;
    2.  
    3. // Loop through each value
    4. foreach (KeyValuePair<string, YourObject> item in myDict) {
    5.     // Access the key with item.Key
    6.     // Access the value with item.Value
    7. }
    8.  
    9. // Find a specific item by key, using LINQ (ensure you add using statement to top of script)
    10. using System.LINQ;
    11. YourObject yourObj = myDict.Single(s => s.Key == "someValue").Value;
    Is this what you're after?
     
    WoodyDRN likes this.
  3. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Do you need to get a key, or a value? As in Key/Value pair.
     
  4. Stradmann

    Stradmann

    Joined:
    Jun 11, 2018
    Posts:
    38
    I think that the code Schneider21 told is what i was looking for, especially the foreach loop with being able to acces the key.
    But I don't understand nothing about the 9-11 lines of the code.
    Thanks a lot, and if you want and please, I would like to know the meaning of those lines.
     
  5. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Well I thought the comment spelled it out pretty clearly, but okay...

    If you want to get a single item from a dictionary, using the key, line 11 is how you'd do that. This way uses the LINQ class, so the using statement from like 10 needs to be added to the top of your script with the other dependencies.
     
  6. Stradmann

    Stradmann

    Joined:
    Jun 11, 2018
    Posts:
    38
    sorry... what i don't understand is the meaning of "=>" or what seems "s" variable are doing or tha Single.. is what confuse me.. but thanks anyway:)
     
  7. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    That's a lambda expression. It's a bit more advanced, but very common in LINQ statements.
     
  8. S3cubestudios

    S3cubestudios

    Joined:
    Jun 8, 2015
    Posts:
    4
    This is exactly correct brother. But it is System.Linq. Thanks mate