S.Tacked
S.Tacked

Reputation: 13

How to access a Object property, in a List of generated Objects

I searched the heck out of it, and i can't solve it. I have a program setup like this (it's in Unity and Visual Studio 2019 for C#): Note that the CSV loading goes fine, when i debug the code i can see everything filled with corect data.

#region character class
public class _Character
{
    public int Id { get; set; }
    public int Variation { get; set; }
    public string Name { get; set; }
    public string LastName { get; set; }
}
#endregion

//Tools.LoadCsv generates a string[,] from a csv file
//Tools.IntParse parses int's with a lot of format error checking

void Start()
    {
#region load characters class
        string[,] CharacterCSV = Tools.LoadCsv(@"Assets/GameDB/character.csv");
        List<_Character> Character = new List<_Character>();
        for (int i = 1; i < CharacterCSV.GetUpperBound(0); i++)
        {
            _Character temp = new _Character();
            temp.Id = Tools.IntParse(CharacterCSV[i, 0]);
            temp.Variation = Tools.IntParse(CharacterCSV[i, 1]);
            temp.Name = CharacterCSV[i, 2];
            temp.LastName = CharacterCSV[i, 3];

            Character.Add(temp);
        }

        CharacterCSV = null;
        #endregion
}

I barely understand objects, so i'm sorry if i am doing it wrong.

The questions i have are:

  1. Why does the Object List generation Háve to be in Start ? I can't seem to do that in it's own class.

  2. How can i get an object from the Character Object List, containing Id = 100 and Name = "John" , and access it from another class or method.

I ussualy frankenstein the heck out of code and make it good enough for me, but now i wanted to make something nice and cant seem to get to the objects.

Thanks in advance!

//the major issue was declaring the object inside the class, when declared outside the class, the List Object was available to the outside world. List<_Character> Character = new List<_Character>(); move to outside Start{}

I'm not editing the question to correct the code, because the question needs to stay clear. //

Upvotes: 1

Views: 141

Answers (1)

Hellium
Hellium

Reputation: 7346

Why does the Object List generation has to be in Start ? I can't seem to do that in it's own class.

How can i get an object from the Character Object List, containing Id = 100 and Name = "John" , and access it from another class or method.

If you want to retrieve a character from outside of the class, then, you have to declare the list outside of the Start function, otherwise, the list will be destroyed since it's a local variable of the function.

// Declare the list outside of the functions
private List<_Character> characters;

void Start()
{
    // Avoid using regions, they encourage you to make very long functions with multiple responsabilities, which is not advised
    // Instead, create short and simple functions, and call them
    LoadCharactersFromCSV();
}

void LoadCharactersFromCSV()
{
    string[,] CharacterCSV = Tools.LoadCsv(@"Assets/GameDB/character.csv");

    // If you can, indicate the approximate numbers of elements
    // It's not mandatory, but it improves a little bit the performances
    characters = new List<_Character>( CharacterCSV.GetUpperBound(0) );

    // I believe `i` should start at 0 instead of 1
    for (int i = 1; i < CharacterCSV.GetUpperBound(0); i++)
    {
        // I advise you to create a constructor
        // instead of accessing the properties one by one
        _Character temp = new _Character();
        temp.Id = Tools.IntParse(CharacterCSV[i, 0]);
        temp.Variation = Tools.IntParse(CharacterCSV[i, 1]);
        temp.Name = CharacterCSV[i, 2];
        temp.LastName = CharacterCSV[i, 3];

        characters.Add(temp);
    }

    CharacterCSV = null;
}

// Using this function, you will be able to get a character from its id
public _Character GetCharacter( int id )
{
    for (int 0 = 1; i < characters.Count; i++)
    {
        if( characters[i].Id == id )
            return characters[i];
    }

    // Return null if no character with the given ID has been found
    return null ;
}

Then, to call GetCharacter from another class:

public class ExampleMonoBehaviour : MonoBehaviour
{
    // Replace `TheClassName` by the name of the class above, containing the `Start` function
    // Drag & drop in the inspector the gameObject holding the previous class
    public TheClassName CharactersManager;


    // I use `Start` for the sake of the example
    private void Start()
    {
        // According to your code, `_Character` is defined **inside** the other class
        // so you have to use this syntax
        // You can get rid of `TheClassName.` if you declare `_Character` outside of it
        TheClassName._Character john = CharactersManager.GetCharacter( 100 );
    }
}

Upvotes: 1

Related Questions