Finding A class of an array of that by using one of the element

[System.Serializable]
public class Character
{
public string id;
public int Atk;
public int Hp;
public int Def;
}

[System.Serializable]
public class CharacterList
{
public Character[] characters;
}

public Characterlist charlist = new CharacterList();

void Start()
{
charlist = JsonUtility.FromJson<CharacterList>("Assets/data.json".text);
}

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”

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!

You want to look into linq. You can do database type searches on a list.

CharacterList is a super unintuitive name to use, first of all, it’s a class, and second of all it contains an array

1 Like

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.

Naming is Hard ™.

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!”

2 Likes

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.

This guy is correct. Linq is godly…

using System.linq;
var character = charlist.FirstOrDefault(x => x.id == "1");

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.

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?)

Did you add this to the top of your script file?
Double-check spelling etc.

using System.Linq;

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.

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

I’m sorry about that. I think the problem is happened on

[System.Serializable]
public class CharacterList
{
public Character[] characters;
}
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?