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 Finding A class of an array of that by using one of the element

Discussion in 'Scripting' started by Pideoms, May 25, 2022.

  1. Pideoms

    Pideoms

    Joined:
    May 21, 2022
    Posts:
    4
    Code (CSharp):
    1. [System.Serializable]
    2. public class Character
    3. {
    4. public string id;
    5. public int Atk;
    6. public int Hp;
    7. public int Def;
    8. }
    9.  
    10. [System.Serializable]
    11. public class CharacterList
    12. {
    13. public Character[] characters;
    14. }
    15.  
    16. public Characterlist charlist = new CharacterList();
    17.  
    18. void Start()
    19. {
    20. charlist = JsonUtility.FromJson<CharacterList>("Assets/data.json".text);
    21. }
    22.  
    23.  
    in this code, I can run out the character list in the gameObject.
    However, i dont know how to get the whole class data by one of the elements ( such as "id")

    For example, to get
    {
    id = "a1"
    int = 1
    Hp = 3
    Def = 4
    }

    by the "id" = "a1"
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Since you're using a list, you'd need to search all the entries.

    You could also search all the entries ONCE and put them in a Dictionary keyed by this
    id
    field.

    Then you could simply look it up by id in the Dictionary!
     
  3. darkwingfart

    darkwingfart

    Joined:
    Oct 13, 2018
    Posts:
    80
    You want to look into linq. You can do database type searches on a list.
     
  4. pixaware_pwedrowski

    pixaware_pwedrowski

    Joined:
    Oct 25, 2018
    Posts:
    116
    CharacterList
    is a super unintuitive name to use, first of all, it's a class, and second of all it contains an array
     
    Kurt-Dekker likes this.
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,528
    Well, that's debatable. You probably know the type
    System.Collections.Generic.List<T>
    , right? Guess what, it's a class that contains an array and is named List ^^.

    Since for proper deserialization (with the JsonUtility) you need a wrapping class for the root element, I don't really see an issue with that name. Others may have given the class a generic name like "CharacterData" which tells you even less.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Naming is Hard (tm).

    I'd go with
    CharacterRepository
    for the entire thing, and
    AllCharacters
    or just
    Characters
    for my internal array.

    "We have a fistfight over the classname in aisle 3... cybersecurity... stat!"
     
    pixaware_pwedrowski and Bunny83 like this.
  7. pixaware_pwedrowski

    pixaware_pwedrowski

    Joined:
    Oct 25, 2018
    Posts:
    116
    Interesting, I didn't realize what's under the hood of the List. Anyway, I think that
    CharacterRepository
    as @Kurt-Dekker is a much better name, as it doesn't confuse with the list's existence.
     
  8. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    This guy is correct. Linq is godly...

    Code (CSharp):
    1.  
    2. using System.linq;
    3. var character = charlist.FirstOrDefault(x => x.id == "1");
    4.  
    In this case, 'FirstOrDefault' will return the object found, or an empty object.
    So after that, you can check 'is object null'.

    Here is a live example of linq in use. https://www.w3schools.com/cs/trycs.php?filename=demo_array_linq

    There are so many ways it can be helpful. Best to learn them all.
     
  9. Pideoms

    Pideoms

    Joined:
    May 21, 2022
    Posts:
    4
    When i used the Linq, The CS1061 is appear. How can I do

    error CS1061: '?.CharacterList' does not contain a definition for 'FirstOrDefault' and no accessible extension method 'FirstOrDefault' accepting a first argument of type '?.CharacterList' could be found (are you missing a using directive or an assembly reference?)
     
  10. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    Did you add this to the top of your script file?
    Double-check spelling etc.

    Code (CSharp):
    1. using System.Linq;
     
  11. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    Ahh, I seel.
    What I gave you was an example. Thought you understood this is meant to be used on Lists..
    This is the code you want.

    Did you google using LINQ though? Bad practice to just copy/paste what you don't understand.

    Code (CSharp):
    1. var character = charlist.characters.FirstOrDefault(x => x.id == "1");

     
  12. Pideoms

    Pideoms

    Joined:
    May 21, 2022
    Posts:
    4
    I'm sorry about that. I think the problem is happened on
    Code (CSharp):
    1. [System.Serializable]
    2. public class CharacterList
    3. {
    4. public Character[] characters;
    5. }
    6. public Characterlist charlist = new CharacterList();
    which mean the charlist is not the array or list, which is a class named "CharacterList"
    I don't know am I understand correctly. But if that is correct, how can I solve the problem?